[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to populate a list box in form1 from form2

Posted on 2007-10-09
5
Medium Priority
?
328 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".
0
Comment
Question by:pratikshahse
  • 3
5 Comments
 
LVL 4

Expert Comment

by:jimstar
ID: 20040379
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
 

Author Comment

by:pratikshahse
ID: 20040595
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
 
LVL 4

Accepted Solution

by:
jimstar earned 2000 total points
ID: 20040684
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
 
LVL 7

Expert Comment

by:joesthebighmoe
ID: 20040692
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
 
LVL 4

Expert Comment

by:jimstar
ID: 20040701
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question