Results 1 to 3 of 3

Thread: [2.0] Adding items to a combobox

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    [2.0] Adding items to a combobox

    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

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: [2.0] Adding items to a combobox

    Btw, Form2 is not opened when the button on Form1 is clicked

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Adding items to a combobox

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width