Reset Text in Combobox After Selection - c#

I have a combobox on my form. It's in DropDown mode and it has autocomplete. When it is first shown, its text is "Choose part...". I would like it to reset its text to this after a selection is made. I've tried this (assuming the combobox is named comboBox1):
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
// [omitted]
comboBox1.Text = "Choose part...";
}
It only works when the selection is made using the keyboard (e.g. type a value and press [Enter] or start typing, use the arrows to select one of the autocorrect values, and press [Enter]). When the selection is made using the mouse, the text remains the value selected.
I've had problems with keyboard & mouse doing different things with comboboxes before, but that had to do with certain events not firing. I'm sure that this event is firing (the omitted code above runs regardless of the method used).
Has anyone seen this before? Any solutions?

Try using a delegate instead:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {
// [omitted]
this.BeginInvoke((MethodInvoker)delegate { comboBox1.Text = "Choose part..."; });
}
And as Hans commented, this probably is not considered the best UI implementation with how users come to expect a ComboBox to work.

Another approach is to add an item at index 0:
ComboBox1.Items.Insert(0, new ListItem("Choose Part...", "Choose Part..."));
then reset the combobox to item 0 after handling the ComboBox1.SelectedListItemChanged() event
ComboBox1._SelectedIndexChanged = 0;
The user experience issue here is somewhat ambiguous: there may be times when you want to reset the list to a "known good state". For example, a user could be confused if the list remains on the previous selection. Resetting the list box does give the user some sense that whatever action they intended actually happened.

Related

Is there a way to have a textbox value show up in another textbox when a button is clicked?

