简体   繁体   中英

winForms How to a create reusable control

I have a groupBox control (which is manually created) with a bunch of textbox controls inside it that will be used to display information... I'm trying to create multiple of these groupBox controls, and for each groupBox control i create, I add varying text into each individual textbox in the groupBox and then display the groupBox... kinda the way a stackoverflow has the same web design/layout through out its thousands of webpages, but each webpage displays different information or text.

here in my code i attempt this, but i cant figure out a way to create a new instance of the manually created 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

    }

And just to clarify my objective here

I'm trying to display multiple instances of a manually created groupBox which contains textbox controls

  • and the problem is, i cant create new instances of the manually created groupBox which means i cant display multiple of said groupBox controls at a time (can display only one at a time)

  • and my question is, how do i create a new instance of my manually created groupBox with all its current controls. And on a side note, If there's a better way to achieve what I'm trying to do, I'm all ears

Thanks!

--------------------------Thanks for advice guys--------------------------------

Alright here is what i did, I created a User Control with the controls and everything. The code looks something like this

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

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

As you can see, its simply a bunch of properties that change the values of the text box controls that have been manually created in User Control Designer

Now, in the form that the User Control will be displayed On, here is the new code

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

    }

Its pretty straight forward, so every time the "DisplayTask()" method is called, a new instance of the user control is created, the property values are assigned respectively, then displayed.

Thanks for pointing me in the Right direction @Reza Aghaei

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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