如何从另一个班级更换标签? c#windows窗体visual studio [英] How to change a label from another class? c# windows forms visual studio
问题描述
我知道有很多线索在谈论这件事,并相信我已经看过他们所有的人,但我认为我有点缓慢,并且无法弄清楚如何做到这一点,所以这是事情!
我有一个表单
pre $ $ code public partial class Form1:Form
{
public Form1( )
{
InitializeComponent();
$ b private void button4_Click(object sender,EventArgs e)
{
adi mYadi = new adi();
adi.paso();
$ b $ private void Form1_Load(object sender,EventArgs e)
{
}
public void l8u(string l)
{
label8.Text = l;
l8u
方法应该改变 label8
中的文本,所以它不能是静态的,因为 label8
isn '静态(是公开的),我有这个其他的类
公共类adi:instrucion
{
私人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;
$ b $ public void paso(){
//在此方法中,我需要更改label8的值
paso方法负责改变 label8
但我做不到!我已经尝试了许多不同的方法,例如做一些类似于
public void paso()
{
Form1.l8u();
}
但是这不可能,因为 Form1
只是类的名称,l8u不是静态方法,也尝试将
希望你能帮助我! 's Modifier prperty to internal or public,then call your form and change your label text directly ..
即
Form2 frm = new Form2(); // Form 2包含label8,并调用form1
的方法(iebuttonclick)if(List< WhoLovesMe> .Count> 0)
{
frm.Label8。文字=有人爱我:);
}
else
{
frm.Label8.Text =Noone Loves Me :(;
}
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 l8u
method is supposed to change the text in label8
, so it can't be static because label8
isn't static (is public) and I have this other class
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 label8
but I just can't do it! I've tried many different ways for example doing something like
public void paso()
{
Form1.l8u();
}
But that's not possible since Form1
is just the name of the class and l8u is not and static method, also tried setting label8
as 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.
Hope you can help me!
Simply change your label' s Modifier prperty to internal or public and then call your form and change your label text directly..
i.e.
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 :(";
}
这篇关于如何从另一个班级更换标签? c#windows窗体visual studio的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!