How to prevent user from deleting item by pressing "backspace"? - c#

Users will usually expand the ComboBox, pick the desired option and the ComboBox will hide other options. Now user can delete the chosen option by pressing backspace button. May I know how to prevent it?

This could be avoided by handling the PreviewKeyDown event and marking any use of the backspace key as handled
void OnComboPreviewKeyDown(object sender, KeyEventArgs e) {
if (e.Key == Key.Back) {
e.Handled = true;
}
}

You could set its DropDownStyle to DropDownList if you don't want it to be editable at all.

Related

RadGridView Enter Key Issue

I have a winform application that contain a radgridview. Only one cell is set to enable editing. The remaining cells are read only. I have several radgridview event handle that perform different computation. When I'm editing a cell and hit the tab key it jump to the next cell(perfect). My problem is when I hit the "Enter" key, it throw a sort of infinite loop error. How can I disable the "Enter" key or change the behavior to mimic the tab key function? I try the below but it doesn't catch the "Enter" key action. I was reading that the enter key triggers functionality in our Grid (end edit, move to next row, etc.),
private void radGridView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.SuppressKeyPress = true;
}
}
I was able to solve the problem by re-coding my codes. I had two cell value that get updated on the CellValueChanged event. I move the updated code to the CellEditEnd event.

Activate a button Process via custom KeyPress

Basically in a C# Windows Form Application I am working on I have 2 buttons I press button1 and button2.
How do I make it when I press 2 custom keys simultaneously(eg. CTRL+L) the program does the steps coded for button1? Keeping in mind that the window might not be active.
I have looked at this: Keypress To Simulate A Button Click in C# but I don't think this would work if the window isn't active, and its also only one button pressed not two.
In Form KeyUp Event and the Properties is KeyPressPreview = true you can achieve this task.
private void Form_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.L && e.Control)
{
yourButton.PerformClick();
e.Handled = true;
}
}
or
if you want in Keypress Event
private void Form_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.Control && e.KeyCode == Keys.L)
{
yourButton.PerformClick();
e.Handled = true;
}
}
EDIT:
I realized that the OP want to the keypress Event from his statement "but I don't think this would work if the window isn't active"
You probably must check this
How to set a Windows hook in Visual C# .NET
If you precede any character in the button text with &, and keep UseMnemonic to true,. the alt+character will serve as shortcut key.
For example, in your case change the text of the buttons to button&1 and button&2 respectively. Keep UserMnemonic = true. Now if you press alt+1 then button1 will be pressed and if you press alt+2, button2 will be pressed.
Also 1 and 2 respectively will be underlined.
Hope that helps.

C# prevent default action on a listbox control when the right or left arrow keys are pressed

I have a listbox control in a windows app and I want to disable the default right and left arrow keydown event triggers. Currently when you press right or left arrows the selected item travels up and down the listbox. I want to add my own actions.
Try adding an event handler to the ListBox.KeyDown event. If the key pressed is an arrow key, set the Handled flag of the KeyPressEventArgs to true to prevent further processing.
A code example, based on an MSDN Forum post
private void listBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
If (e.KeyCode == Keys.Right || e.KeyCode == Keys.Left)
e.Handled = true;
}
You have to override the ProcessCmdKey method in the listbox control. Create a new class, derive it from listbox, then override the ProcessCmdKey.

Using enter key to check CheckBoxes in C#.NET

I want to use Enter key instead of Space key to check the checkboxes..
private void Form2_KeyDown(object sender, KeyEventArgs e)
{
CheckBox c1 = this.ActiveControl as CheckBox;
if (e.KeyData == Keys.Enter && this.ActiveControl.Equals(c1))
c1.Checked = true;
}
I could do it if i write this code in the KyeUp of the checkbox, but the thing is, I have several Checkboxes in the form and I cant write this under each of their KeyUp, so I need to use it under the KeyUp of the form..
What do I need to change??
Set the form's KeyPreview property to true.
Alternatively, you could loop through the checkboxes (using the Controls property, perhaps recursively) and add the same handler to every checkbox.
Simply determine which control has the focus and check/uncheck it as appropriate. This link should help: http://www.webdeveloper.com/forum/archive/index.php/t-36261.html

Press Escape key to call method

Is there a way to start a method in C# if a key is pressed? For example, Esc?
use the OnKeyPress Event of your textbox and in the event
if(e.KeyCode==Keys.Escape)
{
yourTextBox.Text = string.Empty;
}
As others have mentioned, handle the KeyDown or KeyUp event of the appropriate control. The KeyPress event would work for the Escape key as well, though it will not trigger for some keys, such as Shift, Ctrl or ALt.
If you want to execute this function anytime the user presses the Escape key, then you probably want to handle the event on the Form. If you do this, you will probably also want to set the Form's KeyPreview property to true. This will allow the Form control to receive the event even if the focus is currently inside of one of the child controls.
If you want the behavior to be specific to a control, such as clearing the text within a textbox that currently has focus, then you should handle the KeyDown or KeyUp event of the TextBox control. This way, your event handler will not be triggered if the user presses the escape key outside of the textbox.
In some situations you might want to prevent child controls from handling the same event that you've just handled. You can use the SuppressKeyPress property on the KeyEventArgs class to control this behavior:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
MessageBox.Show("Escape key pressed");
// prevent child controls from handling this event as well
e.SuppressKeyPress = true;
}
}
In case someone is looking for how to do this in a console application
if (Console.ReadKey().Key == ConsoleKey.Escape)
{
return;
}
I am writing WinForms application. User fills the textbox and if he wants to delete everything, he just clicks esc key on keyboard
I think you need to handle the KeyDown event.
You have to switch the form property "KeyPreview" to true or your events will not be fired. Handling these events alone will not do anything even though the events are correct. It will look to you like nothing really happens even though you have subscribed the proper event handlers.
First in Properties do > KeyPreview : True
Then :
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
//call your method here
}
}
Are you writing a Console application, a WinForms application or something else? Are you trying to capture the ESC key at all times (regardless of the focused window/application) or something else?
More context required.
If you're writing a Console app, then you should start looking at things like Console.ReadKey...
http://msdn.microsoft.com/en-us/library/system.console.readkey.aspx
With Event KeyPress...
//Escape
if (e.KeyChar == '')
{
this.DialogResult = DialogResult.Cancel;
e.Handled = true;
}
You can use KeyUp event too. I prefer it though.
private void Window_KeyUp(object sender, System.Windows.Input.KeyEventArgs e) {
if (e.Key == Key.Escape) {
//WHAT WILL HAPPEN INSERT HERE
}
}
The basic answer is listed here several time
Implement Form_KeyDown
Private Sub frmCustomerSearch_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
Try
If e.KeyCode = Keys.Escape Then
ClearFindForm()
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Set form.keyPreview
form.KeyPreview = true
The additional thing you need to check is whether you have a button capturing ESC so it can be the form.cancelButton
to be sure ...
form.CancelButton = nothing
This is sneaky. If you have set that and forgot about it, the Escape key will not trigger the KeyDown event.
I was led to this because a button set to be the form.CancelButton does not seem to fire if it is invisible or on a non visible tab,so KEYDOWN is your only option.

Categories

Resources