如何更改另一个类的标签?c#windows窗体visual studio
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10770920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change a label from another class? c# windows forms visual studio
提问by A Zuniga
I know there are a lot of threads talking about this and believe me I've seen all of them, but I think I'm a little slow and cant figure out how to do this so here is the thing! I have one form
我知道有很多线程在谈论这个,相信我我已经看过所有线程,但我认为我有点慢,无法弄清楚如何做到这一点,所以这就是事情!我有一个表格
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button4_Click(object sender, EventArgs e)
{
adi mYadi= new adi();
adi.paso();
}
private void Form1_Load(object sender, EventArgs e)
{
}
public void l8u(string l )
{
label8.Text = l;
}
}
The l8umethod is supposed to change the text in label8, so it can't be static because label8isn't static (is public) and I have this other class
该l8u方法应该更改 中的文本label8,因此它不能是静态的,因为label8它不是静态的(是公共的),而且我还有另一个类
public class adi :instrucion
{
private int paso;
private int registroD;
private int registroO;
private int valor;
private int vsin;
public adi()
{
paso = 1;
}
public void setRD(int i){
registroD = i;
}
public void setR0(int i)
{
registroO = i;
}
public void setV(int i)
{
valor = i;
}
public int getRD()
{
return registroD ;
}
public int getR0()
{
return registroO;
}
public int getVf()
{
return vsin;
}
public void paso(){
//in this method I need change the value of label8
}
}
The method paso is the one in charge of changing the value of label8but I just can't do it! I've tried many different ways for example doing something like
方法 paso 是负责更改值的方法,label8但我做不到!我尝试了很多不同的方法,例如做类似的事情
public void paso()
{
Form1.l8u();
}
But that's not possible since Form1is just the name of the class and l8u is not and static method, also tried setting label8as public static but visual studio didn't like that and whenever I used a new control in the form VS change the public static for just public.
但这是不可能的,因为Form1这只是类的名称,而 l8u 不是静态方法,也尝试将其设置label8为公共静态,但 Visual Studio 不喜欢那样,每当我使用 VS 形式的新控件时,都会更改公共静态只是公开。
Hope you can help me!
希望你能帮我!
采纳答案by sihirbazzz
Simply change your label' s Modifier prperty to internal or public and then call your form and change your label text directly..
只需将标签的修饰符属性更改为内部或公共,然后调用您的表单并直接更改标签文本。
i.e.
IE
Form2 frm = new Form2(); // Form 2 contains label8 and calling in a method (i.e.buttonclick) of form1
if (List<WhoLovesMe>.Count > 0)
{
frm.Label8.Text = "Someone Loves Me :)";
}
else
{
frm.Label8.Text = "Noone Loves Me :(";
}
回答by debracey
Changing the label in that manner is not a good idea and violates some programming paradigms. Generally, the underlying business logic classes are not supposed to directly manipulate the UI.
以这种方式更改标签不是一个好主意,并且违反了一些编程范式。通常,底层业务逻辑类不应该直接操作 UI。
The form contains an instance of adi. So, short of passing the form's instance (ie. this) to the adiconstructor (or to the paso method), you're kinda sunk.
该表单包含 adi 的一个实例。因此,如果没有将表单的实例(即this)传递给adi构造函数(或传递给 paso 方法),您就有点沉没了。
Better to use some kind of event that adi can fire when it needs Form1to change its display.
最好使用 adi 需要Form1更改其显示时可以触发的某种事件。
http://msdn.microsoft.com/en-us/library/aa645739(v=vs.71).aspx
http://msdn.microsoft.com/en-us/library/aa645739(v=vs.71).aspx
回答by user3787560
I know this is 2 years ago but couldnt you just do this
我知道这是 2 年前的事,但你不能这样做
public static void function(label l)
{
l.Text = "Changed text"
}
and then in the Form do
然后在表格中做
private void timer_tick(object sender, EventArgs e)
{
function(label);
}
回答by CursedGMod Fruit
I am also looking for answer, but i finally found out how to change the label of form1 from another class.
我也在寻找答案,但我终于找到了如何从另一个类更改 form1 的标签。
usually Form1.Designer.cs looks like this:
通常 Form1.Designer.cs 看起来像这样:
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(59, 174);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(72, 13);
this.label6.TabIndex = 16;
this.label6.Text = "Output String:";
Form1.Designer.cs Should look like this so you can call it on another class:
Form1.Designer.cs 应该如下所示,以便您可以在另一个类上调用它:
label8 = new System.Windows.Forms.Label();
label8.AutoSize = true;
label8.Location = new System.Drawing.Point(219, 26);
label8.Name = "label8";
label8.Size = new System.Drawing.Size(35, 13);
label8.TabIndex = 25;
label8.Text = "label8";
//
// Form1
//
this.Controls.Add(label8);
some "this." text except the part on "this.Controls.Add" in label8 at Form1.Designer.cs
一些“这个”。除了 Form1.Designer.cs 中 label8 中“this.Controls.Add”部分之外的文本
And you should call it from the another class like this:
你应该像这样从另一个类调用它:
WindowsFormsApplication999.Form1.label8.Text = "your text here."; //This should modify label8.Text.
edit:
编辑:
You should also modify this in Form1.Designer.cs
您还应该在 Form1.Designer.cs 中修改它
private System.Windows.Forms.Label label8;
into this:
进入这个:
public static System.Windows.Forms.Label label8;

