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".
pratikshahseAsked:
Who is Participating?
 
jimstarCommented:
From your orignal question, the reason it couldn't find the member was that you are passing it to the function as a "form", so it loses some of the information about what kind of form it is. Try passing the parameter as the type of form it is - frmReversalSetup.

Then, from wherever you are calling LoadFranchiseTypes, you'll pass in the instantiation of the form that you wish to load the data into.

public static bool LoadFranchiseTypes(frmReversalSetup frmForm)
            {
                  bool LoadFranchiseTypes = false;
                  SqlParameter[] sqlParms = null;
                  SqlDataReader rsTypes = null;
...
0
 
jimstarCommented:
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.
0
 
pratikshahseAuthor 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 = "";

0
 
joesthebighmoeCommented:
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
0
 
jimstarCommented:
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).
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.