将文本框字符串从一种形式发送到另一种形式的列表框
Sending a textbox string from one form to a listbox in another form
我已经在下面上传了我的代码。我面临的问题是试图将文本从 form2 中的 textBox1 和 textBox2 获取到 form1 中的列表框。当用户打开第二个表单时,他们应该将文本放入文本框并单击按钮 - 它应该关闭 form2 并将文本插入 form1 的 listBox
我尝试了很多事情,但我似乎做不到。我对 c# 编码也很陌生,刚刚开始使用 windows 表单,因此非常感谢任何帮助。
下面的表格 1 代码
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)
{
}
}
}
下面的表格 2 代码
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)
{
}
}
}
你的大体思路是对的,但你必须理解的是Mark在评论中所说的。在 Form2
中创建 Form1
并将数据添加到该表单将不起作用,因为一旦 Form2
关闭,Form1
的实例将不再存在。 IE。您当前正在做的事情看起来像这样的树层次结构:
Form1 (open Form2)
|
+-> Form2 (open and save to Form1)
|
+-> Form1 (contains saved data but you never see it)
原Form1
为父。它负责打开Form2
。用ShowDialog
,Form1
可能知道Form2
怎么关闭。因为它是父窗体,所以它可以访问 Form2
上的任何 public 属性。所以说 Form2
有以下 public 字段:
public TextBox textBox1;
public TextBox textBox2;
然后您可以像这样从中检索数据:
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();
}
我已经在下面上传了我的代码。我面临的问题是试图将文本从 form2 中的 textBox1 和 textBox2 获取到 form1 中的列表框。当用户打开第二个表单时,他们应该将文本放入文本框并单击按钮 - 它应该关闭 form2 并将文本插入 form1 的 listBox
我尝试了很多事情,但我似乎做不到。我对 c# 编码也很陌生,刚刚开始使用 windows 表单,因此非常感谢任何帮助。
下面的表格 1 代码
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)
{
}
}
}
下面的表格 2 代码
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)
{
}
}
}
你的大体思路是对的,但你必须理解的是Mark在评论中所说的。在 Form2
中创建 Form1
并将数据添加到该表单将不起作用,因为一旦 Form2
关闭,Form1
的实例将不再存在。 IE。您当前正在做的事情看起来像这样的树层次结构:
Form1 (open Form2) | +-> Form2 (open and save to Form1) | +-> Form1 (contains saved data but you never see it)
原Form1
为父。它负责打开Form2
。用ShowDialog
,Form1
可能知道Form2
怎么关闭。因为它是父窗体,所以它可以访问 Form2
上的任何 public 属性。所以说 Form2
有以下 public 字段:
public TextBox textBox1;
public TextBox textBox2;
然后您可以像这样从中检索数据:
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();
}