13

I have two forms. First, Form1 has a group box, some labels and a listbox. I press a button and new Form2 is opened and contains some text. I want to transfer the text in Form2 to the listbox in the Form1.

So far, what I have done is make modifier of listbox to public and then put this code in the button of Form2

Form1 frm = new Form1();
frm.ListBox.items.Add(textBox.Text);

But amazingly, this does not add any value. I thought I was mistaken with the insertion so I made the same procedure. This time, I made a label public and added textbox value to its Text property but it failed.

Any ideas?

Afnan Bashir
  • 7,319
  • 20
  • 76
  • 138

6 Answers6

21

Try adding a parameter to the constructor of the second form (in your example, Form1) and passing the value that way. Once InitializeComponent() is called you can then add the parameter to the listbox as a choice.

public Form1(String customItem)
{
  InitializeComponent();
  this.myListBox.Items.Add(customItem);
}

// In the original form's code:
Form1 frm = new Form1(this.textBox.Text);
Brad Christie
  • 100,477
  • 16
  • 156
  • 200
7

Let's assume Form1 calls Form2. Please look at the code:

Form1:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 frm = new Form2();
        frm.Show();
        frm.VisibleChanged += formVisibleChanged;


    }

    private void formVisibleChanged(object sender, EventArgs e)
    {
        Form2 frm = (Form2)sender;
        if (!frm.Visible)
        {
            this.listBox1.Items.Add(frm.ReturnText);
            frm.Dispose();
        }


    }

}

Form2:

 public partial class Form2 : Form
{

    public string ReturnText { get; set; }

    public Form2()
    {
        InitializeComponent();

    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.ReturnText = this.textBox1.Text;
        this.Visible = false;

    }


}

The answer is to declare public property on Form2 and when form gets hidden. Access the same instance and retrieve the value.

Kamil Krasinski
  • 511
  • 4
  • 10
6

Below code working perfect on my machine.

private void button1_Click(object sender, EventArgs e)
{
    Form1 f1 = new Form1();
    f1.listBox1.Items.Add(textBox1.Text );//ListBox1 : Modifier property made public
    f1.ShowDialog();
}

Ok, If you are Calling Sequence is like, Form1->Form2 and Form2 updates the value of Form1 then you have to use ParentForm() or Delegate to update the previous form.

ItsJ0el
  • 55
  • 1
  • 5
indiPy
  • 7,844
  • 3
  • 28
  • 39
  • Why we have to show already shown form again I am going from form one to form 2 enter value on form 2 and that value should be updated to form 1 – Afnan Bashir Jan 03 '11 at 20:59
  • Modified the answer check the links which may help you. Like I said ParentForm() or Delegate will work here. – indiPy Jan 04 '11 at 19:50
4
Form1 frm = new Form1();

frm is now a new instance of class Form1.

frm does not refer to the original instance of Form1 that was displayed to the user.

One solution is, when creating the instance of Form2, pass it a reference to your current instance of Form1.

mbeckish
  • 10,485
  • 5
  • 30
  • 55
2

Please avoid the concept of making any public members like you said >>i have done is make modifier of listbox to public and then in form2 in button code<< this is not a good practice,on the other hand the good one is in Brad Christie's Post,I hope you got it.

  • 4
    Welcome to stackoverflow. Your comment -- a very good point -- should probably be a comment rather than an answer. And be sure to up-vote the answer you did like. Again, welcome! – Marvo Nov 11 '12 at 04:22
0

This code will be inside the form containing myListBox probably inside a button click handler.

Form2 frm2 = new Form2();
frm2.ShowDialog();
this.myListBox.Items.Add(frm2.myTextBox.Text);
frm2.Dispose();
Kevin
  • 2,281
  • 1
  • 14
  • 16
  • You won't see the listbox changes until the new form's already been closed (since you're calling ShowDialog before populating the listbox). The code will wait for a DialogResult from ShowDialog, then proceed on to add a listbox item, then immediately dispose. – Brad Christie Jan 03 '11 at 20:35