Drop down Combo Box not auto completing. - c#

The combo box which I created is not auto completing words stored in a list after typing in the first letter and quickly pressing "Tab". However if you type in slowly and then press "Tab". It's auto completes. I have figured out that the issue is with the key_down event. When you keep a key pressed then press "Tab" it doesn't auto completes, where as if you release the key and then press "Tab" it auto completes.
If I start typing in the second character and then "Tab" out it auto completes.
Key_up event:
private void cboReasonCd_KeyUp(Object eventSender, KeyEventArgs eventArgs)
{
int KeyCode = (int)eventArgs.KeyCode;
int Shift = (int)eventArgs.KeyData / 0x10000;
CancelFillCbo.ComboReasonCD_KeyUp(KeyCode, Shift);
}
Here CancelFillCbo is the form name and ComboReasonCD method is used to populate the list.
Key_Down event:
private void cboReasonCd_KeyDown(object sender, KeyEventArgs e)
{
mlCurrentPosition = cboReasonCd.SelectionStart;
}
cboReasonCd is the name of combo box.

Have you tried built-in autocomplete? You don't need pressing tab.
However if you want to write your own autocomplete, use textbox and TextChanged event to display matching words, or keydown event detecting if tab has been pressed to show them.

Related

KeyDown Event, triggered when AutoCompleteBox drop-down is opened

In AutoCompleteBox, when drop-down is opened and key is pressed (for example Enter) control's KeyDown event is not arroused, and this makes to press Enter two times. I'm trying to make small dialog box, that doesn't oblige user to press keyboard buttons lots of times, but which closes with result when user has found his variant and presses Enter only one time. How to catch this event?
AXAML:
<AutoCompleteBox
FilterMode="Contains"
KeyDown="InputBox_KeyDown"/>
C#:
private async void InputBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
// Getting form result
Close();
}
}

how to launch event from a textbox even when the field is empty c# wpf

I get data from the data base then i show it in a datagrid (wpf) , the user can make quick search (filter) from a textbox after clicking on Enter.
I use this event to handle the button -Enter- click
(OnSearch - This event is spawn whenever the search button is clicked or the Enter key is pressed.)
the problem is when the user don't write anything, the event will not be launched even when he click on Enter-Button !
how can i proceed to make it work
public MainWindow()
{
InitializeComponent();
//m_txtTest is a SearchTextBox
m_txtTest.OnSearch += new RoutedEventHandler(m_txtTest_OnSearch);
}
void m_txtTest_OnSearch(object sender, RoutedEventArgs e)
{
//to get the entered string
SearchEventArgs searchArgs = e as SearchEventArgs;
....
....
....
....
}
So when your user types something in and presses enter it is the textbox handling the Event.
When they don't type anything though the textbox doesn't have focus and cant handle the event.
What I would do is create an event for searching on the window or grid.
Somthing like this
this.OnPreviewKeyDown += new OnPreviewKeyDownEvent;
http://msdn.microsoft.com/en-us/library/ms748948.aspx
You can think of it like this
Window has focus
->Grid has focus
-->textbox has focus
What ever the last thing that has focus (or the most inner element, think of it like a cake) is will be the thing that sees the event first. If you have that event registered for that UI element it will handle it.
The textbox isn't focused so it wont see the event

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
}
}

DataGridView - Shortcut Keys Vs MouseClick for Cell Focus

I've a weird situation while keyboard shortcuts of a button along with datagridview is used. Here is the explanation:
I've a winform in which there is a datagridview and a save button. Save button has keyboard shortcut Alt+S. I enter some rows in the grid and I validate all the cells on Save button click and if cell value is invalid then I'll set the focus to that cell. Here is the code used to achieve that:
for (var index = 0; index < GridView.Rows.Count; index++)
{
var itemRow = GridView.Rows[index];
if (itemRow.Cells[SomeColumn.Name].Value.ToString() == string.Empty)
{
GridView.CurrentCell = itemRow.Cells[SomeColumn.Name];
GridView.Focus();
}
}
Above code works fine and sets the focus in the particular cell with cursor when I click on the save button.
But, when I use shortcut key, focus is not set to the cell at all. I'm not sure why it is behaving like this.
Out of curiosity I've tested an event of the datagridview. CellLeave event of datagridview is fired when I click on save button, but when I use shortcut for save button it doesn't get fired.
Why is it behaving like this? any inputs? How can I set the focus when I use shortcuts?
Update: I've done a trick to work it properly. I've added SaveButton.Focus() code in the button click event of the save button as first line. This is redundant but forcing the grid to lose the focus when user uses short cut key to access this button. This solution is working fine, but not sure what could be the reason for the above situation. Still looking for an answer. :(
Check the key combination on DataGridVeiw keyDown event then if the key is Alt+S then set focus to DatagridVeiw
Updated:
private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == (Keys.Alt | Keys.S))
{
//put your code to validate i.e. what u are trying in button click event
}
}

How to intercept the TAB key press to prevent standard focus change in C#

Normally when pressing the TAB key you change the focus to the next control in the given tab order. I would like to prevent that and have the TAB key do something else. In my case I'd like to change focus from a combobox to a completely different control. I can't do this by setting the tab order. I need to do this programatically. Any idea how? It seems like the KeyDown and KeyPress events can't handle TAB key correctly.
Thanks.
Override ProcessDialogKey or ProcessTabKey on your Form and do the logic you want depending on which control is focused.
Based on JRS's suggestion of using the PreviewKeyDown event, this sends the key press through to the control:
private void textBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Tab)
e.IsInputKey = true;
}
Then you can handle the control's KeyDown event if you want to customise the behaviour:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab)
{
MessageBox.Show("The tab key was pressed while holding these modifier keys: "
+ e.Modifiers.ToString());
}
}
TextBoxBase alternative
If the control is derived from TextBoxBase (i.e. TextBox or RichTextBox), with the Multiline property set to true, then you can simply set the AcceptsTab property to true.
TextBoxBase.AcceptsTab Property
Gets or sets a value indicating whether pressing the TAB key in a multiline text box control types a TAB character in the control instead of moving the focus to the next control in the tab order.
Override the control's LostFocus event see link below for examples:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.lostfocus.aspx
Since I am building a UserControl, I ended up using the PreviewKeyDown event on the control. This avoids having to handle key press events on the host form.
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.previewkeydown.aspx
You can try this code on your KeyDown event:
if (e.KeyCode == Keys.Tab) {
//your logic
e.SuppressKeyPress = true;
}
If the button clicked is Tab, then do any custom logic you want, then call SuppressKeyPress to stop the KeyPress event from firing and invoking the normal Tab logic for you.

Categories