[英]How do i use labels that are in Form1 dsigner at the new class?
For example in my new class i have this: 例如在我的新课上,我有这个:
if ((connectionStart[f] == i && connectionEnd[f] == j) || (connectionStart[f] == j && connectionEnd[f] == i)) // this checking dosent work good !
{
//button2.Enabled = false;
return;
}
else
{
/*label12.Text = connectionStart[f].ToString();
label11.Text = connectionEnd[f].ToString();
label9.Visible = true;
label10.Visible = true;
label11.Visible = true;
label12.Visible = true;*/
}
Marked them with /* and */ for now 暂时用/ *和* /标记它们
But how can i use them also the button2 so it will take effect when running the program ? 但是我如何也可以使用它们button2以便它在运行程序时生效?
I cant make the button or labels static in Form1 since they are in the designer and i dont want to use static. 我不能在Form1中将按钮或标签设为静态,因为它们在设计器中,并且我不想使用静态。
And i dont want to create Form1 instance. 而且我不想创建Form1实例。
I also want to build for all the labels,buttons and other objects like that some function/s in the class so i can use them easy and so they will take effect when im running the program. 我还想为所有标签,按钮和其他对象(例如该类中的某些函数)构建对象,以便我可以轻松使用它们,以便它们在运行程序时生效。
For now they are not exist in the new class that clear. 目前,它们还不存在于新的类中。
** Maybe to create a property in Form1 for all this objects so i can use them easy in the new Class ? **也许要在Form1中为所有这些对象创建一个属性,以便我可以在新的Class中轻松使用它们? ** **
First of all these controls are placed on some form which is already instantiated. 首先,将这些控件放在已经实例化的某种形式上。 So you need to provide instance of this for in your new class (ctor, property, method, etc). 因此,您需要在新类(ctor,属性,方法等)中为其提供实例。
Then in the form class you need to open access to the controls instances (because they are private or protected by default) via public property or methods. 然后,在表单类中,您需要通过公共属性或方法打开对控件实例的访问权限(因为它们是私有的或默认情况下受保护的)。
Then you can access controls in the your class like: 然后,您可以访问类中的控件,例如:
public class MySecondClass {
private MainForm mainForm;
public MySecondClass(MainForm mainForm)
{
this.mainForm = mainForm;
}
public void SomeFinctionOfNewClass()
{
this.mainForm.Label10.Text = "Some text for label"; // it's using property
}
}
As an option 2 you can create an event in the your new class and subscribe to it in the form class where you have access to the controls. 作为选项2,您可以在新类中创建一个事件,然后在可以访问控件的表单类中订阅该事件。 Then using event arguments you can provide settings of the controls to the even handler. 然后,可以使用事件参数将控件的设置提供给even处理程序。 In this case you will not open access to the controls from any other place then form class. 在这种情况下,您将无法从窗体类之外的任何其他位置打开对控件的访问。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.