Hello,

I have started to create an app that through some other things needs to take the text from two text boxes on a form and then post the text on list box that's located on another form.
Following is the code with which I've tried doing this... and of course... it failed...

private void btnSave_Click(object sender, EventArgs e)
        {
            if (sizeTextBox.Text == "" && productNumberTextBox.Text == "")
            {
                MessageBox.Show("no input all");
            }
            else if (sizeTextBox.Text == "" && productNumberTextBox != null)
            {
                MessageBox.Show("no input 1");
            }
            else if (productNumberTextBox.Text == "" && sizeTextBox != null)
            {
                MessageBox.Show("no input 2");
            }
            else
            {
                formAddProduct.sizeListBox.Items.Add("{0} for {1}", productNumberTextBox.Text, sizeTextBox.Text);
                this.Close();
            }
        }

If you have any idea on how I could this, I'd be very grateful.

thank you

Recommended Answers

All 2 Replies

If the second form is open using ShowDialog (which it probably should) then you can add a property on the second form to hold the new listbox value.
Then when the form is closed retrieve the value from the property and update the list box on the first form.

Add property to second form

private string newListItem;
        public string NewListItem
        {
            get { return newListItem; }
        }

Modify btnSave_Click to set newListItem

private void btnSave_Click(object sender, EventArgs e)
        {
            if (sizeTextBox.Text == string.Empty && productNumberTextBox.Text == string.Empty)
            {
                MessageBox.Show("no input all");
            }
            else if (sizeTextBox.Text == string.Empty) // productNumberTextBox must have value otherwise not get this far
            {
                MessageBox.Show("no input 1");
            }
            else // sizeTextBox must have data otherwise not get this far
            {
                MessageBox.Show("no input 2");
            }
            else
            {
                // validation OK set new list item
                newListItem = string.Format("{0} for {1}", productNumberTextBox.Text, sizeTextBox.Text);
                // close dialog with OK result
                this.DialogResult = DialogResult.OK;
            }
        }

In first form, open second form as a dialog

using (Form1 dialog = new Form1())
            {
                if(dialog.ShowDialog()== DialogResult.OK)
                {
                    // result OK - get new item
                    this.listBox1.Items.Add(dialog.NewListItem);
                }
            }

thank you!
it worked great!

yes, the second form was opened using ShowDialog :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.