Changing value of label with combobox - c#

This is probably a really basic question, but I'm been out of programming for a few years so here it goes. I'm trying to make a combobox with certain values and when I press one of the values, I want labels to change their values.
Lets say I have a value "TEST" in the combobox, when selected, I want the label lblHeight to be changed to "TEST". I code I've tried is below, but I can't understand why it doesn't work. Can someone help?
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedText == "TEST")
{
lblHeight.Text = "TEST";
}
}

Try using SelctedItem instead of SelectedText, like this:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedItem.ToString() == "TEST")
{
lblHeight.Text = "TEST";
}
}

Related

ComboBox SelectedIndexChanged event: why the SelectedText propery is not changed? [duplicate]

This question already has answers here:
ComboBox.SelectedText doesn't give me the SelectedText
(11 answers)
Closed 6 years ago.
I would like to get the text of the selected item in combobox whenever the selection is changed.
I therefore use the SelectedIndexChanged event, but the combobox text does not changed. it remains empty.
private void myCombobox_SelectedIndexChanged(object sender, EventArgs e)
{
string myTxt = myCombobox.SelectedText; //myTxt is null.
}
Just when I select twice the same item, the text is changed accordingly.
Should I use another event?
Any ideas?
If you are looking for the text that is in the combobox after it is selected then you would want to do something like this:
private void myCombobox_SelectedIndexChanged(object sender, EventArgs e)
{
string myTxt = myCombobox.Text;
}
This will take all the text from the combobox, don't forget to look at your Delegate in the Designer to ensure this actually occurs once the Combobox is changed
If you indeed need the ComboBox.SelectedText (I also suggest you carefully read the description for this property before deciding https://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedtext(v=vs.110).aspx):
private void myCombobox_SelectedIndexChanged(object sender, EventArgs e)
{
var originalValue = myCombobox.SelectedText;
var tempCb = sender as ComboBox;
if(tempCB != null)
{
var newValue = tempCb.SelectedText;
}
}
The reason for getting Null value is because you are using the 'SelectedText' property. In order to Get the current value You have to use the Text property
private void myCombobox_SelectedIndexChanged(object sender, EventArgs e)
{
string cmbTextValue = this.myCombobox.text;
}
Hopes this will solve the problem :)
If you want the Text of a selected index you have to use the .Text property, not SelectedText.
For after the value is selected use the SelectionChangeCommited Event.
Try this:
private void myCombobox_SelectionChangeCommited(object sender, EventArgs e)
{
string myTxt = myCombobox.Text;
}
You can also test SelectedItem as well, not sure if that will solve a null value.
string myTxt = myCombobox.SelectedItem.Text.ToString()
But I think the latter would be used more for conversion issues. Try both, let me know how it works out.

Edit Items in a ListBox

I am creating a program using WinForms so users can input info into textboxes on one form which then are saved into a Listbox on another form. I would like to be able to edit the items saved in the listbox by opening the original form on a button click. Really struggling with it as I can't think of the code and I can't seem to find a solution.
My Code:
private void btnAdd_Click(object sender, EventArgs e)
{
RoomDisplayForm newRoomDisplayForm = new RoomDisplayForm();
newRoomDisplayForm.ShowDialog();
if(newRoomDisplayForm.DialogResult == DialogResult.OK)
{
listBoxRooms.Items.Add(newRoomDisplayForm.value);
}
newRoomDisplayForm.Close();
}
private void btnRemove_Click(object sender, EventArgs e)
{
this.listBoxRooms.Items.RemoveAt(this.listBoxRooms.SelectedIndex);
}
private void btnEdit_Click(object sender, EventArgs e)
{
}
So i've got a Add and Remove button which work perfectly just need a solution to the edit button.
Thanks in advance
I'm guessing newRoomDisplayForm.value is a property or a public member inside the form. You just need to do something like this:
private void btnEdit_Click(object sender, EventArgs e)
{
if(listBoxRooms.SelectedIndex < 0) return;
var tmpValue = listBoxRooms.Items[listBoxRooms.SelectedIndex].ToString();
RoomDisplayForm newRoomDisplayForm = new RoomDisplayForm();
newRoomDisplayForm.value = tmpValue;
newRoomDisplayForm.ShowDialog();
//TODO: inside "newRoomDisplayForm" set the value to the textbox
// ie.: myValueTextBox.Text = this.value;
if(newRoomDisplayForm.DialogResult == DialogResult.OK)
{
// replace the selected item with the new value
listBoxRooms.Items[listBoxRooms.SelectedIndex] = newRoomDisplayForm.value;
}
}
Hope it helps!
You can simply remove the listitem in that specific position, create a new item and add it again. it's kind of replacement.

