Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combobox SelectedItem not working propely

I'm trying to retrieve the selected item from a combobox, though i can't get it to work.

Form1 form = new Form1();
string cpuCount = form.comboBox1.SelectedItem.ToString();

Now, this is not returning anything. BUT, if i insert this code in my InitializeComponent(), it selects item with index = 3, and return that proper item.

comboBox1.SelectedIndex = 3;

Why does it behave like this? If I now select for example item with index = 5, it still will think the selected item is the one with index = 3.

---------- I think i should expand to show you how my code looks.

Form1 - adding all items to the comboboxes.

public partial class Form1 : Form
{
    Profile profile = new Profile();
    public Form1()
    {
        InitializeComponent();
        Profile profile = new Profile();
        string[] prof = profile.getProfiles();
        foreach (var item in prof)
        {
            comboBox5.Items.Add(Path.GetFileNameWithoutExtension(item));
        }

        int ram = 1024;
        for (int i = 0; i < 7; i++)
        {
            comboBox4.Items.Add(ram + " GB");
            ram = ram * 2;
        }

        int vram = 512;
        string size;
        for (int i = 0; i < 5; i++)
        {
            if(vram > 1000)
            {
                size = " GB";
            }
            else
            {
                size = " MB";
            }
            comboBox2.Items.Add(vram + size);
            vram = vram * 2;
        }

        for (int i = 1; i < 5; i++)
        {
            comboBox1.Items.Add(i * 2);
        }

        for (int i = 0; i < 5; i++)
        {
            comboBox3.Items.Add(i * 2);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string current = profile.currentProfile();
            profile.saveProfile(current);
        }

    }

So, button3 is my "save"button. And here is my "Profile"-class

class Profile
{
    public string folder { get; set; }
    public Profile()
    {
        this.folder = "Profiles";
        if (!File.Exists(folder))
        {
            Directory.CreateDirectory(folder);
            File.Create(folder + "/default.cfg").Close();
        }
    }

    public string[] getProfiles()
    {
        string[] files = Directory.GetFiles(folder);
        return files;
    }

    public void saveProfile(string filename)
    {
        Form1 form = new Form1();
        string cpuCount = "cpuCount=" + form.comboBox1.SelectedItem;
        string RAM = "maxRAM=" + form.comboBox4.SelectedItem;
        string VRAM = "maxVRAM=" + form.comboBox2.SelectedItem;
        string threads = "cpuThreads=" + form.comboBox3.SelectedItem;
        string path = folder + "/" + filename;
        StreamWriter sw = new StreamWriter(path);
        string[] lines = { cpuCount, RAM, VRAM, threads };

        foreach (var item in lines)
        {
            sw.WriteLine(item);
        }



        sw.Close();

    }

    public string currentProfile()
    {
        Form1 form = new Form1();
        string selected = form.comboBox5.SelectedValue + ".cfg".ToString();
        return selected;
    }
}

Thank you.

like image 712
Zacharias Avatar asked Dec 31 '25 13:12

Zacharias


2 Answers

The problem is that there is nothing selected in your ComboBox. You create your form and then, without previous user interaction, you want to get the SelectedItem which is null at that moment.

When you create ComboBox control and fill it with items, SelectedItem property is null until you either programratically set it (by using for example comboBox1.SelectedIndex = 3) or by user interaction with the control. In this case you are not doing anything of the above and that is why you are geting the mentioned error.

EDIT Based on the edited question Change your code like this: first change the saveProfile method so you could pass the four strings which you write into the text file. Note that you could alternatively pass the reference of the form but I wouldn't suggest you that. So change the method like this:

public void saveProfile(string filename, string cpuCount, string RAM , string VRAM , string threads)
    {
        string path = folder + "/" + filename;
        using(StreamWriter sw = new StreamWriter(path)) 
        {
             sw.WriteLine("cpuCount=" + cpuCount);
             sw.WriteLine("maxRAM=" + RAM );
             sw.WriteLine("maxVRAM=" + VRAM );
             sw.WriteLine("cpuThreads=" + threads);
        }        
    }

And then call it from button3 Click event handler like this:

private void button3_Click(object sender, EventArgs e)
{
            string current = profile.currentProfile();
            string cpuCount = this.comboBox1.SelectedItem.ToString();
            string RAM =  this.comboBox4.SelectedItem.ToString();
            string VRAM = this.comboBox2.SelectedItem.ToString();
            string threads = this.comboBox3.SelectedItem().ToString();
            profile.saveProfile(current, cpuCount, RAM, VRAM, threads);
}

Or alternatively

private void button3_Click(object sender, EventArgs e)
{
            string current = profile.currentProfile();
            profile.saveProfile(current, this.comboBox1.SelectedItem.ToString(), this.comboBox4.SelectedItem.ToString(), this.comboBox2.SelectedItem.ToString(), this.comboBox3.SelectedItem().ToString());
}
like image 106
Nikola Davidovic Avatar answered Jan 02 '26 01:01

Nikola Davidovic


From what I can see, you are calling form.comboBox1.SelectedItem.ToString() right after the creation of Form1. This means that the cpuCount variable is initialized right after the form is created, thus far before you have the chance to change the selected item with your mouse.

If you want to retrieve the value of the combobox after it is changed, you can use the SelectedIndexChanged event.

like image 28
Otiel Avatar answered Jan 02 '26 03:01

Otiel