forum

Home / DeveloperSection / Forums / Accessing ComboBox text/value in another Form

Accessing ComboBox text/value in another Form

Anonymous User 1949 17-Dec-2013

I want to get the chosen language from the Form1 combobox and enable other forms to view the language. The combobox has approximately 20 languages.

 I have a method called ComboBoxLang_SelectedIndexChanged (occurs when the language in the combobo is changed):        

// Works fine in Form1.cs
private void ComboBoxLang_SelectedIndexChanged(object sender, EventArgs e)
{
    string selectedItem = this.comboBoxLang.GetItemText(this.comboBoxLang.SelectedItem);
    comboBox2.Items.Clear();
    if (selectedItem == Language.English)
    {
        ToEnglish();
    }
    else if (selectedItem == Language.French)
    {
        ToFrench();
    }
    // And so on...
}

For the moment, to test that it works I just want to display a message of the language chosen in another form. I have researched and have tried multiple ways, but obviously I am going wrong somewhere! For example:

internal string comboBoxVal
{
    get
    {
        return comboBoxLang.SelectedItem.ToString();
    }
}

But this comes up with an error: "NullReferenceException was unhandled, Object reference not set to an instance of an object".


c# c# 
Updated on 17-Dec-2013
I am a content writter !

Can you answer this question?


Answer

1 Answers

Liked By