简体   繁体   English

C#如何添加类的标签以形成控件

[英]C# how to add class's labels to form's control

I'm making menu class that makes me easy to control the menus 我正在制作菜单课程,这使我可以轻松控制菜单
For easy understand look at the image below 为了便于理解,请查看下图

在此输入图像描述


If I want to make these menus, I create my menu class and add 7 menus to the class, set the text, set the color, set the position ... 如果我想制作这些菜单,我会创建我的菜单类并在课程中添加7个菜单,设置文本,设置颜色,设置位置......

Everything is fine, But I have a problem with controls. 一切都很好,但我的控件有问题。
When I created the menu class in the Form1.cs I don't know how should I add the labels to Form1's controls. 当我在Form1.cs中创建菜单类时,我不知道应该如何将标签添加到Form1的控件中。
The way I thougth is make the class get the form's control and add the labels in the class. 我想要的方法是让类获得表单的控件并在类中添加标签。
So is there any ways to get the parent class's controls in it's child class? 那么有没有办法在其子类中获取父类的控件?

like 喜欢

main class example 主要的例子

namespace Tetris
{
    public partial class Form_Start : Form
    {
        public Form_Start()
        {
            InitializeComponent();
            Menu menu = new Menu(10,postion_x,postion_y,str_arr);
        }
    }
}

menu class example 菜单类示例

class menu
    {
        Label[] lab;
        List<string> str = new List<string>();
        int num, start_x, start_y, x_interval, y_interval;
        public create_menu(int value, int start_x, int start_y, int x_interval, int y_interval, List<string> str)
        {
            num = value;
            lab = new Label[num];
            start_x = this.start_x;
            start_y = this.start_y;
            x_interval = this.x_interval;
            y_interval = this.y_interval;
            str = this.str;
            int count = 0;
            foreach (Label label in lab)
            {
                label.Text = str[count];
                label.Location = new System.Drawing.Point(start_x + x_interval * count, start_y + y_interval * count);
                count++;
                Parent.Controls.Add(label); // <- I want to know this code
            }
        }
}

If you have any better ideas or know the other ways please tell me. 如果您有任何更好的想法或了解其他方式,请告诉我。 thank you 谢谢



------------------------------------------------- -------------------------------------------------
By sending the controls to the class, I added the labels to the form. 通过将控件发送到类,我将标签添加到表单。

create_menu menu = new create_menu(3, 20, 20, 0, 10, menu_str, this.Controls);
public create_menu(int index, int start_x_, int start_y_, int x_interval_, int y_interval_, List<string> str_, Control.ControlCollection ctrl)
{ ctrl.Add(label[i]); }

Is there any better ideas?? 有没有更好的想法?

You need to tweak your class a bit. 你需要稍微调整你的课程。 Simply inherit your menu class from System.Windows.Forms.Control class as shown below: 只需从System.Windows.Forms.Control类继承您的菜单类,如下所示:

class menu : System.Windows.Forms.Control
{
        //....your entire code for menu class
}

Now whenever you add your menu to a windows form Form1 you can access its parent using this.Parent (for getting its parent control) or this.ParentForm (for getting its parent windows form) syntax. 现在,无论何时将菜单添加到Windows窗体Form1您都可以使用this.Parent (用于获取其父控件)或this.ParentForm (用于获取其父窗口窗体)语法来访问其父窗口。 You can use this syntax inside any of the methods defined inside your menu class. 您可以在menu类中定义的任何方法中使用此语法。 Eg I can do something like this in one of your methods: 例如,我可以在你的一个方法中做这样的事情:

public create_menu(int value, int start_x, int start_y, int x_interval, int y_interval, List<string> str)
{
    //other code
    Console.WriteLine(this.Parent.Name);
}

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM