RadGridView Enter Key Issue - c#

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.

Related

Detect when the key pressed is the first char

I've added a KeyPress event handler to my DataGridView. If user presses "=" in a Cell, this event fires. But the = key must be first char.
How can I detect whether the pressed key is the first char?
I used the code shown here for this. I've made a string variable, named meter. It keeps the last pressed key, so I can understand from the length of meter if it's the first char or not.
It is worked actually, but when user deletes the key then it gives the wrong result.
Is there anyone give me some advice? Maybe different solution?
// this keeps pressed key and makes string.
string meter = string.Empty;
void Control_KeyPress(object sender, KeyPressEventArgs e)
{
// if the first pressed "=" key. Then meter="=". So meter length=1
meter = meter +e.KeyChar.ToString();
if (meter.Length == 1)
{
//if user keypress "="
if (e.KeyChar == '=')
{
//do things
}
}
}
I find a solution. I explain how to solve for other users If they face the similar problem.
First of all thanx to #Jimi. He gave me the idea.
I added "cellTb" object that represents the cell textbox using "EditingControlShowing" event.So it allows me to detect text is "=" or not. Here is the codes.
DataGridViewCell currentCell;
TextBox cellTb; // this represents cell textbox
private void dgv_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
e.Control.KeyUp += new KeyEventHandler(Control_KeyUp);
currentCell = this.dgv.CurrentCell;
cellTb = (TextBox)e.Control;
}
void Control_KeyUp(object sender, KeyEventArgs e) //
{
if (cellTb.Text == "=") // this is my check operations.
{
if (e.KeyCode == Keys.D0) //if user keyup "="
{
//do things
}
}
}
Edit: Explanation
#Harald Coppoolse.Actually it is my fault not telling exactly what I'm trying to do. I want to try something similar excel aplication. If the user press “=”, then he/she can selects columns then when press Enter the result will shown. But the problem is after the user pressed “=” then selecting another cell make cursor leaves the main cell. I asked question about that before. There is a link below. But what i asked is some diffucult to make possible. #JohnG.( commenter) advice me to use textbox control.
Handle select click event datagridview
Its seems sense. So i decided to used textbox. I added picture for easy understanding how i perform it.
For now it seems succeeded but i do not know which problems will be occur in the future.
I want to touch on the points you draw attention.
“What would happen if the operator keeps the equal sign down for a
while,”
I tried now it returns string like that“=====”. This is user problem.
“what if the operator selects several rows and presses the equal sign?
And what about copy-paste to paste the equal sign, or drag and drop?”
Actually i never think about this situations. But i ll try if conditions.
In a conclusion i ll change my codes according to your directions.
Thank you very much and your time.
I'm not sure if it is wise to react on KeyPress. What would happen if the operator keeps the equal sign down for a while, so that a KeyPress appears rapidly after each other, or what if the operator selects several rows and presses the equal sign?
And what about copy-paste to paste the equal sign, or drag and drop?
I think what you want is this:
Whenever the operator is editing DataGridViewTextBoxCell X of the DataGridView, and during editing the contents of the EditingControl of the cell X contains only the equal sign, I want to call procedure MyProcedure(DataGridViewCell cell)
(TODO: invent proper name for MyProcedure)
So you don't want this when the operator has finished editing the cell, you want this during cell editing.
For this you need access to the DataGridViewTextBoxEditingControl Class
This object is only available while the DataGridViewTextBoxCell class is in edit mode. You get access to the object just before the operator starts editing via event DataGridView.EditingControlShowing
Using visual studio designer:
this.dataGridView1.EditingControlShowing += this.OnEditingControlShowing);
In the event handler you subscribe to the TextChanged event of the editing control that is about to be shown
private void OnEditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
e.Control.TextChanged += ShownEditingControl_TextChanged;
}
Now whenever the operator types something in the Cell's editing control, you get notified:
private void ShownEditingControl_TextChanged(object sender, EventArgs e)
{
DataGridViewTextBoxEditingControl shownEditingControl =
(DataGridViewTextBoxEditingControl)sender;
// Do what you want to do:
Debug.WriteLine(shownEditingControl.Text);
}
You can do what you want to do if the text contains only the equal sign, or if the text has several characters with the equal sign as first character.
if (shownEditingControl.Text == "="
{
// do what you want to do if the operator edited only the equal sign
// for example:
DataGridView dgv = shownEditingControl.EditingControlDataGridView;
DataGridViewCell cell = dgv.CurrentCell;
MyProcedure(cell);
}

WPF DataGrid - key that put a cell in editmode

I'm using DataGrid key press events in my key down preview and key up events. Is there a way to detect whether a key will put the grid into edit mode (and detect this before the BeginningEdit callback) so I can anticipate edit mode on editable cells rather that handling those key presses is a special way?
What's the best way to test if a key will put a cell into edit mode?
void DataGrid_OnPreviewKeyDown(object sender, KeyEventArgs e) {
if (CanEnterCellEditMode(e.Key))
return;
else if (e.Key == Key.Escape) {
}
}

How to accept Ctrl+Enter as return while processing Enter event on a Textbox in WPF?

I'm making a chat window and I want to send the message when the user presses the Enter key on the TextBox.
But I also want to let the user enter line breaks using Ctrl+Enter key.
The problem is when I set AcceptsReturn=True and the user presses the Enter key the KeyDown event fires after the TextBox appends a line break, so the sent message would always contain a line break at the end. Is there a way to disable the Enter key while still allowing Ctrl+Enter?
I came up with the ugliest way, which is when the Enter key is pressed first remove the letter before the cursor and then process it. But is there a better way?
I'm not 100% sure of your requirements, so you may have to juggle this code about a bit, but you should be able to do what you want by setting the e.Handled property of the KeyEventArgs to false. According to the linked page, this:
Gets or sets a value that indicates the present state of the event handling for a routed event as it travels the route.
In plain English, that just means that we can stop it from being used any further. Try something like this:
private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter) // if Enter key is pressed...
{
// ... and Ctrl key is NOT... then ignore it
if (Keyboard.Modifiers != ModifierKeys.Control) e.Handled = true;
}
}

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

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.

How to defocus a button when barcode scanner sent data ending with newline

I am writing a C# barcode application. I have a EAN-13 regex to detect barcodes in "Form1_KeyPress" function. I have no mechanism to detect where the input comes from. Here is my problem:
I have a reset button in the form which clears all fields and barcodes listed in a dataGridView. When I clicked on it, it gets focus as normal. When it has focus, if I read a barcode via barcode scanner, the newline at the end of each barcode reading causes this button to be clicked thus clearing all fields. So barcodes read are added to dataGridView but immediately deleted due to activation of reset button.
My current solution is to focus on a read-only textbox at the end of each "button_Click" function, but I don't want to write an irrelevant line at the end of each "click" function of buttons. What do you recommend? (by the way I cannot prevent surpress enter key in form's keydown function)
You can't capture the Enter key in the form's keystroke events because it is handled by the button.
If you add:
private void button_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.IsInputKey = true;
}
}
to a button then the Enter key won't cause the button to be clicked, and you will see a Form_KeyDown event for it.
You don't want to add this to every button, so create a simple UserControl which is just a button with this code added.
Update
This doesn't work for the space bar. If you set form.KeyPreview = true and add:
private void form_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Space)
{
e.Handled = true;
}
}
then the space bar won't press the button but it will still work in text boxes.
I don't know why Space and Enter behave differently.

Categories

Resources