I have implemented a trackbar that adjusts a textbox value accordingly with the slider as shown with the code below.
private void trackBar_Temp_Scroll(object sender, EventArgs e)
{
Oven_Temp.Text = trackBar_Temp.Value.ToString();
I am trying to have the value displayed in the oven_temp textbox appear within another textbox called feedback, when a button is clicked using this code.
private void Oven_select_Click(object sender, EventArgs e)
{
Oven_Temp.Text = Feeback.Text;
No errors seem to be appearing however no output appears within the feedback textbox.
Anyone able to help a student out for a coursework who has done a limited amount of coding before? :)
You set Oven_Temp.Text to the value of Feeback.Text. According to your text, you however wanted to set Feeback.Text to the value of Oven_Temp.Text. So you got the Assignment flipped.
Try Feeback.Text = Oven_Temp.Text;
The other common mistake is that the Event is not actually registered with the Button. You can put a simple Message Box in to test if the event even fires. That is usually one of the first things I check with Events.

When combobox changes do something ( C# )

I've been trying to make a program with 3 comboboxes where depending on what you pick different things happen.
Here is a screenshot of what I'm stuck with.
The only thing missing in the screenshot is the following which is in the private void Form1_Load event
cBxColor1.Items.Add("Black");
cBxColor2.Items.Add("Black");
cBxTest.Items.Add("Something");
In the screenshot above I try two methods to write something in the textbox. One whenever the text changes and then checks for the choosen item. In this case Something, Black and Black. I'm planning on adding more later but so far I'm trying to get this to work with one.
The original plan was to have while(the selected texts in the comboboxes are Something, Black and Black) then add some text to the textbox if that is true.
Screenshot of the error I get when trying the other method, I'm not sure what this means.
I've googled and searched around for a solution but I truly couldn't find anything that would help to solve my problem. I would appreciate if the 1337 hax0rz on here would help me out.
TextChanged is a Event. Use it in a methode like this:
private void ComboBox_TextUpdate(Object sender, EventArgs e)
{
//Your code here
MessageBox.Show("You are in the ComboBox.TextUpdate event.");
}
Add the event with += to your combobox in your initialisation:
ComboBox.TextUpdate += ComboBox_TextUpdate;
So at every TextUpdate your Methode ComboBox_TextUpdate gets called and you can code there.
Instead of using the if condition to see if the text has changed, you should use the ComboBox event SelectedValueChanged.
To create that event right-click on your ComboBox and select properties. Select "Events" and double-click the textbox next to the SelectedValueChanged event.
Then you want to check the values of each ComboBox like you did.
private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
{
if (cBxColor1.SelectedText.Equals("Black") || cBxColor2.SelectedText.Equals("Black") || cBxTest.SelectedText.Equals("Something"))
{
tbxTest.Text = "TEST";
}
}
Also, that while statement is almost a death threat because once it enters that condition, it will not leave.
You won't be able to change the ComboBox value due to the while being executed.

How Do I Set Up Listview in WPF to Respond to Enter as well as MouseClick?

I have a WPF listview from which I make selections using "SelectionChanged" in my XAML. My backing code is in C#. The app works fine. I'd like to also make selections by pressing Enter when a particular row is highlighted, to obtain the same result as mouse-clicking on that entry.
I tried setting up a KeyUp event in my XAML and checking for when e.Key is "Return" in the backing code, and this too works fine, as far as it goes. The problem is that there's no way I can see to pick up what row in the listview I was on when I pressed Enter: SelectedIndex is -1, and SelectedItem is null, so no selection apparently occurred; all this shows is that I was in the listview when I pressed Enter.
Can someone enlighten me on how to do this? I'm relatively new to WPF, so please be specific in your response.
The SelectedIndex is directly linked to the SelectionChanged event and only changes when the item is selected.
You should use something like the ListViewItemMouseHoverEvent to get the index for the enter KeyUp event.
YourListViewItemType item;
private void ListView1_ItemMouseHover(Object sender, ListViewItemMouseHoverEventArgs e) {
item = e.Item;
}
private void Page_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(item == null) return;
if(e.KeyCode == Keys.Return)
{
// Do something with highlighted item
}
}
Something like this should work
Well, as it turns out, using the up and down arrows on the listview (when it's in focus) DOES set SelectedIndex. Not sure why I didn't pick up on that before, but once I found that out, it was a simple matter to retrieve the record from my observable collection and parse it out. I ended up coding a Keyboard.KeyUp event in my XAML and checking what key was pressed in my event handler, e.g., C or Enter means change, D or Delete means delete, and so forth.
This is a button in a ListView
It should work with a text block
You just need to get the DataContext
Button btn = (Button)sender;
GabeLib.SearchItem srchItem = (GabeLib.SearchItem)btn.DataContext;

Control is not getting focused on Keyup event

Hi all I have created a dynamic combo box with a Textbox and a button to appear as dropdown style, every thing works fine but I handled keyup event for the textbox so that when user enter some text I will search for the results and display them
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
//Some code to filter my data
textBox1.Focus();
}
But I am unable to set the focus Immediately back to the textbox after results getting displayed so can some one help me
Code I used is from here
http://www.planetsourcecode.com/vb/scripts/showcode.asp?txtCodeId=8554&lngWid=10
I have found that the Focus() method is a bit flaky.
Other options:
textBox1.Select(textBox1.Text.Length - button1, 1);
...or simply:
textBox1.Select();
If you can verify that something else is going wrong, then this might be off base, otherwise you might simply be fighting weirdness.

C# and backspace detection in textbox in order to reload listbox

As part of this assignment for my class, in which I have a listbox which is connected to a database and it displays the first and last name of the students. I have also created a search feature in which the user types in the first and/or last name they are looking for in a textbox and when they press the "search" button it displays the filtered results appear in the listbox.
The last part of the question asks me to detect when the user clears the textbox, to once again display the original data in the listbox. I have the data in a method called databaseload()and so it is really down to how to I get my program to detect that the listbox is once again empty.
I found a couple of things online, and when I tried them, it didn't work.
private void searchTextBox_KeyPress(object sender, KeyPressEventArgs e){
if (e.KeyChar == 8)
{
databaseload();
}
}
and I have also tried KeyDown
I also don't want it to reload when first backspace is detected. I want it to reload the listbox when the searchTextBox has nothing in it.
Your help is greatly appreciated.
You should handle the KeyUp event, which fires after the key has been entered into the text.
There is a TextChanged event which seems more appropriate than manually detecting backspaces. The delete key can remove all the text as well, or someone might just highlight all of the text and cut it to the Clipboard.
Example of using the TextChanged event:
private void textBox_TextChanged(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(((TextBox)sender).Text))
{
// reload database
}
}

Categories