在课堂上使用按钮 [英] using button in a class
本文介绍了在课堂上使用按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何访问我在班级上的form_上的按钮_
How can I access my button _which is on my form_in my class?
推荐答案
有很多方法可以做到这一点,但是也许这样做让您知道您可以做什么.
Well there are a lot of ways to do this but maybe this will give you an idea of something you could do.
public partial class Form1 : Form
{
public Button MyButton
{
get { return button1; }
set { button1 = value; }
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
MyClass mc = new MyClass(this);
}
}
class MyClass
{
Form1 mainForm;
public MyClass(Form1 form1)
{
mainForm = form1;
//access your button through mainForm
mainForm.MyButton.Text = "MyText";
}
}
您也可以只将Button传递给您的类,而不是像这样传递对表单的引用
You could also just pass your Button to your class instead of passing a reference to your form like so
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
MyClass mc = new MyClass(button1);
}
}
class MyClass
{
Button myButton;
public MyClass(Button myButton)
{
this.myButton = myButton;
//access your button
myButton.Text = "MyText";
}
}
这篇关于在课堂上使用按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文