I'm using winforms with a combobox that has a wider drop down width than it's size. when a user selects something from there, it displays just the ending of the text instead of the beginning. how do i default it to show text starting with the start of the string?
ie. combobox has items
Atlanta Georgia
Athens Georgia
Miami Florida
....
and the user picks one and all they see in the box afterwards is "a Georgia"
no, i unfortunately don't have the realestate to make the combobox bigger, and the order of the words in the list won't be changed.
Thanks!
The trick is to call the select after the SelectedIndexChanged event happens:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {
this.BeginInvoke(new Action(() => { comboBox1.Select(0, 0); }));
}
Select position zero by force:
comboBox1.Select(0, 0);
...after the selection is complete and locked in, just call Select.
You could also use:
comboBox1.SelectAll();
...if you want it all highlighted.
Related
I am working on my software design project and I need to use serial communication between arduino and computer. I use 3 buttons in the project, 2 buttons are for moving the highlighted item in Combobox and the other one is to select the item. I tried to do it that way but somehow it can only select when I press the buttons to move them. Is it possible to do that it changes the highlight of the items?
How are you making the "move" buttons change the selected item?
Anyway, to select an item in the ComboBox, use the SelectedIndex property:
private void button1_Click(object sender, EventArgs e)
{
comboBox1.DroppedDown = true;
comboBox1.SelectedIndex = 1; // selected the second item
}
Not sure how you're receiving commands from your Arduino, but when you want to send an up arrow, you could call:
private void ArrowUp()
{
SendKeys.Send("{Up}");
}
Solution is when button is read I wrote this code, SendKeys.Send("{UP}"); or SendKeys.Send("{DOWN}"); depending on the button.
I'm developing Win Form Application
In my UI, I have to make ComboBox which should be dropdown only at first but after selecting any item, it should be partially editable.
E.G.
Options are like,
Item Value - 10
Item Value - 20
Item Value - 30 etc.
Now, if Item Value - 20 is selected, number 20 should be editable (20-29 of course)
But here editing should be allowed only to change numeric value, not Text part.
and also only numeric part should be selected to make it more user friendly.
If there may some internal properties in ComboBox to do so (which I guess will not be the case), it will be straight forward way.
Else to do so, I was thinking of having a TextBox drawn/placed on ComboCox accurately.
In this approach I'm not clear how to put TextBox so accurately that my custom user control looks like single unit and it don't overlap "text" part of combobox ?
I don’t think the combo box control was intended to be used in this way as a simple validated text box seems to be a better control for you and the user if they must enter data. Granted, you could simply add all the numbers you need into the combo box, but the user would have to scroll to the number they wanted. If this list is large then it won’t be very user friendly. The problem with the user typing something into the combo box is a matter of what to do when the user finishes typing. Even with suggestions on, when the user finishes typing something into the combo box, nothing happens until the user presses the Enter key.
If the user typed something that matches one of the items in the list then presses the Enter key, the combo boxes SelectedIndexChanged event will get fired. However, if the user types something that is NOT currently in the items list and presses the Enter key the SelectedIndexChanged event will NOT get fired.
Using the combo box KeyDown event, you could capture the “EnterKey” when the user presses the enter key in the combo box, as this is something the user would do anyway to select an existing item. So when the user types something that already exist in the list and presses the Enter key BOTH SelectedIndexChanged AND KeyDown events get fired in that order. If the user types something that is NOT in the items list, then only the KeyDown event gets fired.
Using the KeyDown event a check is made on what the selected value is. If the item typed by the user is in the items list then we can ignore these as they were handled in the previous call to SelectedIndexChanged. When the user typed something new, obviously you would possibly need to check the range of the values then simply call your method with the users input.
Again this seems hacky and IMHO, a validated text box would be easier on you and simple for the user especially if they have to type it anyway.
List<string> comboData;
public Form1() {
InitializeComponent();
comboData = new List<string>();
for (int i = 1; i < 100; i++) {
comboData.Add(i.ToString());
}
comboBox1.DataSource = comboData;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {
MessageBox.Show("selection changed " + comboBox1.Text);
}
private void comboBox1_KeyDown(object sender, KeyEventArgs e) {
if (e.KeyCode == Keys.Enter) {
if (comboData.Contains(comboBox1.Text.ToString())) {
MessageBox.Show("User entered existing data: " + comboBox1.Text);
// selection changed event has already handled this, however
// if the user just pressed "enter" previously
// then selection changed event wont get fired because the selection did not change
}
else {
MessageBox.Show("User entered NEW data: " + comboBox1.Text);
// we have data that is NOT currently in the list
// so selection changed WONT get fired
// need to call your method with user typed value
}
}
}
// make user user only enters numbers
private void comboBox1_KeyPress(object sender, KeyPressEventArgs e) {
if (!Char.IsNumber(e.KeyChar)) {
e.Handled = true;
}
}
Note: I cannot tell what exactly what your combo items list contains. If there is text in the items like “Item Value –”... this seems unnecessary. In the code above I simply used the needed info i.e... the numbers in the combo box items list. Also I used a key press event to filter out unwanted alpha characters.
Hope this helps.
I have a case where the user is given a ComboBox with potentially a lot of choices in it. Paired with this is a TextBox that filters the items. What I would like to do is open the drop down list when the TextBox has focus--let the user see what the current filter accomplishes as they type it. (This isn't just autocomplete, I'm currently matching the filter text anywhere in the item, I may replace this with a RegEx search down the road.)
It sounds simple enough--drop the box when the TextBox gets focus, close it when it loses focus. It opens--and promptly closes back up. Any good answers?
My Google-Fu must be weak tonight, I can't believe nobody has wanted to do this before yet I find nothing out there. (I have seen a related thing of typing in an open ComboBox to provide suggested options like Google does but my list is required, not merely suggestions.)
You can add on the Focus event of the TextBox code for the ComboBox setting the property
ComboBox.DroppedDown = true;
Than add on the TextChanged event of the TextBox code
ComboBox.SuspentLayout();
//ComboBox.Items add/remove
ComboBox.ResumeLayout();
Don't forget to reset the items when Text is empty.
EDIT:
This seems to work (but you don't get to see the mouse)
string[] items = { "abcd", "abc", "bcd", "cd" };
private void textBox1_TextChanged(object sender, EventArgs e)
{
comboBox1.SuspendLayout();
comboBox1.Items.Clear();
comboBox1.Items.AddRange(items.Where(item => item.ToLower().Contains(textBox1.Text.ToLower())).ToArray());
comboBox1.ResumeLayout();
comboBox1.DroppedDown = true;
}
I'm using winforms with a combobox that has a wider drop down width than it's size. when a user selects something from there, it displays just the ending of the text instead of the beginning. how do i default it to show text starting with the start of the string?
ie. combobox has items
Atlanta Georgia
Athens Georgia
Miami Florida
....
and the user picks one and all they see in the box afterwards is "a Georgia"
no, i unfortunately don't have the realestate to make the combobox bigger, and the order of the words in the list won't be changed.
Thanks!
The trick is to call the select after the SelectedIndexChanged event happens:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {
this.BeginInvoke(new Action(() => { comboBox1.Select(0, 0); }));
}
Select position zero by force:
comboBox1.Select(0, 0);
...after the selection is complete and locked in, just call Select.
You could also use:
comboBox1.SelectAll();
...if you want it all highlighted.
If the user click on a item in the listbox, the listboxItems_SelectedIndexChanged is called. But, even if the user miss an item and randomly clicks inside the listbox (not on items) the listboxItems_SelectedIndexChanged is still called.
How can I change this? I only want action on item click.
Note: removing the ability to navigate the application with keyboard is not a option.
I guess that in some cases you don't have enough list items in your control, therefore you have some space that you can click on and then SelectedIndexChanged is fired.
I guess you cannot dynamically resize the control to always fit the number of list items or else you wouldn't be asking this question.
Now, what should happen when the user click (selects) the same list item? Should some logic happen even though the selected index is the same (so when it was clicked the first time the same logic happend)?
If you require that selecting the same index more than once should be ignored then you could use the following hack:
Keep a variable at the form scope (the form containing the listbox control) and each time the selection index changes set that variable. Then use it later to check if the same selection has been made to ignore handling the event. Here is an example:
private int _currSelIdx = -1; // Default value for the selected index when no selection
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedIndex == _currSelIdx)
return;
Console.WriteLine(listBox1.SelectedIndex);
_currSelIdx = listBox1.SelectedIndex;
}
It ain't pretty, but hey...whatever works!
Maybe SelectedIndexChanged is not the right place to put your logic, since it is triggered even when you change the selection with the keyboard.
I would use MouseClick instead, checking if the click occurred over the selected item, i.e. something like this:
private void listBox1_MouseClick(object sender, MouseEventArgs e)
{
if (listBox1.SelectedIndex < 0 || !listBox1.GetItemRectangle(listBox1.SelectedIndex).Contains(e.Location))
MessageBox.Show("no click");
else
MessageBox.Show("click on item " + listBox1.SelectedIndex.ToString());
}
This link may help, instead of double click, implement the same for single click
i want to detect an item double click in a winforms listbox control. [how to handle click on blank area?]