【问题标题】:Trying to bind form2 textbox to form1 listview试图将 form2 文本框绑定到 form1 列表视图
【发布时间】:2012-05-28 15:35:52
【问题描述】:

我有带有ListView 的winform1 和添加按钮。当我按下添加按钮时,它会打开新的 winform2,其中包含 2 个文本框、姓名和姓氏,以及保存按钮。 现在我想要的是在我按下保存时将这些值添加到 listView 中。我的代码没有错误,但我的列表框不会更新。

这是我的列表类的代码:

public class Person
{
    public string Name { get; set; }

    public string Surname { get; set; }
}

这里是form2代码:

public partial class add : Form
{
    public add()
    {
        InitializeComponent();
    }

    Form1 f1 = new Form1();
    List<Person> People = new List<Person>();

    private void button1_Click(object sender, EventArgs e)
    {
        Person p = new Person();
        p.Name = textBox1.Text;
        p.Surname = textBox2.Text;
        People.Add(p);
        f2.listView1.Items.Add(p.Name + " " + p.Surname);
    }
}

现在问题是调试没有显示任何错误。我的 listbox1 没有更新,我不知道我做错了什么。

尝试使用f2.ShowDialog();,然后它会在列表视图中显示添加的项目,但它会再次打开 form1,当我添加新数据时,以前的数据将丢失。谁能帮我解决这个问题?

【问题讨论】:

  • 您的代码无法编译,因为在您的 button1_Click 代码中引用了成员 f2 - 您确定您的代码没有输入错误吗?这也不是严格的“绑定”,因为您没有使用任何绑定。您只是将项目直接添加到列表视图中。我敢打赌,您将在 Form2 中创建一个新的 Form1,然后将其添加到 Form1 列表视图中。我假设首先创建 Form1,它会打开 Form2 - 您应该将 Form1 传递给 Form2 或在 Form1 中编写您的列表框添加代码。人员列表的意义何在?这是私人的 - 这有点令人困惑!

标签: c# .net winforms data-binding


【解决方案1】:

我会确保add 表单中的Person 可供调用方Form1 使用,这样当用户单击“确定”按钮时,您可以将该信息添加到列表视图中。

为简单起见,我已更改我的版本以添加单个项目。我让你来决定如何为List&lt;Person&gt; 做同样的事情。

在代码中,它看起来像这样:

public partial class add : Form
{
    // notice that we don't need a List, just a single item
    public Person person = new Person();

    public add()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.person.Name = this.nameTextBox.Text;
        this.person.Surname = this.surnameTextBox.Text;

        // the listView is only be updated if the changes were accepted
        // setting the result to OK will also close the dialog
        this.DialogResult = DialogResult.OK;
    }
}

还有Form1的代码:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void addButton_Click(object sender, EventArgs e)
    {
        var add = new add();

        if (add.ShowDialog() == DialogResult.OK)
        {
            this.listView1.Items.Add(add.person.Name +
                                     " " + add.person.Surname);
        }
    }
}

【讨论】:

  • 谢谢,这正是我想要的:)
  • @user1396678 如果此代码对您有用,请点赞以感谢 Gustavo 的回答......然后每个人都乐于回答所有问题 ;)
【解决方案2】:

为你的文本框试试这个:

public string textbox1_text
    {
        get { return textBox1.Text; }
        set { textBox1.Text = value; }
    }

使用这些代码加载新表单:

Form2 f2 = new Form2();
        f2.Owner = this;
        f2.Show();

【讨论】:

    【解决方案3】:

    这段代码似乎是错误的:

    f2.listView1.Items.Add(p.Name + " " + p.Surname); //Form2
    

    也许你的意思是:

    f1.Show();
    f1.listView1.Items.Add(p.Name + " " + p.Surname); //Form1
    //this.Close(); <-- if you want to close the form after show f1
    

    ?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-19
      • 2021-01-28
      • 2014-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多