We help IT Professionals succeed at work.

How to populate a list box in form1 from form2

pratikshahse
pratikshahse asked
on
351 Views
Last Modified: 2010-04-15
I have a listbox in form1 "list1" . I got a class file where I want to populate "list1".  But I cannot see the controls of form1 in my class file.

This how I call the routine in the class file from form1

Module1.LoadFranchiseTypes(this)

This is how I am trying to add items in my listbox from Module1(class file).

frmForm.lstFranType..items.add(strFranName.Trim())

but its throwing me an error saying "lstFranType is not defined".
Comment
Watch Question

Commented:
Are you referring to the instance of form1, or are you using the actual class of the form?  For example, if you have a form called MyForm, you need to instantiate it by doing MyForm formInstance = new MyForm(), and then you can access it's members by doing formInstance.lstFranType<etc>.  If you were to do MyForm.lstFranType<etc> it wouldn't work since you're not referring to an instance.

If that's not the issue, perhaps you can post a little more code to give some additional context for us to help you.

Author

Commented:
I tried to instantiate "frmReversalSetup" (actual name of the form) but when I tried to look up its controls I did not find any of its controls.

Here is the code that I use in module1 to populate the list box

public static bool LoadFranchiseTypes(form frmForm)
            {
                  bool LoadFranchiseTypes = false;
                  SqlParameter[] sqlParms = null;
                  SqlDataReader rsTypes = null;
                  string strFranName = "";
                  frmReversalSetup frmReversalSetupInstance = new frmReversalSetup();
           
           

                  try
                  {
                        rsTypes = DesktopDataMeister.ExecuteReader(Server, CommandType.StoredProcedure, "store proc", sqlParms);
                        if (rsTypes.HasRows)
                        {
                              while(rsTypes.Read())
                              {
                                    switch(Convert.ToInt32(rsTypes["franchise_type_cde"]))
                                    {
                                    
                                    case 1:
                                          strFranName = rsTypes["franchise_type_nme"].ToString();
                                          break;
                                    case 2:
                                          // do nothing
                                    case 3:
                                          // do nothing
                                    case 4:
                                          strFranName = rsTypes["franchise_type_nme"].ToString();
                                          break;
                                    default:
                                          strFranName = rsTypes["franchise_type_nme"].ToString();
                                          break;
                                    }
                       
                                    frmReversalSetupInstance..lstFranType .items.add (strFranName )
                                    
                              LoadFranchiseTypes = true;
                              rsTypes.Close();
                        }
                  }
                  catch(Exception exc)
                  {
                  }
                  finally
                  {
                        if(rsTypes != null) rsTypes.Close();
                  }


                  // Err.Clear
                  return LoadFranchiseTypes;

                  string strError = "";

Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
The Microsoft User Interface library is object-oriented. You might be wise to follow OO design while working with those forms.
May I suggest that in the code you show above, collect all the Franchise names into a string collection. Then on the constructor of the frmReversalSetup object (form) pass in this string collection.
Does this make sense?
In other words, divide and conquer your code. No one outside of the frmReversalSetup object needs to know how to load things up into its list boxes. And if one day you decide to switch to a combo box or list view, or whatever, no code outside of the frmReversalSetup is affected.
Cheers,
Joe

Commented:
Also, when you pass in the frmReversalSetup as a parameter, you can directly access the member as frmForm.lstFranType (you won't need to instantiate it again within your LoadFranchiseTypes function).
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.