In c#, I want to select combo box items by keyboard and when i press enter after selecting one that item should be selected. how to do it?
try something like this ..., this will explain how to change the items using mouse and key board ,....
I found this method worked fine in all the conditions. But I m not sure if anything more accurate than this method is available.
bool IsMouse = false;
private void cmbMy_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (IsMouse)
{
//Write the logic if selection is changed by mouse
}
else
{
//Write the logic if selection is changed by keyboard
}
IsMouse = false;
}
private void cmbMy_IsMouseCapturedChanged(object sender, DependencyPropertyChangedEventArgs e)
{
IsMouse = true;
}
In isMouseCapturedChanged event of combo box i made a bool variable true and when selection changed of the combo box i m checking the bool doing the required task and then setting isMouse to false.
Or you need to bulid your own custom combobox ..
You need build a custom ComboBox class and override the Control.ProcessKeyEventArgs Method.
Related
I am making a Windows Forms application with algorithms for school and I want to add some nice functionality to display that the algorithm is working well. One of those things is that when the user selects an item in one listbox, the items that are part of that one item get automatically selected in another listbox. This is done by the application.
I would like it if the user could not select another item in the listbox that is automatically monitored, but enabled = false sets the color to gray which makes the text invisible when an item is automatically selected.
Is there any other way to achieve this?
What you could do is
When the program selects an entry in the second list, set a flag
When an entry in the list is selected, read that flag
If the flag is not set, unselect the item
Unset the flag
Code wise, this equates to something like the following (please note that I would not write code like this in a real-world szenario, but to get the gist of it, it should suffice)
private bool _valueIsSetProgrammatically = false;
private void listBox1_SelectedValueChanged(object sender, EventArgs e)
{
this._valueIsSetProgrammatically = true;
this.listBox2.SelectedItem = this.listBox1.SelectedItem;
}
private void listBox2_SelectedValueChanged(object sender, EventArgs e)
{
if (!this._valueIsSetProgrammatically)
{
this.listBox2.SelectedItem = null;
}
this._valueIsSetProgrammatically = false;
}
Please note that this snippet unselects the second listbox. If you'd like to retain the selected item, you could change the second method to
private void listBox2_SelectedValueChanged(object sender, EventArgs e)
{
if (!this._valueIsSetProgrammatically)
{
this.listBox2.SelectedItem = this.listBox1.SelectedItem;
}
this._valueIsSetProgrammatically = false;
}
(Technically the flag is not needed in this case, you could simply set the SelectedItem of listBox2 to the SelectedItem of listBox1.)
I have code using an AutoCompleteStringCollection:
private void txtS_TextChanged(object sender, EventArgs e)
{
TextBox t = sender as TextBox;
string[] arr = this.dbService.GetAll();
if (t != null)
{
if (t.Text.Length >= 3)
{
AutoCompleteStringCollection collection = new AutoCompleteStringCollection();
collection.AddRange(arr);
this.txtSerial.AutoCompleteCustomSource = collection;
}
}
}
How can I get the event for "item selected" after user selects an AutoComplete suggestion? And value of field?
There's no such thing as chosen item Event for a textBox, which I believe you're using for the AutoComplete. What you could do is add a key down event to your textBox. There you could verify if the enter key was pressed (clicking on a suggested link is the same as pressing enter). Something like that:
private void textBox1_KeyDown(object sender, KeyEventArgs e) {
if (e.KeyData == Keys.Enter) {
String selItem = this.textBox1.Text;
}
}
Rather than focusing on detecting if an item from the autocomplete list was selected, instead you should check if the current value of the textbox is in the set of autocomplete entries.
if (txtSerial.AutoCompleteCustomSource.Contains(t.Text))
{
// Logic to handle an exact match being selected
...
}
else
{
// Update the autocomplete entries based on what was typed in
}
If the user typed in an exact string which happens to be be within the list of autocomplete values -- OR -- they select that value from the autocomplete list -- should this produce any different behavior? I think that in most cases it should not.
Short answer: make a custom event
Long answer:
You can intercept the KeyDown event of your textbox for numpad Enter or normal Enter and the mouse doubleclick event of the toolbox and compare the content of the toolbox then fire an event if they match that a delegate will pick up.
It depends a bit on the situation and workflow of your program but I have an example where I trigger the check on focuslost of the combobox. And then I check if the selected value is part of the collection:
private void cmbt1Name1_LostFocus(object sender, RoutedEventArgs e)
{
ComboBox cmb = sender as ComboBox;
FillFivePoints(cmb);
}
private void FillFivePoints(ComboBox usedCombobox)
{
if (txtSerial.AutoCompleteCustomSource.Contains(t.Text))
{
...
I have a combobox with the the property DropDownList that runs some code on SelectionChangeCommitted event. It works fine with mouse-click but if I try to select using arrow keys it fires after the first key press. How do I get this to work as a standard drop-down that I can navigate with mouse and keys?
private void dd_jobs_SelectionChangeCommitted(object sender, EventArgs e)
{
Pk_Error p = new Pk_Error(ref_num, j[dd_jobs.SelectedIndex]);
p.Show(); p.BringToFront();
this.Close();
}
I had the same issue to adhere to 508 compliance. I had to implement a combobox that inherits from the windows one and override the functionality.
For you capture the selected text in the OnEnter call and use that during the OnLeave to make the change. Override the OnSelectionChangeCommitted to check if the list is dropped and not make a selection.
if(this.DroppedDown == true)
{
base.OnSelectionChangeCommitted (e);
sCurrentItem = this.Text;
}
I've got a combobox that opens a new form window with a datagridview, and I want the users to choose the items through that datagridview rather than through the combobox. I've got this code to achieve that:
private void comboBox1_DropDown(object sender, EventArgs e)
{
valSel.incBox = (ComboBox)sender;
valSel.Show();
if (this.comboBox1.DroppedDown)
{
MessageBox.Show("test");
SendMessage(this.comboBox1.Handle, CB_SHOWDROPDOWN, 0, 0);
}
}
As you see I'm also trying to hide the dropdown of the combobox but it isn't working. I assume it's because the combobox hasn't actually "dropped down" yet, so that part of the code is never run.
Is there an event or something I can cell when the combobox has fully "dropped down" so i can send the message to close it again?
You should be able to simply set the height of the ComboBox to something really small. Last time I looked at it, this determined the height of the popup part (the actual height of the control is determined by the UI/font size).
The more elegant way, however, would be using a custom control that just mimics the appearance of dropdown boxes (I'm rather sure that can be done some easy way).
In comboBox1.Enter set the focus to a different control if condition is met.
private void comboBox1_Enter(object sender, EventArgs e)
{
if (comboBox1.Items.Count < 1)
{
comboBox1.DroppedDown = false;
comboBox2.Focus();
MessageBox.Show("Select a list first");
comboBox2.DroppedDown = true;
}
}
1) create a KeyPress event on ComboBox from the properties.
2) write code
private void cmbClientId_KeyPress(object sender, KeyPressEventArgs e)
{
((ComboBox)sender).DroppedDown = false;
}
I am building a WinForms Application in C# .NET
The WinForms Application has a ComboBox where the DropDownStyle is set to DropDownList. When the App is launched, I read an XML file to populate the values of the ComboBox. And, at this time, nothing is selected in the ComboBox by default. As a result, buttons Change and Delete are disabled.
Now, when the user selects a value, I want the buttons Change and Delete to be enabled. So far I have accomplished (although, I am not sure that I have done it in the right way).
I have written the code in the SelectionChangeCommitted Event.
private void cbList_SelectionChangeCommitted(object sender, EventArgs e)
{
if (cbList.SelectedItem != null)
{
this.btnModify.Enabled = true;
this.btnRemove.Enabled = true;
}
else
{
this.btnModify.Enabled = false;
this.btnRemove.Enabled = false;
}
}
Now, when I chose a value...the buttons get enabled (as expected). The user then clicks on Delete button and we remove the selected value. Now, there is nothing Selected in the cbList but the buttons are still enabled?
What is the function/event where I check if a value is selected or not and then enable/disable the buttons.
At the moment, dont have Visual Studio, so I dont remember which events we have. But you can make this,
private void CheckButtons()
{
if (cbList.SelectedItem != null)
{
this.btnModify.Enabled = true;
this.btnRemove.Enabled = true;
}
else
{
this.btnModify.Enabled = false;
this.btnRemove.Enabled = false;
}
}
and use your func in event
private void cbList_SelectionChangeCommitted(object sender, EventArgs e)
{
CheckButtons();
}
as you said, after deleting, buttons are still visible, so you can put CheckButtons() function after your delete function like
DeleteX();
CheckButtons();