Search code examples
c#winformscombobox

Populating ComboBox in User Control


I'm trying to understand the life cycle of a UserControl within a Form. The following is a test project, yet, UserControl1 (added via the Designer) is showing but no data populated in ComboBox1. UserControl2 doesn't show (added at Form1 OnLoad event), and UserControl3 doesn't show via a Button event. Any help would be appreciated.

Here's the code for Form1:

using System;
using System.Windows.Forms;

namespace ComboBoxTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            using (UserControl1 ucCtrl = new UserControl1())
            {
                groupBox2.Controls.Add(ucCtrl);
                ucCtrl.BringToFront();
            }
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            using (UserControl1 ucCtrl = new UserControl1())
            {
                groupBox3.Controls.Clear();
                groupBox3.Controls.Add(ucCtrl);
                ucCtrl.BringToFront();
            }
        }
    }
}

And here's the code for UserControl1

using System;
using System.Data;
using System.Windows.Forms;

namespace ComboBoxTest
{
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
            PopulateComboBox();
        }

        private void PopulateComboBox()
        {
            using (DataTable dtData = new DataTable())
            {
                DataColumn dtCol;
                DataRow dtRow;

                dtCol = new DataColumn
                {
                    DataType = typeof(Int32),
                    ColumnName = "Index"
                };
                dtData.Columns.Add(dtCol);

                dtCol = new DataColumn
                {
                    DataType = typeof(String),
                    ColumnName = "Option"
                };
                dtData.Columns.Add(dtCol);

                for (int xI = 0; xI < 5; xI++)
                {
                    dtRow = dtData.NewRow();

                    dtRow["Index"] = xI;
                    dtRow["Option"] = "Option " + Convert.ToString(xI);
                    dtData.Rows.InsertAt(dtRow, xI);
                }
                comboBox1.DataSource = dtData;
                comboBox1.DisplayMember = "Option";
                comboBox1.ValueMember = "Index";
                comboBox1.SelectedIndex = 0;
            }
        }
    }
}

Solution

  • UserControl1 (added via the Designer) is showing but no data populated in ComboBox1. UserControl2 doesn't show (added at Form1 OnLoad event), and UserControl3 doesn't show via a Button event

    The root issue is because you've wrapped the construction in using statements. For example:

     using (UserControl1 ucCtrl = new UserControl1())
     {
        groupBox2.Controls.Add(ucCtrl);
        ucCtrl.BringToFront();
     }
    

    To fix your issue's, just remove the using statements as when it hits the end of the using, the usercontrol is disposed of.

    For example of the above (using removed):

     UserControl1 ucCtrl = new UserControl1();
     groupBox2.Controls.Add(ucCtrl);
     ucCtrl.BringToFront();