【问题标题】:Invoking a button in a form from another form从另一个表单调用一个表单中的按钮
【发布时间】:2016-03-20 14:33:50
【问题描述】:

我有两个表单,Form1 和 Form 2。我需要从 Form2 访问 Form1 中的一个按钮,以便刷新 Form1 中的 datagridview

这是Form1:

private void BookLoad()
    {
        DataSet d = new DataSet();
        SqlCommand com = new SqlCommand("select * from Books  ", cn);
        SqlDataAdapter da = new SqlDataAdapter(com);
        da.Fill(d, "Books");
        dataGridView2.DataSource = d;
        dataGridView2.DataMember = "Books";

        DataSet ds = new DataSet();
        SqlCommand cmd = new SqlCommand("select * from Issue where StudentNumber LIKE @number", cn);
        cmd.Parameters.AddWithValue("@number", textBox1.Text);
        da = new SqlDataAdapter(cmd);
        da.Fill(ds, "Issue");
        dataGridView1.DataSource = ds;
        dataGridView1.DataMember = "Issue";

        DataSet dss = new DataSet();
        SqlCommand cmdd = new SqlCommand("select Books_Borrowed,Date_Borrowed,Return_Date,Quantity from Issue where StudentNumber = @number", cn);
        cmdd.Parameters.AddWithValue("@number", textBox1.Text);
        da = new SqlDataAdapter(cmdd);
        da.Fill(dss, "Issue");
        dataGridView3.DataSource = dss;
        dataGridView3.DataMember = "Issue";

    }


   private void button2_Click(object sender, EventArgs e)
    {
      Form2 form = new Form2();
      form.Show();
    }


    private void button10_Click(object sender, EventArgs e)
    {
       BookLoad();
       MessageBox.Show();
    }

我需要从 Form2 访问 button10

【问题讨论】:

  • 您想使用Form2 中的什么操作来触发from1 中的button10
  • 当我单击表单 2 中的按钮时,它会做一些事情并将某些内容插入数据库,然后隐藏自己。我需要访问 button10 来刷新表格 1

标签: c# winforms


【解决方案1】:

首先,在使用Hide()Show() 时要小心谨慎,当表单不可见时,可能会有一些事情无法按预期工作。

实现该功能的理想解决方案,在Form2 中定义一个事件并在Form1 中订阅该事件

按照指定在Form2 中声明一个委托(将此逻辑保留在Form2 中)

public delegate void Listen(object sender, EventArgs args);

// Expose the event off your component
public event Listen ListenChange;

在隐藏表单之前,将此逻辑保留在Form2 中的Button_Click 事件中。

// And to raise it
if (ListenChange != null)
{
   ListenChange(this, new EventArgs());
}

现在在Form1 订阅此事件并引发更改。

Form2 form = new Form2();
form.ListenChange += (s, ev) => button10.PerformClick();
form.Show();

【讨论】:

    【解决方案2】:

    似乎Form2 是某种编辑对话框。 真的不应该访问Form1上的控件。

    更简单的事情是使用ShowDialog()方法将Form2作为模态打开(使用Show方法可以在多次单击button2时打开多个Form2

    private void button2_Click(object sender, EventArgs e)
    {
      Form2 form = new Form2();
      DialogResult r = form.ShowDialog();
      if (r == DialogResult.OK)
      {
          BookLoad();
          MessageBox.Show();
      }
    }
    

    【讨论】:

      【解决方案3】:

      这不是一个好方法,但如果您想这样做,请在 Form1 中添加一个静态函数:

      public static void btnDown(){
          SomeButton.Invoke();
      }
      

      现在像这样从 Form2 调用这个函数

      Form1.btnDown();
      

      【讨论】:

        猜你喜欢
        • 2015-03-25
        • 2012-07-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-25
        • 1970-01-01
        • 1970-01-01
        • 2017-08-04
        相关资源
        最近更新 更多