Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding new user control programmatically in windows forms

Hey so first off i would like to point out that I know that there are several other questions about this topic up here, I have even done this exact thing myself before. I am asking on here because I do not know what my problem is.

Here is the code where I attempt to display the new user control

private void ValidationLabel_Click(object sender, EventArgs e)
    {
        EntrySuggestion t_ES = new EntrySuggestion();
        t_ES.Show();
        MainScreen home = new MainScreen();
        home.Show();
    }

I was trying to get the t_ES to display (which it does not) but the main Screen does. Both of these are User Controls.

Here is the code for my EntrySuggestion User control

 using System;
using System.Collections;
using System.Windows.Forms;

namespace TeamManagementSystem
{
    public partial class EntrySuggestion : UserControl
    {
        private ArrayList items = new ArrayList();

        public EntrySuggestion()
        {
            InitializeComponent();
        }

        public EntrySuggestion(ArrayList i)
        {
            InitializeComponent();
            items = (ArrayList)i.Clone();
        }

        private void EntrySuggestion_Load(object sender, EventArgs e)
        {
            foreach (string item in items)
            {
                RadioButton t_RB = new RadioButton();
                t_RB.Text = item;
                ItemSuggestionTable.Controls.Add(t_RB);
            }
        }
    }
}

I do want to use the second constructor but I cannot get this to work with either. Any help would be great

like image 266
lamilambkin Avatar asked Sep 19 '25 05:09

lamilambkin


2 Answers

You need to add your user control to the display surface of the main form (or another container already present)

    MainScreen home = new MainScreen();
    home.Show();
    EntrySuggestion t_ES = new EntrySuggestion();
    home.Controls.Add(t_ES);
like image 168
Steve Avatar answered Sep 20 '25 19:09

Steve


Add your user control to the form:

home.Controls.Add(t_ES);
like image 38
rajeemcariazo Avatar answered Sep 20 '25 21:09

rajeemcariazo