2013-02-26 170 views
-4

我的项目中有两种形式。在form1中,我有一个dataGridView,并在form2中有4个TextBoxes。我想要从一个DataGridview中使用CellMouseClick事件的变量中获得一个值,然后将它传递给Form2中的一个TextBox将数据从一个表单传递到另一个表单

我试过这个。

Form1上#它给我一个错误

public form(int id) 
{ 
    int x; 
    x = dataGridView1.CurrentRow.Cells[0].Value.ToString(); 
} 

什么亚姆想在窗口2

回答

7

做一个constructor可以constructconstruction给定的前提条件类型。

如果这意味着一个整数,那么就这样吧:

public MyForm(int id) { 
    SomeIdProperty = id; 
} 

代替var form = new MyForm();,做到:

var form = new MyForm(idOfTheRelevantThing); 

然后表现出来。

+0

,我该如何称呼它从另一种形式 – 2013-02-26 22:25:32

+0

比它工作完美 – 2013-02-27 02:21:18

2

如果从Form1显示Form2,则可以使用构造函数传递该值。事情是这样的:

class Form2 { 
    public string Value { get; set; } 
    public Form2(string value) { 
     Value = value; 
    } 

    public void Form2_Load() { 
     textBox1.Text = Value; 
    } 
} 

,并做到这一点(内Form1.cs):

Form2 f = new Form2("the value here"); 
f.ShowDialog(); //or f.Show(); 
4

Form1中

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 
    private void button1_Click(object sender, EventArgs e) 
    { 
     var frm2 = new Form2(dataGridView1.Rows[0].Cells[0].Value.ToString()); 
     frm2.Show(); 
    } 
} 

窗体2

public partial class Form2 : Form 
{ 
    public Form2(string s) 
    { 
     InitializeComponent(); 
     textBox1.Text = s; 
    } 
} 
相关问题