从一种形式传递价值,另一个(C#) [英] Passing Value from One Form to Another (C#)

查看:136
本文介绍了从一种形式传递价值,另一个(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的节目搜索表单。当用户双击搜索表单上的单元格(在DGV),我希望程序关闭的形式,并跳转到该项目的主要形式。



我通过标识具有唯一ID的每个项目这样做。



我试图传递行id的值的其他形式。问题在于,它说,我是路过每次值为零。但是,当我插入搜索表单上的一些消息框,它说,该整数'ID'已成功分配给变量在主窗体上:公众诠释CellDoubleClickValue {搞定;组; }



下面是我的代码:



搜索表单:



 私人诠释身份证; 

私人无效searchdgv_CellDoubleClick(对象发件人,DataGridViewCellEventArgs E)
{
this.rowIndex1 = e.RowIndex;
this.id = Convert.ToInt32(this.searchdgv.Rows [this.rowIndex1] .Cells [身份证]值。);
invmain INV =新invmain();
inv.CellDoubleClickValue = this.id;
this.DialogResult = DialogResult.OK;
this.Close();
//MessageBox.Show(inv.CellDoubleClickValue.ToString());
//以上,就说明它得到了成功分配。
}



主要形式:

 公众诠释CellDoubleClickValue {搞定;组; } 

私人无效searchToolStripMenuItem_Click(对象发件人,EventArgs五)
{
搜索Form1中=新的搜索();
form1.ShowDialog();

如果(form1.DialogResult == DialogResult.OK)
{
MessageBox.Show(CellDoubleClickValue.ToString());
} //这表明值为:0


解决方案

我建议你做如下:



搜索表单:

 公众诠释SelectedId {搞定;组; } 

私人无效searchdgv_CellDoubleClick(对象发件人,DataGridViewCellEventArgs E)
{
this.rowIndex1 = e.RowIndex;
this.SelectedId = Convert.ToInt32(this.searchdgv.Rows [this.rowIndex1] .Cells [身份证]值。);
this.DialogResult = DialogResult.OK;
this.Close();
}



主要形式有:

 私人无效searchToolStripMenuItem_Click(对象发件人,EventArgs五)
{
搜索Form1中=新的搜索();
form1.ShowDialog();

如果(form1.DialogResult == DialogResult.OK)
{
INT selectedId = form1.SelectedId;
//什么就做什么用selectedId ...
}


I have a search form in my program. When the user double-clicks a cell (of the dgv) on the search form, I want the program to close that form and jump to the item on the main form.

I'm doing this by identifying each item with a unique id.

I'm trying to pass the value of the rows id to the other form. The problem is that, it says that I'm passing the value zero each time. But when I insert some message boxes on the search form, it says that the integer 'id' has successfully been assigned to the variable on the main form: public int CellDoubleClickValue { get; set; }

Here is my code:

Search Form:

    private int id;

    private void searchdgv_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
        this.rowIndex1 = e.RowIndex;
        this.id = Convert.ToInt32(this.searchdgv.Rows[this.rowIndex1].Cells["id"].Value);
        invmain inv = new invmain();
        inv.CellDoubleClickValue = this.id;
        this.DialogResult = DialogResult.OK;
        this.Close();
        //MessageBox.Show(inv.CellDoubleClickValue.ToString()); 
        //Above, it shows it got assigned successfully.
    }

Main Form:

    public int CellDoubleClickValue { get; set; }

    private void searchToolStripMenuItem_Click(object sender, EventArgs e)
        {
        search form1 = new search();
        form1.ShowDialog();

        if (form1.DialogResult == DialogResult.OK)
        {
            MessageBox.Show(CellDoubleClickValue.ToString());
        }//Here it shows that the value is: 0

解决方案

I suggest you do as follows:

Search Form:

public int SelectedId { get; set; }

private void searchdgv_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
    this.rowIndex1 = e.RowIndex;
    this.SelectedId = Convert.ToInt32(this.searchdgv.Rows[this.rowIndex1].Cells["id"].Value);
    this.DialogResult = DialogResult.OK;
    this.Close();
}

Main form:

private void searchToolStripMenuItem_Click(object sender, EventArgs e)
{
    search form1 = new search();
    form1.ShowDialog();

    if (form1.DialogResult == DialogResult.OK)
    {
        int selectedId = form1.SelectedId;
        // Do whatever with selectedId...
    }

这篇关于从一种形式传递价值,另一个(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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