通过textbox.Text从另一个表单(C#)添加项目到列表视图

时间:2015-03-26 19:08:11

标签: c# listview listviewitem

可悲的是,由于我是新人,所以我无法包含图片......所以请在这里与我合作。

这是我尝试使用的代码,用于添加其他表单中的项目' 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");
        }
    }

问题在于:没有错误,也没有抛出异常,但是没有任何内容添加到列表视图中。调试代码时,它显示信息已存储但未传递到列表视图中......为什么? (提前致谢)

1 个答案:

答案 0 :(得分:0)

这是因为您在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);