我有2种形式,它们都具有label1,form1和form2 label1必须显示相同的输出,当我单击form1上的按钮时,form1中的label1将会更改,以便form2中的label1
Form1
SqlDataReader reader = cmdnext.ExecuteReader();
while (reader.Read())
{
label1.Text = reader[0].ToString();
break;
}
sqlcon.Close();
Lobbypage lp = new Lobbypage(label1.Text);
}
Form2
public Lobbypage(string labelText)
{
InitializeComponent();
label1.Text = labelText;
}
跳过按钮
private void button1_Click(object sender, EventArgs e)
{
sqlcon.Open();
SqlCommand cmdcurrent = sqlcon.CreateCommand();
cmdcurrent.CommandType = CommandType.Text;
cmdcurrent.Parameters.Add("@Title", SqlDbType.VarChar).Value = title;
cmdcurrent.CommandText = "update tblQLCashier set status = 'missing' where queID = (select min(queID) from tblQLCashier where status = 'On process' and department = @Title)";
cmdcurrent.ExecuteNonQuery();
SqlCommand cmdnext = sqlcon.CreateCommand();
cmdnext.CommandType = CommandType.Text;
cmdnext.Parameters.Add("@Title", SqlDbType.VarChar).Value = title;
cmdnext.CommandText = "update tblQLCashier set status = 'On process' , department = @Title where queID = (select min(queID) from tblQLCashier where status = 'Pending' and department ='')";
cmdnext.ExecuteNonQuery();
sqlcon.Close();
sqlcon.Open();
cmdnext.CommandText = "select queID from tblQLCashier where queID = (select min(queID) from tblQLCashier where department in ('', @Title) and status in ('Pending', 'On process'))";
SqlDataReader reader = cmdnext.ExecuteReader();
while (reader.Read())
{
label1.Text = reader[0].ToString();
break;
}
sqlcon.Close();
Lobbypage lp = new Lobbypage(label1);
}
form1和form2中的label1应该同时更新并具有相同的输出
答案 0 :(得分:1)
在Form2中
public Lobbypage(Label label)
{
InitializeComponent();
label1 = label;
}
在Form1
Lobbypage lp = new Lobbypage(label1);
那是怎么回事?在label1
的构造函数中提供Lobbypage
时,它将创建一个新变量(label
),它是label1
的副本。 “技巧”是label1
实际上是一个引用,因此副本也将指向相同的标签对象。
答案 1 :(得分:1)
您可以创建一个静态字符串属性,并将其分配给两个标签。
答案 2 :(得分:0)
为什么不公开标签,这是对我有用的代码:
namespace WindowsFormsApp1
{
public partial class Form2 : Form
{
static public Label label2 = new Label();
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
label2.Location = new Point(20, 20);
Controls.Add(label2);
label2.Text = "mama";
}
}
}
其他表格
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
Form2 hi = new Form2();
hi.Show();
InitializeComponent();
}
private void button1_Click_1(object sender, EventArgs e)
{
Form2.label2.Text = "Mathman";
}
}
}
确保将功能添加到处理程序。这意味着不仅仅是复制粘贴。Dubble单击表格以自动添加加载功能 after Click before Click