PDA

Click to See Complete Forum and Search --> : [2.0] Adding items to a combobox


Fromethius
Dec 5th, 2006, 09:11 AM
Allright, I have a.. Form1 and a Form2. I have one combobox on Form2 and one button on Form1. When I click the button on Form1 I want it to add the word "Hello" to the combobox on Form2.

Also, if I wanted to do the same thing but with a button, how would I do that?

Also, if inside a void on Form1 I have a series of code that spits out a number each time. Like.. for the Button1_Click event on Form1 it uses a formula using the Textboxes on Form1 to get a number, how can I add that to a combobox on Form2?


I am pretty stumped on multi-form interaction

Fromethius
Dec 5th, 2006, 04:34 PM
Btw, Form2 is not opened when the button on Form1 is clicked

jmcilhinney
Dec 5th, 2006, 05:54 PM
If object A wants to affect object B then it requires a reference to object B. That's a universal law of OOP. Does your Form1 have a reference, either directly or indirectly, to Form2? If it doesn't then what you ask is impossible. That's your first order of business.

If, and when, Form1 does have a reference to Form2 then it's a simple matter of accessing its properties and methods, exactly as is the case for all objects. You either access the ComboBox directly if it is public, or else have Form2 provide a method that Form1 can call that will take a value and add it to the ComboBox internally. The second way is preferable.

Remember that forms are simply objects like any other and obey the same laws as every other object in your code. Do not think of them or treat them any differently to any other type of object. When you design a form in the IDE you are defining a class, and every form you create at run time is an instance of that class. That's no different to all the strings you create at run time being instances of the String class.

I suggest that you read the "Forms in VB.NET" tutorial in my signature. The code samples are VB.NET but the principles are exactly the same in all .NET langauges.