Home > Software engineering >  How to add range for combobox in c#?
How to add range for combobox in c#?

Time:08-15

I have a combo box in table layout panel .I want to add the existing serial ports to it.But it doesn't show my ports in the items.It shows nothing.(I have to mention that when the combo box wasn't in table layout panel it worked, but now it doesn't).

private void Form1_Load(object sender, EventArgs e)
        {
            string[] ports = SerialPort.GetPortNames();
            cBoxComPort.Items.AddRange(ports);
        }

CodePudding user response:

You need InitializeComponent(); without it you should get a runtime exception.

public Form1()
{
    InitializeComponent();

    string[] ports = SerialPort.GetPortNames();
    cBoxComPort.Items.AddRange(ports);
}
  • Related