双击C#中的datagridview后重复的表单 [英] Duplicated form after double click into the datagridview in C#

查看:87
本文介绍了双击C#中的datagridview后重复的表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我将尝试解释我希望我的应用程序做什么:



1)在主窗体中我有一个TextBox和一个DataGridView。我将在TextBox中插入我想要搜索的内容,然后单击F1打开将在另一个DataGridView中显示的第二个表单。



2)我将双击进入第二个表单DataGridView和该列值将从主窗体显示到TextBox中。



3)之后,TextBox被填充并根据该值而定将插入到主窗体DataGridView中的那个值详细。



在第二个窗体DataGridView中我有双击事件:



我尝试了什么:



So, I will try to explain what I want my application to do:

1) In the Main Form I have a TextBox and a DataGridView. I will insert into the TextBox what I want to search and then click F1 to open the Second Form which will display in another DataGridView.

2) I will double click into the Second Form DataGridView and that column value will be displayed into the TextBox from the Main Form.

3) After it, that TextBox is filled and depending on that value it will insert into the Main Form DataGridView that value detailed.

In the Second Form DataGridView I have the double click event with this:

What I have tried:

private void dataGridView1_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
    try
    {
        DataGridViewRow dr = dataGridView1.SelectedRows[0];
        this.Hide();

        frmPrincipal frm = new frmPrincipal();

        frm.Show();

        frm.txtCarga.Text = dr.Cells[0].Value.ToString();

        frm.txtCarga.Focus();
        frm.txtCarga.SelectAll();

    }
    catch (Exception ex)
    {
        MessageBox.Show("Erro\nDetalhes: " + ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}





这里的问题是如果我使用frm.Show();它将打开一个新的frmPrincipal表单,但我已经有一个。如果我评论frm.Show();代码不会被执行,但不会显示错误。基本上这个值不会显示在TextBox中。



我该怎么办?



The issue here is that if I use the frm.Show(); it will open a new frmPrincipal form, but I already have one. If I comment the frm.Show(); the code will not be executed, but no erros displays. Basically that value will not be displayed into the TextBox.

What should I do?

推荐答案

问题是你需要停下来思考你在做什么。

The problem is that you need to stop and think what you are doing.
frmPrincipal frm = new frmPrincipal();
frm.Show();

将始终创建表单的新实例并显示它 - 它不会,也不会,并且绝对不应该与现有实例进行通信而不是创建新实例整数变量应该连接到前一个并在设置新值时影响它们!

即使它确实如此,你也不应该尝试访问该表单上的控件,因为你不应该知道它们甚至存在!出于一个很好的理由,默认情况下它们是 private - 并且将它们公开以便你可以做这样的事情是一种非常糟糕的做法,因为它将两种形式锁定在一起,所以你不能改变一个而不考虑对所有其他形式的影响可能取决于它的内部。



你想要做的是回头到上一个实例 - 这并不困难:在两个表单之间传输信息,第2部分:孩子到父母 [ ^ ]将告诉你如何做 - 并在你完成它们时停止隐藏表格!改为关闭它们。

Will always create a new instance of a form and display it - it doesn't, can't, and very definitely shouldn't communicate with an existing instance any more than creating a new integer variable should "join" it to the previous one and affect them both when you set a new value!
And even if it did, you shouldn't be trying to access controls on that form because you shouldn't know they even exist! They are private by default for a very good reason - and making them public so you can do things like that is a very bad practice as it "locks" the two forms together, so you can't change one without considering the effects on all the other forms that might depend on it's internals.

What you want to do is "talk back" to the previous instance - which isn't difficult: Transferring information between two forms, Part 2: Child to Parent[^] will show you how to do it - and stop hiding forms when you are finished with them! Close them instead.


这篇关于双击C#中的datagridview后重复的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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