我现在可以传输值,问题出在开关案例中,我进行了切换,以便能够将数据传输到多个表单,仅放置打开的表单,我不能在开关中这样做,它在运行时进入默认案例,所以问题是on (FORMID) 我如何正确使用 switch case 这里是我的 form1 代码
public partial class Form1 : Form
{
Form2 f2 = new Form2();
public Form1()
{
InitializeComponent();
f2.setParent(this);
f2.MdiParent = this.MdiParent;
}
private void button1_Click(object sender, EventArgs e)
{
f2.Show();
f2.Activate();
}
}
}
这个表格2代码:
public partial class Form2 : Form
{
public int FORMID = 0;
private Form1 f1;
private Form3 f3;
DataTable dt;
public Form2()
{
InitializeComponent();
}
void load_table()
{
string constring = "Data Source =.; initial Catalog = business; Integrated Security=SSPI;";
SqlConnection CN = new SqlConnection(constring);
SqlCommand cmd = new SqlCommand("select * from T_AKARAT_BUILDING_TP", CN);
try
{
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
dt = new DataTable();
sda.Fill(dt);
BindingSource bsource = new BindingSource();
bsource.DataSource = dt;
dataGridView1.DataSource = bsource;
sda.Update(dt);
}
catch { }
}
private void Form2_Load(object sender, EventArgs e)
{
load_table();
}
public void setParent(Form1 parentValue)
{
f1 = parentValue;
}
public void setParent(Form3 parentValue)
{
f3 = parentValue;
}
private void dataGridView1_DoubleClick(object sender, EventArgs e)
{
switch (FORMID)
{
case 1:
f1.textBox1.Text = dt.Rows[dataGridView1.CurrentCell.RowIndex][0].ToString();
f1.textBox2.Text = dt.Rows[dataGridView1.CurrentCell.RowIndex][1].ToString();
this.Hide();
break;
case 2:
f3.textBox1.Text = dt.Rows[dataGridView1.CurrentCell.RowIndex][0].ToString();
f3.textBox2.Text = dt.Rows[dataGridView1.CurrentCell.RowIndex][1].ToString();
this.Hide();
break;
default:
MessageBox.Show("plz select");
break;
}
this.Hide();
}
}
}
我在表格 2 中将文本框修饰符设置为公开,但此代码不起作用&我不知道这里缺少什么或错误希望对我有所帮助
希望有人纠正我的代码而不是举例