【问题标题】:winForms How to a create reusable controlwinForms 如何创建可重用控件
【发布时间】:2018-05-14 02:10:23
【问题描述】:

我有一个 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 控件(一次只能显示一个)

  • 我的问题是,我如何使用所有当前控件创建手动创建的 groupBox 的新实例。另外,如果有更好的方法来实现我想要做的事情,我会全力以赴

谢谢!

【问题讨论】:

  • 您似乎在寻找UserControl。例如看看Walkthrough: Authoring a Composite Control with Visual C#。您可以找到几个有用的链接here
  • 哇,谢谢……这几乎解决了我的问题,而且,我们又见面了……stackoverflow.com/questions/44337300/…
  • @Jamisco 如果您已经创建了您的 UserControl ,您应该在此处将其添加为答案(至少具有最低要求代码的基本想法)。它也会对其他人有所帮助。
  • 我应该把这个问题的标题改成“如何创建可重复使用的控件”之类的吗?...当前的问题与答案没有正确的关联
  • @Jamisco 当然,如果您正确更改它会更好

标签: c# winforms instance groupbox


【解决方案1】:

--------------谢谢大家的建议------ ---------------

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

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 为我指明正确的方向

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-26
    • 1970-01-01
    • 2014-08-16
    • 1970-01-01
    • 2013-09-05
    • 2021-07-08
    • 2012-10-23
    • 1970-01-01
    相关资源
    最近更新 更多