populating a listbox of a form by clicking a button of another form

Hi everybody, I am creating an application in C#(.net).

Here there are 2 forms.
When the application runs form1 runs first. There is a button1 on form1.If I click the button form2 appears as active form. form1 remains inactive but visible.
Now there is a button2 on form2. If I click the button2 form2 disappears
(this.visible = false). But by clicking button2 I also wanna populate a listbox that is on form1.

That is, after clicking button2 form2 will disappear and updated form1 will become visible.

How can I do this?

Comments

  • This is one option.

    Create a Class which both forms can share as follows:

    class Class1
    {
    public static string[] myList = { "", "", "", "" };
    }

    Set the list box on form1 to point at the Data held on the shared class as follows:

    private void Form1_Activated(object sender, EventArgs e)
    {
    listBox1.Items.AddRange(Class1.myList);
    }

    On the exit button of form two you would hide the form and update the shared data on the shared class with your new data as follows:

    private void button1_Click(object sender, EventArgs e)
    {
    Class1.myList[0] = "One";
    Class1.myList[1] = "Two";
    Class1.myList[2] = "Three";
    Class1.myList[3] = "Four";
    this.Visible = false;
    }

    This would close form two and update form1

    I would be interested in other sugestions which may be more efficient.

    Good Luck

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion