I can't items to my combobox

I am under the impression the code is;

comboBox1.Items.Add("NAME");

but this doesn't work.

The program compiles correctly but nothing is displayed in the box.

What am I doing wrong?

Hello Buster2209,
That statement should work without any problem. I guess it has another bug.

Cheers

I can't items to my combobox

I am under the impression the code is;

comboBox1.Items.Add("NAME");

but this doesn't work.

The program compiles correctly but nothing is displayed in the box.

What am I doing wrong?

It is adding it to your combobox drop down options, but you won't see it in the combobox's textbox until you (the user) select one of the items you've added from the drop-down options, which then makes it the selected item.

If you programatically want it to display a given field from the list you have loaded, you need to look at ComboBox.DisplayMember or at ComboBox.SelectedIndex.

Use ComboBox.DisplayMember ... don't short cut it. That way you know that what is being displayed is actually in the in the ComboBox's list of items and the word/string you want displayed.

Member Avatar for nssltd

As far as i know your code works but you can do it in the designer. under combox tasks, edit items and their you can add the text you want their

EXAMPLE

try
            {
                if (comboBox1.Text == "TEST")
                {
                    comboBox1.Items.Add("TEST A");
                }
                else
                {
                 comboBox1.Items.Add("TEST B");
                }
            }
            catch
            {
                MessageBox.Show("Sorry an error occured, try restarting the application");
            }

it's always a good idea to include error messages in code.

NSSLTD

Here is my complete code;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

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

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox1.Items.Add("test");
            comboBox1.Items.Add("test2");
        }
    }
}

It is no more than a window with a combobox in it.

I have tried this code on two different machines and with c# 2008 but the combobox is still empty!

Again, as I said above ... all you are doing is adding items to the drop-down list of the combobox, you are not changing the text displayed.

Are you saying test and test2 are not being added to the drop-down list of the combobox?

Again, as I said above ... all you are doing is adding items to the drop-down list of the combobox, you are not changing the text displayed.

Are you saying test and test2 are not being added to the drop-down list of the combobox?

When I debug my program, test and test2 are not displayed even when I click on the arrow on the combobox.

How can I make them displayed in the GUI?

Hi buster2209,
I guess you have to use another method for adding items. Please note, you can add items to your combobox when "SelectedIndexChanged" occurs. But before your combobox has not any items and index can't be changed.....

Cheers

try another way:

public Form1()
        {
            InitializeComponent();
            this.ComboBoxPopulate();
        }
 
        private void ComboBoxPopulate()
        {
            comboBox1.Items.Add("test");
            comboBox1.Items.Add("test2");
        }

Cheers

Member Avatar for nssltd

As far as i know my code, @Rogachev's code and buster2209's later paste of method works! it adds items to a drop down box, i just cant understand! try this though i put together a little program using some of my methods and Rogachev's populate method (hope you dont mind!) download the zip file and run the solution or the application under RELEASE! i hope it helps and if not reply on this thread!
Also if inside the zip file there is a blank file open that with your zip opener to!
Hope this helps
NSSLTD

Thanks for everybody's help!

I have it sorted now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.