【问题标题】:Show the value in textbox in the parent form, of a cell selected of the gridview in the child form在父窗体的文本框中显示子窗体中网格视图选定单元格的值
【发布时间】:2021-03-03 13:27:07
【问题描述】:

这是我的代码: 在父窗体中:

public void tokenform_Load(object sender, EventArgs e)
    {
       tbvarchecker.Text = HelpForm.code;
    }
   private void TextBox1_KeyDown(object sender, KeyEventArgs e)
    {
        HelpForm help = new HelpForm();
        if (e.KeyCode == Keys.F2)
        {               
            help.Show();               
        }
    }
    public void setvalues(string cd)
    {
        tbvarchecker.Text = cd;
        label10.Text = cd;
    }

  Child Form code:
     private void dgv(object sender, DataGridViewCellMouseEventArgs e)
    {
        DataGridViewRow row = dgvshowallfields.Rows[e.RowIndex];

        code = row.Cells[0].Value.ToString();
        code1 = row.Cells[0].Value.ToString();
        this.Close();
    }

    private void HelpForm_FormClosed(object sender, FormClosedEventArgs e)
    {
        tokenform tkf = new tokenform();
       tkf.setvalues(code);
        tkf.setvalues(code1);
        tkf.tokenform_Load(sender,e);

    }

    //private void HelpForm_FormClosing(object sender, FormClosingEventArgs e)
    //{
    //    tkf.setvalues(code);
    //}

此代码被声明为公共静态字符串代码,而 code1 只是字符串。 我还检查了断点,并且该值达到了父表单函数设置值中的函数,但仍然无法在文本框中显示它。 各位大神可以帮帮我吗……??

【问题讨论】:

    标签: c# visual-studio winforms textbox


    【解决方案1】:

    首先,将父表单传递给您的Show() 调用。

    变化:

    help.Show();
    

    收件人:

    help.Show(this);
    

    然后,在子表单中,您可以将.Owner 属性转换为您的父表单类型并调用其方法:

    private void HelpForm_FormClosed(object sender, FormClosedEventArgs e)
    {
        tokenform tkf = this.Owner as tokenform;
        if (tkf != null) 
        {
            tkf.setvalues(code);
            tkf.setvalues(code1);
            tkf.tokenform_Load(sender,e);
        }
    }
    

    【讨论】:

    • 非常感谢,兄弟,这有帮助,但你能解释一下我做错了什么吗?为什么我不能早点显示这个值......??
    • 在您的 FormClosed() 事件中,您正在创建一个新的 tokenform 实例。您尝试使用的实例根本不可见,并且与您在屏幕上看到的实例不同。
    猜你喜欢
    • 1970-01-01
    • 2018-10-16
    • 1970-01-01
    • 2012-11-20
    • 2010-10-31
    • 2015-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多