Link to home
Start Free TrialLog in
Avatar of pratikshahse
pratikshahse

asked on

How to populate a list box in form1 from form2

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".
Avatar of jimstar
jimstar

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.
Avatar of pratikshahse

ASKER

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 = "";

ASKER CERTIFIED SOLUTION
Avatar of jimstar
jimstar

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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).