Visual Basic Development Bookmark and Share   
 Home > Visual Basic General > Reference a form from a variable
 

Reference a form from a variable

Hi All,
I am upgrading a very large project from VB6 to VS2008. I have several forms with dozens of combo boxes on each form. I use the same combo box names for each form and want to fill them from a module with different items depending upon the calling form. In VB6, I dimensioned a public variable - FormName as object and called Set FormName= frmMyForm1 on Form Load. In a module, I was able to write the following code:

With FormName
.combobox1.additem("item")
.combobox2.additem("item"
End With

I cannot make this work in VB.Net. When the form loads Icall FormName = Me. If I dimension FriendFormName as Object, I get the error message that Public member combobox1 on type MyForm1 not found. If you are attempting to access members on a late-bound object make sure itis declared public". If I dimension FriendFormName as Form, I get an error that says "combobox1 is not a member of System.Windows.Forms.Form".

I have tried both CType and DirectCast to convert the formvariable from Object to Form with no success. I also tried creating a public array and storing each form name in it. No luck

Any help would be much appreciated as it will take countless hours to rewrite all of this code. Thanks!

Lisa

Lisa S  Tuesday, November 24, 2009 3:36 AM
There are several ways you can accomplish this. One way isto pass the combobox in as a parameter to the function.

Another option (a more .NET type option) is to have themodule code return a list to the form. Then have the form assign the DataSource of the ComboBox to the passed list.

Hope this helps.
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Tuesday, November 24, 2009 3:58 AM
Hi Lisa,

Try this

Private Sub cmdShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdShow.Click
    AddComboItems(Me)
  End Sub

  Private Sub AddComboItems(ByVal FormName As Form)
    With FormName
      CType(.Controls("combobox1"), ComboBox).Items.Add("item")
    End With
  End Sub
you can refer to a control by string name. this will be safe as long as the form contains a control by the supplied string name
FREE DEVELOPER TOOLS, CODE & PROJECTS at www.srsoft.us Database Code Generator and Tutorial
Jeff - www.SRSoft.us  Tuesday, November 24, 2009 4:04 AM
FormName should be declared as type Form1 (or whatever type 'Me" is in the above example), but this is not necessary. By default, forms are public within a project, so within Form2 you should be able to simply use:

TextBox1.Text = FirstForm.ComboBox1.SelectedItem.ToString
or equivalent for your form and control names.
Acamar  Tuesday, November 24, 2009 4:04 AM
There are several ways you can accomplish this. One way isto pass the combobox in as a parameter to the function.

Another option (a more .NET type option) is to have themodule code return a list to the form. Then have the form assign the DataSource of the ComboBox to the passed list.

Hope this helps.
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Tuesday, November 24, 2009 3:58 AM
Hi Lisa,

Try this

Private Sub cmdShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdShow.Click
    AddComboItems(Me)
  End Sub

  Private Sub AddComboItems(ByVal FormName As Form)
    With FormName
      CType(.Controls("combobox1"), ComboBox).Items.Add("item")
    End With
  End Sub
you can refer to a control by string name. this will be safe as long as the form contains a control by the supplied string name
FREE DEVELOPER TOOLS, CODE & PROJECTS at www.srsoft.us Database Code Generator and Tutorial
Jeff - www.SRSoft.us  Tuesday, November 24, 2009 4:04 AM
FormName should be declared as type Form1 (or whatever type 'Me" is in the above example), but this is not necessary. By default, forms are public within a project, so within Form2 you should be able to simply use:

TextBox1.Text = FirstForm.ComboBox1.SelectedItem.ToString
or equivalent for your form and control names.
Acamar  Tuesday, November 24, 2009 4:04 AM
Hi All,

Thanks very much! You solved my problem. Don't know why I did not see it.

Lisa
Lisa S  9 hours 8 minutes ago

You can use google to search for other answers

Custom Search

More Threads

• Is there any SP++ like tool available for JAVA based applications?
• how to create package file in vb.net window application
• Big equations without the real deal equation symbols.
• Extract 'description' information from an MS Access table using visual basic
• XML Serialization of ArrayList
• Dynamic Combo Box Help
• VB-Express 2005 and Starter kit Movie collection
• Sorting array names by value in array
• Slow performance on Paint Event
• viewing the next photographs from folder