winForms如何创建可重用的控件

时间:2018-05-14 02:10:24

标签: c# winforms instance groupbox

我有一个groupBox控件(手动创建),里面有一堆文本框控件,用于显示信息......我试图创建多个这些groupBox控件,并为每个groupBox我创建控件,我将不同的文本添加到groupBox中的每个单独的文本框中,然后显示groupBox ...有点像stackoverflow通过其数千个网页具有相同的网页设计/布局,但每个网页显示不同的信息或文本

这里在我的代码中尝试这个,但我无法找到一种方法来创建手动创建的groupBox的新实例

    // the GetInstance() method is the same as the "This" Keyword - long story

    public static void DisplayTask(Task task2Display)
    {
        Control groupBox = manuallyCreatedGroupBox; // attempt at creating reference 
        // obviously doesn't and shouldn't work -  lol

        // This is my major problem,
        // dunno how to create instance of the manually created groupBox
        // i can only reference to it, meaning i can only have one 
        // reference of the groupBox, hence only display one groupBox at a time

        groupBox.Location = taskLocation;
        taskLocation.Offset(0, 10 + groupBox.Size.Height); 

        // we do this so that the next groupBox is displayed ten "length" downward

        // this edits each individual textbox within the "new" groupBox

        foreach (Control cntrl in groupBox.Controls)
        {
            switch (cntrl.TabIndex)
            {
                case 1:

                    cntrl.BackColor = task2Display.priorityColor; // Priority Color
                    break;
                case 2:

                    cntrl.Text = task2Display.taskName; // Task Name
                    break;
                case 3:

                    cntrl.Text = task2Display.dscription; // task Description
                    break;
                     default:
                    break;
            }
        }

        GetInstance().Controls.Add(groupBox);
        // draws the to be the new groupBox control onto the form

        GetInstance().Show(); // show the form

    }

只是为了澄清我的目标

我试图显示手动创建的包含文本框控件的组框的多个实例

  • 问题是,我无法创建手动创建的groupBox的新实例,这意味着我无法一次显示多个所述groupBox控件(一次只能显示一个)

  • 我的问题是,如何使用其所有当前控件创建手动创建的groupBox的新实例。从旁注来看,如果有更好的方法来实现我想要做的事情,那我就是所有的耳朵

谢谢!

1 个答案:

答案 0 :(得分:0)

--------------------------感谢您的建议----------------- ---------------

这就是我所做的,我用控件和一切创建了一个用户控件。代码看起来像这样

public string TaskName
    {
        set
        {
            tskNameTxtBox.Text = value;
        }
    }

    public string TaskDiscription
    {
        set
        {
            tskDscrptnTxtBox.Text = value;
        }
    }

正如您所看到的,它只是一组属性,用于更改在用户控件设计器中手动创建的文本框控件的值

现在,以用户控件将显示为On的形式,这是新代码

// "GetInstance()" method is the same as "This" Keyword - long story
public static void DisplayTask(Task task2Display)
    {

        dsplyrCntrls = new Task_DisplayrUsrCntrl(); // create new instance of user control

        // assign value to user control propeties

        dsplyrCntrls.TaskName = task2Display.taskName; 
        dsplyrCntrls.TaskDiscription = task2Display.dscription;
        dsplyrCntrls.Location = taskLocation;

        taskLocation.Offset(0, dsplyrCntrls.Size.Height + 10); // we do this so that the next task that is display is displayed ten "length" downward
                                                               //   GetInstance().Controls.Add(groupBox); // draws the groupBox control onto the form

        GetInstance().Controls.Add(dsplyrCntrls); // adds user control to form so user can see
        int i = GetInstance().Controls.Count; // testing purpose - make sure control is added to form - which it is
        GetInstance().Show(); // shows the form

    }

它非常直接,所以每次" DisplayTask()"调用方法,创建用户控件的新实例,分别分配属性值,然后显示。

感谢你指点我正确的方向@Reza Aghaei