【问题标题】:searching in table by "person Id " in form1 and display information on labels of form2通过form1中的“person Id”在表格中搜索并在form2的标签上显示信息
【发布时间】:2020-12-05 21:21:21
【问题描述】:

我有两种表格:Form1 和 Form2。 Form1 包含一个文本框和一个搜索按钮。当我单击该按钮时,必须加载表格 2,然后在表格 1 中搜索到的那个人的姓名和家庭将显示在标签上。

通过我的代码,人的信息,在form1上显示,但不在form2上。

表格1:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        tbl1BindingSource.SuspendBinding();

    }
    private void Form1_Load(object sender, EventArgs e)
    {
        // TODO: This line of code loads data into the 'region1003660DataSet.Tbl1' table. You can move, or remove it, as needed.
        this.tbl1TableAdapter.Fill(this.region1003660DataSet.Tbl1);

    }

    private void btnSearch_Click(object sender, EventArgs e)
    {
        MessageBox.Show(tbl1BindingSource.ToString());
        int find = tbl1BindingSource.Find("id", txtSearch.Text);
        if (find >= 0)
        {
            tbl1BindingSource.ResumeBinding();
            tbl1BindingSource.Position = find;
        }
        else tbl1BindingSource.SuspendBinding();
        Form2 f2 = new Form2(find);
        f2.Show();
      
    }
}

表格2:

 public partial class Form2 : Form
{
    public Form2(int f)
    {
        InitializeComponent();
        tbl1BindingSource.SuspendBinding();
        tbl1BindingSource.Position = f;
    }
    private void Form2_Load(object sender, EventArgs e)
    {
        // TODO: This line of code loads data into the 'region1003660DataSet.Tbl1' table. You can move, or remove it, as needed.
        this.tbl1TableAdapter.Fill(this.region1003660DataSet.Tbl1);
  

    }
}

【问题讨论】:

    标签: c# sql-server winforms modal-dialog


    【解决方案1】:

    如果您还没有尝试过使用静态变量,那么您应该这样做。当我想以另一种形式显示成员 ID 以打印在打印文档上时,我也这样做了,它就像一个魅力!

    【讨论】:

      【解决方案2】:

      您的 Form2 会发生什么,btnSearch_Click 已完成?有人还在引用它吗?垃圾收集器可以收集Form2吗?

      在我看来,操作员在文本框中键入了一些文本。完成后,他按下一个按钮。 Form 读取 textBox 中的文本,并尝试在 tbl1BindingSource 中找到它。 找到后会弹出一个新表单,显示找到的信息。

      对话框应该是模态的还是非模态的?换一种说法, 当 Form2 显示时,操作员是否仍然可以使用 Form1 进行操作?例如再次按下相同的按钮?还是应该先关闭 Form2,然后再重新编辑文本框?

      后者(模态)更容易:当 Form2 显示时,您无法访问 Form1

      我不知道你对 SuspendBinding / ResumeBinding 和你的表做了什么。我猜他们与你的问题无关。尽量避免在表单之间共享数据。

      Form2 有两个标签和两个文本框。使用视觉工作室设计器来创建它们。代码类似这样:

      public Form2 : Form
      {
          public Form2()
          {
              InitializeComponents(); // also creates Labels and TextBoxes
                                      // if not: create them here
          }
      
          public string Name
          {
              get => this.TextBoxName.Text;
              set => this.TextBoxName.Txt = value;
          }
          public string Family
          {
              get => this.TextBoxFamily.Text;
              set => this.TextBoxFamily.Txt = value;
          }
      }
      

      作为替代方案:一个属性人:

      public Person Person
      {
          get => new Person {Name = this.TextBoxName.Text, Family = this.TextBoxFamily.Text};
          set
          {
               this.TextBoxName.Text = value.Name;
               this.TextBoxFamily.Text = value.Family;
          }
      

      在表格1中:

      private void ShowForm2(Person person)
      {
          using (var form2 = new Form2())
          {
              form2.Name = person.Name;
              form2.Family = person.Family;
              var dlgResult = form2.ShowDialog(this);
      
              // if you are interested in the result of showing the dialog:
              if (dlgResult == DialogResult.OK)
              {
                  Person person = form2.Person;
                  this.ProcessPerson(person);
              }
          }
      

      如果您希望在显示 Form2 时能够访问 Form1,请将其显示为无模式对话框。确保在关闭 Form1 之前关闭 Form2。

      private Form2 form2 = new Form2();
      
      private void ShowForm2(Person person)
      {
           form2.Name = person.Name;
           form2.Family = person.Family;
           form2.Show(this);
      }
      

      如果 form1 应该对 Form2 中的操作做出反应,请将事件添加到 form2。

      private void btnSearch_Click(object sender, EventArgs e)
      {
          // read the text box,
          // use a separate method to find the person
          // show Form2
          string text = txtSearch.Text;
          Person foundPerson = this.FindPerson(text);
          if (foundPerson != null)
          {
              this.ShowForm2(person);
          }
      

      确保在关闭 Form1 之前关闭无模式 Form2:

      private void OnForm1Closing(object sender, ...)
      {
          // before closing form1, close form2
          if (form2 != null) form2.Close();
      }
      

      正式的你应该等到form2已经关闭,然后再继续:取消这个关闭,订阅form2的Closed事件,当这个事件引发时,dispose form2,from2 == null,然后重新开始关闭Form1。第二次form2会为null,所以可以继续关闭。

      你看到了吗,因为我将 Form2 的显示与获取人员信息和按钮按下分开,所以按钮按下动作不再关心 Form2 是模态还是非模态,它不再关心在哪里转换将搜索文本转换为 Person:它可以是绑定源,例如您的情况,也可以是数据库、JSon 文件,甚至是 Internet:FindPerson 可以在不按按钮的情况下进行单元测试;您可以更改获取信息的位置。您可以重复使用 FindPerson 和 ShowForm2。

      简而言之:不要在一种方法中做太多事情!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-08-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多