我有一个名为 LabelX1 的标签。 这是在表2 的标签上。 在表1, 我有一个按钮。 我要将按钮 s 文本传输到其它表格的标签上。 我试过了 。
form2 frm2 = new form2();
frm2.labelX1.Text = this.button1.text;
但是,这行不通。 有没有一个简单、直截了当的办法来做到这一点?
我有一个名为 LabelX1 的标签。 这是在表2 的标签上。 在表1, 我有一个按钮。 我要将按钮 s 文本传输到其它表格的标签上。 我试过了 。
form2 frm2 = new form2();
frm2.labelX1.Text = this.button1.text;
但是,这行不通。 有没有一个简单、直截了当的办法来做到这一点?
您需要揭发您的标签 或它的属性。
形式2:
public string LabelText
{
get
{
return this.labelX1.Text;
}
set
{
this.labelX1.Text = value;
}
}
然后,你可以做到:
form2 frm2 = new form2();
frm2.LabelText = this.button1.text;
您可以像这样修改 Form2 的构建器 :
public Form2(string labelText)
{
InitializeComponent();
this.labelX1.Text = labelText;
}
然后在文本中创建 Form2 通过 :
Form2 frm2 = new Form2(this.button1.text);
或者你可以做到这一点 & gt; & gt;
((Label)frm2.Controls["labelX1"]).Text = "test";
窗体内 窗体2 写入此
public void ChangeLabel(string s)
{
labelX1.Text = s;
}
然后在创建表单 2 的位置这样做
form2 frm2 = new form2();
frm2.ChangeLabel(this.button1.text);
是否有一种简单、直截了当的方式这样做?
最容易的方式是让标签X1成为表单2的公开成员。 您现在的问题在于从表1 代码表2. 标签X1 不可见。 在表2 设计师中, 您可以使用标签X1 的属性, 并设置它在公共/ 内部的可见度 。
更好的办法是将标签X1. 文字作为一种财产,在课外以代码形式标出。
form2 frm2 = new form2();
((Label)frm2.Controls["labelX1"]).Text=button1.Text;
frm2.Show();
你唯一需要做的 就是把其他表格的标签 公诸于众
for instance: Form1:
public System.Windows.Forms.Label txtInfo;
然后在 Form2 中
Form1 frm =new Form1();
frm.txtInfo.text="....."//you have access because is public
我把父母的窗口财产换成以下代码:
this.MdiParent.Controls["label1"].Text = "test";
如果您需要访问您代码中其他地方的表单2, 您将无法查看您创建的表单实例。 要解决, 我将创建一个公共实例, 以保持对它的引用, 如 :
public form2 form2_pub;
然后,在你们创造它之后,你们将新的分配到你们的公开场合:
form2 frm2 = new form2();
frm2.Show();
form2_pub = frm2
现在,您可以在整个日常活动中参考表2_pub。
至少对我有用
Remember, in your setter you can run whatever other code you want. For instance, I use the following to show what I want on another form by just setting show_scanning to true:
public bool show_scanning //turns on the scanning screen
{
set
{
scanning_pnl.Visible = true;
notReady_pnl.Visible = false;
timer1.Enabled = true;
}
}
Generally the controllers are private. That is why you unable to access it from another form. Above mentioned ways such as passing data through parameters etc are so correct. There is one another method, Go to your form Form2.Designer.cs
private System.Windows.Forms.Label labelX1;
Change private into public . Now the labelX1 is visible to outside.
The another approach is Just change the modifier property of label or text to public and now it allows to access the content of form1 textbox to label on another form
所以代码是
private void button1_click(){
Form2 obj1 =new Form2();
Obj1.show();
Obj1.label1.text="welcome"+textbox1.Text;
}
您有例外吗? 您可以在表格 2 上建立公共属性, 并设置设置, 以设置标签上的文本, 或者将 labex1 访问修饰器公之于众并直接设置。 它应该有效 。
I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...
I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...
Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...
I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?
How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...
I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...
When I m trying to change the default Image of a Control on Windows Forms in Form Designer (no matter where on which control) I get this error: Error message: An item with the same key has ...
I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.