C#将数据从form2传递给开始form1 [英] C# passing data from form2 to opening form1
本文介绍了C#将数据从form2传递给开始form1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
伙计们帮助我解决这个问题
我有两个表单Form1包含四个ComboBox,Form2包含一个ComboBox和一个带三列的Datagridview。
当我在Form1中鼠标点击 One ComboBox 时,它会显示Form1并填充来自 ComboBox 的 ComboBox (Form1)并填写与 ComboBox (Form2)相关的 DataGridView 信息。因此,当我单击 DataGridView 中的一行(在Form2中)时,该行中的数据将传递给Form1中的其他三个ComboBox 。
当我在(Form1)中鼠标点击 ComboBox 时,它将数据传递到(Form2)中的ComboBox并在DataGridview中显示与(Form2)中的ComboBox相关的行,正确。但是,当我在一行上鼠标单击时,它不会将它从(Form2)中的DataGridView传递到(Form1)中的三个ComboBox。
请,我需要你的帮助。
我的尝试:
这是Form1。
显示Form2并将数据传递给ComboBox(Form1)
< pre> private void ComboBox1_MouseClick(对象发送者,MouseEventArgs e)
{
Form2 f2 = new Form2 (this.ComboBox.Text);
f2.MdiParent = this.MdiParent;
f2.Show();
}
此函数它将数据从DataGridView(Form2)返回到Form1
public Form1(字符串Data1,字符串Data2,字符串Data3)
{
InitializeComponent();
this.ComboBox1.Text = Data1;
this.ComboBox2.Text = Data2;
this.ComboBox3.Text = Data3
this.ActiveControl = this.ComboBox1;
}
============================== =========
在表格2中,这是代码:
从Form1获取数据
public Form2(string Data1)
{
InitializeComponent();
this.ComboBox.Text = Data1;
}
这是我向Form1发送数据的时候
private void DataGridView1_CellMouseClick(object sender,DataGridViewCellMouseEventArgs e)
{
Form1 F1 = new Form1(this.DataGridView1.CurrentRow.Cells [0] .Value.ToString(),
this.DataGridView1。 CurrentRow.Cells [1] .Value.ToString(),this.DataGridView1.CurrentRow.Cells [2] .Value.ToString(),
}
解决方案
你一直在创造新的实例 - 这根本不是你需要做的事情。
见这里:在两种表格之间转移信息,第2部分:从孩子到父母 [ ^ ]
另外两个也可能有用:
在两种表格之间转移信息,第1部分:父母对孩子 [ ^ ]
在两种表格之间传递信息,第3部分:儿童与儿童 [ ^ ]
Guys help me with this problem I have two form Form1 contains Four ComboBox and Form2 contain one ComboBox and one Datagridview with three column. When I mouseclick on One ComboBox in Form1 it displays Form1 and fill ComboBox that come from ComboBox (Form1) and fill DataGridView info that related to that ComboBox(Form2). So, When I click on one row in DataGridView (in Form2), the data from that row it passing to other three ComboBox in Form1. When I do mouseclick on ComboBox in (Form1) it passing data to ComboBox in (Form2) and display the rows in DataGridview that related to ComboBox in (Form2), correctly. However, When I mouseclick on one row it does not pass that from DataGridView in (Form2) to three ComboBox in (Form1). Please, I need your help.
What I have tried:
This is in Form1. Display Form2 and Pass data to ComboBox (Form1) <pre>private void ComboBox1_MouseClick(object sender, MouseEventArgs e) { Form2 f2 = new Form2(this.ComboBox.Text); f2.MdiParent = this.MdiParent; f2.Show(); }
this Function it return the Data from DataGridView (Form2) to Form1
public Form1(string Data1, string Data2, string Data3) { InitializeComponent(); this.ComboBox1.Text = Data1; this.ComboBox2.Text = Data2; this.ComboBox3.Text = Data3 this.ActiveControl = this.ComboBox1; }
=======================================
In Form 2 this is Code:
Get data from Form1
public Form2(string Data1) { InitializeComponent(); this.ComboBox.Text = Data1; }
This is when I send data to Form1
private void DataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e) { Form1 F1 = new Form1(this.DataGridView1.CurrentRow.Cells[0].Value.ToString(), this.DataGridView1.CurrentRow.Cells[1].Value.ToString(), this.DataGridView1.CurrentRow.Cells[2].Value.ToString(), }
解决方案
You are creating new instances all the time - that's not what you need to do at all.
See here: Transferring information between two forms, Part 2: Child to Parent[^]
The other two may be of use as well:
Transferring information between two forms, Part 1: Parent to Child[^]
Transferring information between two forms, Part 3: Child to Child[^]
这篇关于C#将数据从form2传递给开始form1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文