通过 textbox.Text 从另一个表单 (C#) 添加项目到列表视图
Adding an Item to listview by textbox.Text from another form (C#)
遗憾的是,我是新手,无法包含图片...所以请在这里与我合作。
这是我尝试使用的代码,用于将另一个表单的 'textbox.Text' 中的项目添加到我的主表单的列表视图中。
public partial class AddingClient : Form //(Form 2)
{
private Form1 UseForm1; // The Object to access Form1's listview
public AddingClient()
{
UseForm1 = new Form1();
InitializeComponent();
}
public void Accept_Click(object sender, EventArgs e) // When all textbox's are filled press accept to add to listview.
{
try
{
// Check to see if there are any blank texts, we can't be having those.
if (name.Text == string.Empty)
throw new System.ArgumentException("You must fill all blanks.", "Name");
if (phoneN.Text == string.Empty)
throw new System.ArgumentException("You must fill all blanks.", "Phone Number");
if (address.Text == string.Empty)
throw new System.ArgumentException("You must fill all blanks.", "Address");
if (email.Text == string.Empty)
throw new System.ArgumentException("You must fill all blanks.", "Email");
// Use the info given via textbox's and add them to items/subitems
ListViewItem lvi = new ListViewItem(name.Text);
lvi.SubItems.Add(phoneN.Text);
lvi.SubItems.Add(address.Text);
lvi.SubItems.Add(email.Text);
// Add the items to the list view.
UseForm1.listView1.Items.Add(lvi); // This is the code I believe to be the problem but found no solutions.
// If no error, success.
MessageBox.Show("Successfully Added Client", "Success");
Close();
}
catch(Exception ex)
{
//If error show the error
MessageBox.Show(ex.Message,"Error");
}
}
问题是:没有错误,也没有抛出异常,但是列表视图中没有添加任何内容。调试代码的时候,显示信息被存储了,但是没有传到listview中。。。为什么? (提前致谢)
发生这种情况是因为您正在 AddingClient 的构造函数中创建 Form1 的新实例。
假设您最初将 Form1 作为主窗体加载,然后单击一个按钮打开 AddingClient。现在在 AddingClient 中,您正在创建 Form1 的新实例。您现在有两个 Form1 实例,并且要插入到屏幕上不可见的第二个 Form1 实例。
您需要做的是,通过函数将 Form1 实例传递给 AddingClient,这可以通过传递 this 来实现,或者您从 Form1 中的 AddingClient 检索值主实例,一旦您关闭了 AddingClient 表单。
public partial class AddingClient : Form //(Form 2)
{
private Form1 UseForm1; // The Object to access Form1's listview
public AddingClient()
{
InitializeComponent();
}
public void setForm1(Form1 form)
{
UseForm1 = form;
}
public void Accept_Click(object sender, EventArgs e) // When all textbox's are filled press accept to add to listview.
{
try
{
// Check to see if there are any blank texts, we can't be having those.
if (name.Text == string.Empty)
throw new System.ArgumentException("You must fill all blanks.", "Name");
if (phoneN.Text == string.Empty)
throw new System.ArgumentException("You must fill all blanks.", "Phone Number");
if (address.Text == string.Empty)
throw new System.ArgumentException("You must fill all blanks.", "Address");
if (email.Text == string.Empty)
throw new System.ArgumentException("You must fill all blanks.", "Email");
// Use the info given via textbox's and add them to items/subitems
ListViewItem lvi = new ListViewItem(name.Text);
lvi.SubItems.Add(phoneN.Text);
lvi.SubItems.Add(address.Text);
lvi.SubItems.Add(email.Text);
// Add the items to the list view.
UseForm1.listView1.Items.Add(lvi); // This is the code I believe to be the problem but found no solutions.
// If no error, success.
MessageBox.Show("Successfully Added Client", "Success");
Close();
}
catch(Exception ex)
{
//If error show the error
MessageBox.Show(ex.Message,"Error");
}
}
}
然后在您的主窗体中调用 AddingClient 窗体的地方写下这个
AddingClient addingClientForm = new AddingClient();
addingClientForm.setForm1(this);
addingClientForm.ShowDialog(this);
遗憾的是,我是新手,无法包含图片...所以请在这里与我合作。
这是我尝试使用的代码,用于将另一个表单的 'textbox.Text' 中的项目添加到我的主表单的列表视图中。
public partial class AddingClient : Form //(Form 2)
{
private Form1 UseForm1; // The Object to access Form1's listview
public AddingClient()
{
UseForm1 = new Form1();
InitializeComponent();
}
public void Accept_Click(object sender, EventArgs e) // When all textbox's are filled press accept to add to listview.
{
try
{
// Check to see if there are any blank texts, we can't be having those.
if (name.Text == string.Empty)
throw new System.ArgumentException("You must fill all blanks.", "Name");
if (phoneN.Text == string.Empty)
throw new System.ArgumentException("You must fill all blanks.", "Phone Number");
if (address.Text == string.Empty)
throw new System.ArgumentException("You must fill all blanks.", "Address");
if (email.Text == string.Empty)
throw new System.ArgumentException("You must fill all blanks.", "Email");
// Use the info given via textbox's and add them to items/subitems
ListViewItem lvi = new ListViewItem(name.Text);
lvi.SubItems.Add(phoneN.Text);
lvi.SubItems.Add(address.Text);
lvi.SubItems.Add(email.Text);
// Add the items to the list view.
UseForm1.listView1.Items.Add(lvi); // This is the code I believe to be the problem but found no solutions.
// If no error, success.
MessageBox.Show("Successfully Added Client", "Success");
Close();
}
catch(Exception ex)
{
//If error show the error
MessageBox.Show(ex.Message,"Error");
}
}
问题是:没有错误,也没有抛出异常,但是列表视图中没有添加任何内容。调试代码的时候,显示信息被存储了,但是没有传到listview中。。。为什么? (提前致谢)
发生这种情况是因为您正在 AddingClient 的构造函数中创建 Form1 的新实例。
假设您最初将 Form1 作为主窗体加载,然后单击一个按钮打开 AddingClient。现在在 AddingClient 中,您正在创建 Form1 的新实例。您现在有两个 Form1 实例,并且要插入到屏幕上不可见的第二个 Form1 实例。
您需要做的是,通过函数将 Form1 实例传递给 AddingClient,这可以通过传递 this 来实现,或者您从 Form1 中的 AddingClient 检索值主实例,一旦您关闭了 AddingClient 表单。
public partial class AddingClient : Form //(Form 2)
{
private Form1 UseForm1; // The Object to access Form1's listview
public AddingClient()
{
InitializeComponent();
}
public void setForm1(Form1 form)
{
UseForm1 = form;
}
public void Accept_Click(object sender, EventArgs e) // When all textbox's are filled press accept to add to listview.
{
try
{
// Check to see if there are any blank texts, we can't be having those.
if (name.Text == string.Empty)
throw new System.ArgumentException("You must fill all blanks.", "Name");
if (phoneN.Text == string.Empty)
throw new System.ArgumentException("You must fill all blanks.", "Phone Number");
if (address.Text == string.Empty)
throw new System.ArgumentException("You must fill all blanks.", "Address");
if (email.Text == string.Empty)
throw new System.ArgumentException("You must fill all blanks.", "Email");
// Use the info given via textbox's and add them to items/subitems
ListViewItem lvi = new ListViewItem(name.Text);
lvi.SubItems.Add(phoneN.Text);
lvi.SubItems.Add(address.Text);
lvi.SubItems.Add(email.Text);
// Add the items to the list view.
UseForm1.listView1.Items.Add(lvi); // This is the code I believe to be the problem but found no solutions.
// If no error, success.
MessageBox.Show("Successfully Added Client", "Success");
Close();
}
catch(Exception ex)
{
//If error show the error
MessageBox.Show(ex.Message,"Error");
}
}
}
然后在您的主窗体中调用 AddingClient 窗体的地方写下这个
AddingClient addingClientForm = new AddingClient();
addingClientForm.setForm1(this);
addingClientForm.ShowDialog(this);