从MDI子窗体调用toolstripbutton [英] Calling toolstripbutton from MDI child form

查看:81
本文介绍了从MDI子窗体调用toolstripbutton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

我在C#中使用MDI父表单。在MDI父表单上有一个工具条。在工具条上,有button1和button2。在按钮1上单击打开form1并更改button1的背面颜色。并且button2单击打开form2并且button2的背面颜色发生变化。那没关系。但是在form1上有一个datagridview,当我在datagridview中双击一行时,我想关闭form1并打开form2并改回tooltrip button2的颜色。因为我想在form2的gridview中显示form1的datagridview中一行的相关详细数据。我怎么试试呢。请帮帮我。谢谢大家。

Dear All,
I am using MDI parent form in C#.There is a toolstrip on that MDI parent form.On toolstrip, there are button1 and button2. On button1 click open form1 and button1's back color is changed. And button2 click open form2 and button2's back color is change. That's OK.But there is a datagridview on form1, when I double click one row in datagridview, I want to close form1 and open form2 and change back color of toolstrip button2. Because I want to show related detail data of one row in datagridview of form1 in form2's gridview. How can I try it.Please help me. Thanks you all.

推荐答案

首先,您需要在两个MdiChild表单上公开DataGridView控件,以便MdiParent表单可以访问它们:所以第一个MdiChildForm的代码可能看起来像这样:
First you need to expose the DataGridView Control on both MdiChild Forms so the MdiParent Form can access them: so the first MdiChildForm's code might look like this:
// we expose the DataGridView by creating a public Property
public DataGridView f1DGView { get; set; }

private void MdiChildForm1_Load(object sender, EventArgs e)
{
    f1DGView = dataGridView1;
}

第二个MdiChildForm的代码是相同的,除了DataGridView名称。



然后,在MdiParentForm中:

The second MdiChildForm's code will be identical, except for the DataGridView name.

Then, in the MdiParentForm:

// to keep track of the current Row
// when the DataGridView on either MdiCHildForm gets a double-click
int currentDGVRow;

// create instances of the MdiChildForms
private MdiChildForm1 = new MdiChildForm1();
private MdiChildForm2 = new MdiChildForm2();

private void MdiParent_Load(object sender, EventArgs e)
{
    MdiChildForm1.MdiParent = this;
    MdiChildForm2.MdiParent = this;
    //
    // both MdiChild Forms must be shown once to allow access to
    // their internal DataGridView Control
    MdiChildForm1.Show();
    MdiChildForm2.Show();
    MdiChildForm2.Hide();
    //
    // handle the CellDoubleClick Event on both ChildMdiForms
    MdiChildForm1.f1DGView.CellDoubleClick += f1DGView_CellDoubleClick;
    MdiChildForm2.f2DGView.CellDoubleClick += f2DGView_CellDoubleClick;
    //
}

private void f1DGView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
    // get the row where the double-click occurred
    currentDGVRow = e.RowIndex;

    // set the ToolStrip buttons background color
    button1.BackColor = Color.Ivory;
    button2.BackColor = Color.Snow;

    // hide the current MdiChildForm and show the other MdiChildForm
    MdiChildForm1.Hide();
    MdiChildForm2.Show();

    // select the same row in the other MdiChildForm which is now visible
    MdiChildForm2.f2DGView.Rows[currentDGVRow].Selected = true;
}

private void f2DGView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
    currentDGVRow = e.RowIndex;

    button1.BackColor = Color.Snow;
    button2.BackColor = Color.Ivory;

    MdiChildForm2.Hide();
    MdiChildForm1.Show();

    MdiChildForm1.f1DGView.Rows[currentDGVRow].Selected = true;
}


这篇关于从MDI子窗体调用toolstripbutton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