Coolguy
Coolguy

Reputation: 2285

C# how to pass the value of form3 back to form1

This question is the followup to the following question: C# Text don't display on another form after double clicking an item in listbox

Now I have typed my value in the textbox of form3. How am I going to pass back the value to form1 to show it in the listbox10 after pressing "OK" in form3? Below is my form3 coding but it don't work:

private void button1_Click(object sender, EventArgs e)
{   
    //This is the coding for "OK" button.
    int selectedIndex = listBox10.SelectedIndex;
    listBox10.Items.Insert(selectedIndex, textBox1.Text);
}

Upvotes: 0

Views: 1094

Answers (3)

Danilo Vulović
Danilo Vulović

Reputation: 3063

You can put public property on form3:

public partial class form3 : Form
{
    public String SomeName
    {
        get
        {
            return textbox1.Text;
        }
    }

    ...
    private void buttonOK_Click(object sender, EventArgs e)
    {
        DialogResult = DialogResult.OK;
        Close();
    }

    private void buttonCancel_Click(object sender, EventArgs e)
    {
        DialogResult = DialogResult.Cancel;
        Close();
    }
 }

In form1, where you are open form3, after ShowDialog, you will write:

if (form3.ShowDialog() == DialogResult.OK)
{

    int selectedIndex = listBox10.SelectedIndex;

    if (selectedIndex == -1) //listbox does not have items
        listbox10.Add(form3.SomeValue);
    else
        listBox10.Items.Insert(selectedIndex, form3.SomeName);
}

Upvotes: 1

Nikola Davidovic
Nikola Davidovic

Reputation: 8656

It should go like this, this is the safest way to do it, in fact if you are working on Windows Mobile this is the only way that won't crash the application. In desktop versions it can crash in debug versions.

 public partial class Form1 : Form
{
    public string name = "something";
    public Form1()
    {
        InitializeComponent();
    }

    public delegate void nameChanger(string nme);
    public void ChangeName(string nme)
    {
        this.name = nme;
    }
    public void SafeNameChange(string nme)
    {
        this.Invoke(new nameChanger(ChangeName), new object[] { nme });
    }

    private void button2_Click(object sender, EventArgs e)
    {
        Form3 f3 = new Form3(this);
        f3.Show();
    }

}

public partial class Form2 : Form
    {
        Form1 ff;
        public Form2(Form1 firstForm)
        {
            InitializeComponent();
            ff = firstForm;
        }

    private void button2_Click(object sender, EventArgs e)
    {
        ff.SafeNameChange("something different from the Form1");
        this.Close();
    }
}

Upvotes: 0

elyashiv
elyashiv

Reputation: 3691

do do something like that:

//form1:
public void add(int num)
{
  //add num to the list box.
}

now, form3 should get an instance of form1 in the constructor, and save it:

//in form3:
private form form1_i
public form3(form i_form1)
{
  .
  .
  .
 form1_i = i_form1;
}

and on button click in form3, call the fumction add in form1.

Upvotes: 0

Related Questions