prers
prers

Reputation: 99

How to change label text according to the comboBox value selected?

I am trying to associate a label with the ComboBox selected value but That label is not getting triggered.What is wrong with my code?

    private void comboBoxCrewMember_SelectedIndexChanged(object sender, EventArgs e)
    {
        string crewMemberName=comboBoxCrewMember.Text;//ComboBox
        string rankName=crewMemberManager.GetRankName(crewMemberName);
        lblRankValue.Text = rankName;//label
    }

My ComboBox consists of name of crew memebers which are selected and the label consists of rank of that particular crew member which is fetched by the method GetRankName.

On execution,I get the whole list of crew members'names but on selecting those names nothing happens to the label.

Upvotes: 1

Views: 10312

Answers (3)

Rehan Manzoor
Rehan Manzoor

Reputation: 2743

its quite simple bro..

private void comboBoxCrewMember_SelectedIndexChanged(object sender, EventArgs e)
{

    string crewMemberName=comboBoxCrewMember.SelectedValue.ToString();
    lblRankValue.Text = crewMemberManager.GetRankName(crewMemberName);

}

what u need to make sure ix that GetRankName() is returning only one value.. and thats it.. hope it helps you can minimize thix code even..

like this

private void comboBoxCrewMember_SelectedIndexChanged(object sender, EventArgs e)
{
    lblRankValue.Text = crewMemberManager.GetRankName(comboBoxCrewMember.SelectedValue.ToString(););
}

Upvotes: 1

Gurunadh
Gurunadh

Reputation: 463

string crewMemberName=comboBoxCrewMember.Text;//ComboBox

the above will give you a string "crewMemberName", now make sure that the bellow method

crewMemberManager.GetRankName(crewMemberName)

is return type of string and it is written like bellow in the file

public string crewMemberManager.GetRankName(string name)

if not do same otherwise please provide that method for further verification.

Upvotes: 0

Kas
Kas

Reputation: 3903

  1. Make sure your Event is bound
  2. Make sure your crewMemberManager.GetRankName(crewMemberName); method works fine
  3. Make sure your ComboBox text is the value you want to parse to the crewMemberManager.GetRankName(crewMemberName); method

IF I were use , I would you something like below to retrieve the SelectedValue of combobox

comboBox1.SelectedIndex;selectedItem.ToString()
Object selectedItem = comboBox1.SelectedItem;
crewMemberManager.GetRankName(selectedItem.ToString());

And also I don' think your problem is with ComboBox or comboBox's selection, I think that your crewMemberManager.GetRankName(crewMemberName); method is caussing this issue , Please make sure that your crewMemberManager.GetRankName(crewMemberName); method works fine,

Upvotes: 0

Related Questions