How can I make a pictureBox that, when clicked, will display some text to a label?

I'm using Visual Studio, so I already have drag-and-dropped some pictureBoxes in there, and they have names and pictures. I just want to be able to click them and have a label say the name of the person in the picture that was clicked.
This is what I currently have (for the pictureBox only)
private void captainFalcon_Click(object sender, EventArgs e)
{
Label.Show("Captain Falcon");
}
FINAL EDIT: Everything is working now, I just followed everyone's suggestions!
Is this what you need?
private void captainFalcon_Click(object sender, EventArgs e)
{
Label.Text = "Captain Falcon";
}
Something like below will work.
private void pictureBox1_Click(object sender, EventArgs e)
{
label1.Text = "Captain Falcon";
}
Obviously change the control names (pictureBox1 and label1) to match yours.
Use
private void captainFalcon_Click(object sender, EventArgs e)
{
xxxxxx.Text ="Captain Falcon";
}
and instead of xxxxx use your label name.
Select your label on designer, look at its properties, and use its "(Name)" property.
For example if your label name is characterName, then your code will be
characterName.Text ="Captain Falcon";

How to run a diffrent method each time a new comboboxItem has been selected

Hi there just building a small conversion calculator and Im adding a combo box to it so its not as cluttered and easy to manage. I wont to add a few options to my combo box so that the user has different options to choose from. However I'am going to build a small class with the conversion calculations so that when a different option is selected within the combo box the correct method will be called. I will add a code snippet to show use were i'am at. I was just using the message box just so i know that it was working. Any code snippets would be great.
private void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBoxItem kilo = ((sender as ComboBox).SelectedItem as ComboBoxItem);
}
private double workOutKilo()
{
double result = 2;
return result;
}
Assign each ComboBoxItem's Tag control a function, within the SelectionChanged event, call the function.
Yeah if you use a switch than it might be easy to choose combined by the code you already have.
I would suggest something like this:
private void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBoxItem kilo = ((sender as ComboBox).SelectedItem as ComboBoxItem);
switch (kilo.ToString())
{
case "Kilo":
//Method();
break;
//...
}
}
I guess this would do the job:
private void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBox kilo = (sender as ComboBox);
int index = kilo.SelectedIndex;
switch (kilo.ToString())
{
case "0":
//Method();
break;
//...
}
}

Test to see if a listbox item is selected

I have a form that loads 3 pre-defined scores in a list box. I want to convert a selected score into a string, and then output that string in a textbox. So far i think i've converted the item to a string, and tried setting it to the textbox but it doesn't seem to be working.
private void frmStudentScores_Load(object sender, EventArgs e)
{
lstStudents.Items.Clear();
lstStudents.Items.Add("Joe Smith|93|92|98");
lstStudents.Items.Add("Mike Jones|56|61|33");
lstStudents.Items.Add("Rita Jackson|100|89|96");
lstStudents.SelectedIndex = 0;
if (this.lstStudents.SelectedIndex >= 0)
{
string a = lstStudents.Items.Cast<string>().ToString();
txtDisplay.Text = a;
}
btnUpdate.Enabled = false;
Assuming your question is about Windows Forms, One way to get the selected item is to use code like this:
txtDisplay.Text =lstStudents.SelectedItem.ToString();
It is common to want to get the selected item that the user has selected, to do this, you need to place the above code in an event to look like this for example:
private void lstStudents_SelectedIndexChanged(object sender, EventArgs e)
{
txtDisplay.Text = this.lstStudents.SelectedItem.ToString();
}
An event can be wired to the control either by code or via the VS IDE, you can't just copy and paste the above code. Ask me if you don't know how to do that.
If you want to grab the first item only, then Plutonix comment above applies. You don't need the IF statement.
Since this is the process at load time, why not try just :
private void frmStudentScores_Load(object sender, EventArgs e)
{
lstStudents.Items.Clear();
lstStudents.Items.Add("Joe Smith|93|92|98");
lstStudents.Items.Add("Mike Jones|56|61|33");
lstStudents.Items.Add("Rita Jackson|100|89|96");
lstStudents.SelectedIndex = 0;
txtDisplay.Text = lstStudents.Items[0].ToString();
btnUpdate.Enabled = false;
EDIT
then add at the listbox's event SelectedIndexChanged :
private void lstStudents_SelectedIndexChanged(object sender, EventArgs e)
{
txtDisplay.Text = lstStudents.Items[lstStudents.SelectedIndex].ToString();
}

Categories

Resources