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

Dani AI

Generated

’s property + ShowDialog approach is the simplest, safest fix for a modal dialog: it keeps the dialog decoupled from the caller and returns exactly one value the caller can add to the ListBox. That pattern avoids making form controls public and is the usual WinForms way to hand data back from a dialog.

Two common pitfalls to watch for that caused the original failure: (1) ListBox.Items.Add only takes a single object — it does not have an overload that accepts a format string plus separate args — so build a single string (string.Format, interpolation or concatenation) and pass that. (2) Validation: don’t compare controls to null or mix up conditions. Use string.IsNullOrWhiteSpace on the Text properties and set DialogResult = DialogResult.OK (or equivalent) before closing so the caller knows the dialog succeeded.

If you want the caller to update immediately (or you prefer a non-modal flow), an event-based approach is clean and easy to implement. Example pattern (second form raises, first form subscribes):

// in the dialog form
public event Action<string> ItemAdded;

private void btnSave_Click(object sender, EventArgs e)
{
    // validate...
    var item = productNumberTextBox.Text + " for " + sizeTextBox.Text;
    ItemAdded?.Invoke(item);
    this.Close();
}
// in the main form, before showing the dialog
using (var dlg = new AddProductForm())
{
    dlg.ItemAdded += item => listBox1.Items.Add(item);
    dlg.ShowDialog();
}

Final tips: keep controls private, prefer properties/events over direct control access, use ErrorProvider or disabling the Save button for UX-friendly validation, and consider binding the ListBox to a collection if you’ll be adding/removing items frequently. — glad the modal property approach worked for you; the event option above is useful when you need immediate updates or non-modal behavior.

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.