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! - Marked As Answer byMartin Xie - MSFTMSFT, ModeratorMonday, November 30, 2009 8:08 AM
-
| | 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- Marked As Answer byMartin Xie - MSFTMSFT, ModeratorMonday, November 30, 2009 8:08 AM
-
| | 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. - Marked As Answer byMartin Xie - MSFTMSFT, ModeratorMonday, November 30, 2009 8:08 AM
-
| | 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! - Marked As Answer byMartin Xie - MSFTMSFT, ModeratorMonday, November 30, 2009 8:08 AM
-
| | 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- Marked As Answer byMartin Xie - MSFTMSFT, ModeratorMonday, November 30, 2009 8:08 AM
-
| | 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. - Marked As Answer byMartin Xie - MSFTMSFT, ModeratorMonday, November 30, 2009 8:08 AM
-
| | 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 |
|