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.
Related
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";
}
}
Is it possible to like refer a string as content for a TextBox in c#? I have a listbox with a bunch of objects in it. And each object contain a string. And when I select an object in the listbox I want its string to be the content in the TextBox, so that whatever I write gets saved to the string.
Like for example in Java you could have a PlainDocument in an object, and whenever you select a different object in a JList you could set the document in a JTextField to the objects PlainDocument.
You can either use Data Binding for an automated solution or you can manually listen for SelectedIndexChanged event of the list box and set the Text property in the event handler.
listBox1.SelectedIndexChanged += (o, e) => {
object selectedItem = listBox1.SelectedItem;
textBox1.Text = selectedItem != null ? selectedItem.ToString() : null;
};
The content of the textbox can be accessed using
myTextBox.Text
This property expects string, so your answer is Yes. I think simply assigning this property would do.
UPDATE
I think you need something like this(assuming you are using WinForms):
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if(listBox1.SelectedItem != null)
textBox1.Text = listBox1.SelectedItem.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
int index = listBox1.Items.IndexOf(listBox1.SelectedItem);
listBox1.Items.Remove(listBox1.SelectedItem);
listBox1.Items.Insert(index, textBox1.Text);
}
Though there is an action for TextChanged event of textbox in WinForms, but changing listbox from there is a bit tricky (ends up calling each other infinitely) as we are already changing textbox from the change event of listbox.
Adding a button to do this simplifies it a lot.
This question already has answers here:
C# Listbox Item Double Click Event
(7 answers)
Closed 8 years ago.
I would like to double click on an item in the list box and have that item show up in the textBox titled File name (please refer to the picture)
private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
listBox1.Items.Add(textBox2.Text);
}
this code does not work, what am I doing wrong
You're doing it other way around. You try to add the text from textbox to listbox.
private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
if(listBox1.SelectedItem != null)
{
textBox2.Text = listBox1.SelectedItem.ToString();
}
}
To get more reliable results when clicking no item in listbox, you can use this answer. Thanks to #Marcel N.
Your code does not add any text to your textbox from your listbox. For your purpose you need to use this code:
private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
textBox2.Text = listBox1.SelectedItem.ToString();
}
You don't need to use any if, because when you have some items in your listbox, you can just click or double click on them. When you don't have any items, you won't have any things to click, so null could never be returned. I tested it, and you can try too.
In a TextBox I'm monitoring the text changes. I need to check the text before doing some stuff. But I can only check the old text in the moment. How can I get the new Text ?
private void textChanged(object sender, EventArgs e)
{
// need to check the new text
}
I know .NET Framework 4.5 has the new TextChangedEventArgs class but I have to use .NET Framework 2.0.
Getting the NEW value
You can just use the Text property of the TextBox. If this event is used for multiple text boxes then you will want to use the sender parameter to get the correct TextBox control, like so...
private void textChanged(object sender, EventArgs e)
{
TextBox textBox = sender as TextBox;
if(textBox != null)
{
string theText = textBox.Text;
}
}
Getting the OLD value
For those looking to get the old value, you will need to keep track of that yourself. I would suggest a simple variable that starts out as empty, and changes at the end of each event:
string oldValue = "";
private void textChanged(object sender, EventArgs e)
{
TextBox textBox = sender as TextBox;
if(textBox != null)
{
string theText = textBox.Text;
// Do something with OLD value here.
// Finally, update the old value ready for next time.
oldValue = theText;
}
}
You could create your own TextBox control that inherits from the built-in one, and adds this additional functionality, if you plan to use this a lot.
Have a look at the textbox events such as KeyUp, KeyPress etc. For example:
private void textbox_KeyUp(object sender, KeyEventArgs e)
{
// Do whatever you need.
}
Maybe these can help you achieve what you're looking for.
Even with the older .net fw 2.0 you should still have the new and old value in the eventArgs if not in the textbox.text property itself since the event is fired after and not during the text changing.
If you want to do stuff while the text is being changed then try the KeyUp event rather then the Changed.
private void stIDTextBox_TextChanged(object sender, EventArgs e)
{
if (stIDTextBox.TextLength == 6)
{
studentId = stIDTextBox.Text; // Here studentId is a variable.
// this process is used to read textbox value automatically.
// In this case I can read textbox until the char or digit equal to 6.
}
}
i'm new in C# and I want to convert value of slider to textbox. One option i found is set binding for text box, but I need send value in event.
I tried some solutions, but not worked.
private void sliderName_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
Slider sliderName = sender as Slider;
TextBox textBoxName = new TextBox();
textBoxName.Text = sliderName.Value.ToString();
}
Thanks for helping and be patient with me. :)
You're creating a new TextBox, but not positioning it anywhere.
You should have your TextBox already on your form, and reference it by the name you gave it at design-time in the IDE. For example, if you just drop a TextBox on the form, the IDE will give it a name like textBox1, and you use it by that name:
private void sliderName_ValueChanged(object sender,
RoutedPropertyChangedEventArgs<double> e)
{
// Don't use the same name used on the form if you're
// declaring a variable here. Use a name that's local to
// this event.
Slider slide = sender as Slider;
// Use the IDE-set name here.
difficultyBox.Text = slide.Value.ToString();
}
Updated to reflect name change based on comment below.
OR an even faster way:
private void sliderName_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
textBox1.Text = ((Slider)sender).value.ToString();
}
The only difference between this post, and Ken's Post is that I am casting the sender as a slider while setting the text property of textBox1.
Just showing you different options and a different way of doing the same thing.
Your approach would probably work, except that you are declaring a new TextBox local to this event handler, so you'll never see anything on the screen.
Try setting the .Text property of a TextBox in your form;
private void sliderName_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
Slider sliderName = sender as Slider;
difficultyBox.Text = sliderName.Value.ToString();
}