Sending a textbox string from one form to a listbo

2019-08-01 07:54发布

问题:

I have uploaded my code below. The problem I am facing is trying to get the text from the textBox1 & textBox2 in form2 into the listBox in form1. When the user opens the 2nd form, they should place text into the textboxes and upon clicking the button - it should close the form2 and insert the text into form1's listBox

I have tried multiple things but I can't seem to do it. I am also quite new to c# coding and have just started on windows forms so any help would be much appreciated.

FORM 1 CODE BELOW

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ToDoList
{
    public partial class Form1 : Form
    {
        List<string> _items = new List<string>();
        public Form1()
        {
            InitializeComponent();
            Form2 form2 = new Form2();
            _items.Add("TITLE\t\t\tDESCRIPTION\t\t\t\t\t\tPRIORITY\tDUE DATE");
            listBox1.DataSource = _items;


        }

        private void filesToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            form2.Show();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {



        }

        public void button2_Click(object sender, EventArgs e)
        {


        }

        public void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        public void listView1_SelectedIndexChanged_1(object sender, EventArgs e)
        {

        }

        private void listBox1_SelectedIndexChanged_1(object sender, EventArgs e)
        {

        }
    }
}

FORM 2 CODE BELOW

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ToDoList
{
    public partial class Form2 : Form
    {
        List<string> _items = new List<string>();
        public Form2()
        {
            InitializeComponent();
        }

        public void button2_Click(object sender, EventArgs e, string tbtext)
        {
            //Form1 form1 = new Form1();
            //form1.listBox1.Items.Add(tbtext);
            Form1 form1 = new Form1();
            _items.Add(textBox1.Text);
            _items.Add(textBox2.Text);
            _items.Add(comboBox1.Text);

            form1.listBox1.DataSource = null;
            form1.listBox1.DataSource = _items;

        }

        public void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public void textBox1_TextChanged(object sender, EventArgs e)
        {


        }

        private void button2_Click(object sender, EventArgs e)
        {






            //IF THE USER DOES NOT ENTER items IN THE SPACES THESE IF STATEMENTS BRING A MESSAGEBOX 
            if (string.IsNullOrWhiteSpace(this.textBox2.Text) && string.IsNullOrWhiteSpace(this.textBox1.Text))
            {
                MessageBox.Show("Please Fill The Entries");

            }

            else
            {
                if (string.IsNullOrWhiteSpace(this.textBox1.Text))
                {
                    MessageBox.Show("Please Enter A Task Name");
                }

                if (string.IsNullOrWhiteSpace(this.textBox2.Text))
                {
                    MessageBox.Show("Please Enter A Description");
                }
            }
        }

        public void textBox1_TextChanged_1(object sender, EventArgs e)
        {

        }

        public void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        public void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}

回答1:

Your general idea is correct, but what you must understand is what Mark stated in comments. Creating Form1 in Form2 and adding data to that form won't work, because once Form2 closes, that instance of Form1 no longer exists. I.E. What you are currently doing looks like this as a tree hierarchy:

Form1 (open Form2)
  |
  +-> Form2 (open and save to Form1)
        |
        +-> Form1 (contains saved data but you never see it)

The original Form1 is the parent. It is responsible for opening Form2. Using ShowDialog, Form1 may know how Form2 closed. And because it is the parent form, it can access any public properties on Form2. So say Form2 has the following public fields:

public TextBox textBox1;
public TextBox textBox2;

You could then retrieve data from these like so:

public void SomeMethodInForm1()
{
   Form2 form2 = new Form2();

   // Show form2 as a modal dialog and determine if DialogResult = OK. 
   if (form2.ShowDialog(this) == DialogResult.OK)
   {
      // Read the contents of form2's TextBox. 
      this._items.Add(form2.textBox1.Text);
      this._items.Add(form2.textBox2.Text);

      this.listBox1.DataSource = null;
      this.listBox1.DataSource = _items;
   }

   form2.Dispose();
}