繁体   English   中英

winForms如何创建可重用的控件

[英]winForms How to a create reusable control

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

在我的代码中,我尝试这样做,但是我无法找出一种方法来创建手动创建的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控件(一次只能显示一个)

  • 我的问题是,如何使用所有当前控件创建手动创建的groupBox的新实例。 顺便说一句,如果有更好的方法来实现我想做的事情,我会全神贯注

谢谢!

--------------------------感谢咨询专家-------------------- ------------

好的,这就是我所做的,我创建了一个带有控件和所有内容的用户控件。 该代码看起来像这样

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

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

如您所见,它只是一堆属性,这些属性会更改在用户控件设计器中手动创建的文本框控件的值

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

// "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

暂无
暂无

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

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