hitechoutlaw Posted April 16, 2003 Posted April 16, 2003 how do i add an item to "ComboBox1" in "Form1" from a class? Quote
jjjamie Posted April 16, 2003 Posted April 16, 2003 The easiest way is to create a subroutine that takes the ComboBox as a parameter and then modify it from there. For example in the class write... Public Sub AddComboItem(ByVal ComboBox as ComboBox, ByVal Item as String) ComboBox.Items.Add(Item) End Sub Then all you have to do is call the subroutine from Form1 (or from elsewhere within the class). Jamie Quote
*Experts* Volte Posted April 16, 2003 *Experts* Posted April 16, 2003 You need to have the instance of the form object stored in a variable which can be passed to the class. At that point you can domyFormVariable.ComboBox1.Items.Add("This and That")To set a variable to a form's instance, put the following like on the Form's constructor or _Load event:myFormVariable = Me Quote
hitechoutlaw Posted April 17, 2003 Author Posted April 17, 2003 (edited) the class is in its own file to make it easier for me to get back to when i need to. the onload thing wont work (i tried it) i tried: Dim Form1 As Form1 Form1.ComboBox1.Items.Add("United States(Dollar)") Form1.ComboBox1.Items.Add("Canada(Dollar)") note: this is a curency converter. Edited April 17, 2003 by hitechoutlaw Quote
hitechoutlaw Posted April 17, 2003 Author Posted April 17, 2003 and o ya what i tried dont work either, ne suggestions? Quote
hitechoutlaw Posted April 17, 2003 Author Posted April 17, 2003 ok maybe i said my question wrong... i want to add an item to a list box by calling it from a class. i.e.: i call the class Class1.Action1 in the action i want to add items to a list box. Quote
*Experts* Nerseus Posted April 17, 2003 *Experts* Posted April 17, 2003 Didn't you see jjjamie's post? Here's the code: ' Put this in Class1 Public Sub Action1(ByVal ComboBox As ComboBox, ByVal Item As String) ComboBox.Items.Add(Item) End Sub You can leave off Item if you want, if the Class1 object is going to know what to add. -Nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
hitechoutlaw Posted April 17, 2003 Author Posted April 17, 2003 well theres a problem to that. because its in a class it dosnt see the combobox on form1. but thanks for ir help Quote
*Experts* Nerseus Posted April 17, 2003 *Experts* Posted April 17, 2003 I assume Class1 is an object created in the form. The form is callign the Action1 method on the instance of Class1. The form certainly knows about the ComboBox and can pass it along to the class. -Ner Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
hitechoutlaw Posted April 17, 2003 Author Posted April 17, 2003 well i have the class in a differnt file and it doesnt know the combo box is there. iif i have to i'll put it in the main form. Quote
hitechoutlaw Posted April 17, 2003 Author Posted April 17, 2003 ok heres what i got and its in form1 with the combobox: Class fill Public Shared Sub fillUSCAN() ComboBox2.Items.Add("United States(Dollar)") ComboBox2.Items.Add("Canada(Dollar)") End Sub End Class it tells me 'Referance to a non shared member requires an object referance.' Quote
*Experts* Nerseus Posted April 17, 2003 *Experts* Posted April 17, 2003 Try this sample project. -Nerwindowsapplication1.zip Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
hitechoutlaw Posted April 17, 2003 Author Posted April 17, 2003 oic that makes sense, thanks man Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.