简体   繁体   English

如何在新课程中使用Form1 dsigner中的标签?

[英]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.

相关问题 如何在form1中使用Class.cs - How to use Class.cs in form1 如何将焦点设置在Form1上? - How do i set the Focus to be on the Form1? 当backgroundworker在新类中完成其操作时,如何在form1中启用真正的ToolStripMenuItem? - How can i enable true ToolStripMenuItem in form1 when backgroundworker completed his operation in a new class? 我该如何在鼠标进入事件时将新表单/用户控件移至Form1的中心? - How can i do that on mouse enter event the new form/usercontrol will move to the center of Form1? 如何在不创建 form1 的新实例的情况下在 form2 中调用 form1 的方法 - How can I call method of form1 in form2 without create new instance of form1 如何在类文件中创建本地参数类并传递参数并在 form1 中使用它们? - How can i create in a class file a local parameters class and pass the parameters and use them in form1? 我如何使用C#在其他类中使用位于Form1中的组件 - How can i use component located in form1 in other class using c# 如何在类中创建 form1 的实例并在 form1 中创建类的实例? - How can I make instance of form1 in a class and make instance of the class in form1? 如何在不为form1创建新实例的情况下往返两个单独的Windows窗体 - How do I go to and fro two individual windows forms without creating new instance for form1 我如何通过公共函数在Form1中的类中传递/使用私有变量? - How can i pass/use private variables froma class in Form1 with a public function?
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM