Results 1 to 7 of 7

Thread: Add data to ComboBox [Resolved]

  1. #1

    Thread Starter
    Addicted Member jordan23's Avatar
    Join Date
    Dec 2002
    Posts
    166

    Add data to ComboBox [Resolved]

    This is the problem. I can add data to a combobox if it is in the same class.

    For example:
    String x = "Test";
    combobox1.Item.Add(x);

    But if I have the data in another class I cannot add it.

    For example

    String x = "Test";
    Form1 myForm = new Form1();
    myForm.combobox1.Item.Add(x);


    But it does not work. Any ideas? I am new to C#, so sorry if this is simple.
    Last edited by jordan23; May 17th, 2004 at 08:20 AM.

  2. #2
    Addicted Member
    Join Date
    Jun 2003
    Location
    Birmingham, AL
    Posts
    188
    what error are you getting

  3. #3

    Thread Starter
    Addicted Member jordan23's Avatar
    Join Date
    Dec 2002
    Posts
    166
    I don't get an error, but it doesn't load the data to the combo box. I can't seem to get data from one class into a control in another.

  4. #4
    Frenzied Member TomGibbons's Avatar
    Join Date
    Feb 2002
    Location
    San Diego, CA Previous Location: UK
    Posts
    1,345
    From what I remember (having done something similar some time ago) what that is doing, is loading a new instance of the form and adding the item to the list on that new form. But you're not showing the form or making it visible so you wouldn't notice. You have to change a line in the class's constructor so that it saves the form that loaded it in a variable or something like that. I will have a look but there is something for you to look out for.

  5. #5

    Thread Starter
    Addicted Member jordan23's Avatar
    Join Date
    Dec 2002
    Posts
    166
    Thanks for the hint. Yeah, if you could give me an example that would be great. I understand in english what you are saying, but my C#(or lack of) terminology is pretty bad. Thanks again.

  6. #6
    Lively Member ayan's Avatar
    Join Date
    Jan 2004
    Posts
    112
    show it
    Code:
    string x="test";
    Form2 f=new Form2();
    f.comboBox1.Items.Add(x);
    f.comboBox1.SelectedIndex=0;
    f.Show();
    provided that comboBox1 is public member on Form2. hope this helps. if not, sorry.

  7. #7

    Thread Starter
    Addicted Member jordan23's Avatar
    Join Date
    Dec 2002
    Posts
    166
    Thanks ayan. That does make sense. Your code worked perfectly. Thanks again.

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