Need to issue a confirmation dialog when "Delete" key is pressed - c#

Okay so I have a DataGridView which is bound with my Data.
I have coded it to be in "edit" mode or "non-edit" mode with some buttons and
DGVSomeGrid.ReadOnly = true; //Or false when applicable.
The user is also allowed to delete rows, but I have to write this to my DataBase. There are foreign keys affected and thus I would like to invoke a warning | confirmation dialog with:
DialogResult = MessageBox.Show();
if (DialogResult == DialogResult.Yes)
{}
else if (DialogResult == DialogResult.No)
{}
This I am fine with aswell.
My problem is I'm not really catching the right event (I think), and I'm not really sure how to cancel the delete procedure. Any help will be greatly appreciated.
I have experimented with:
keydown
and
keypress
events.

I dont have much experience with Winforms but I think the DataGridView.UserDeletingRow Event is just what you are looking for.

Related

Can't escape from ToolStripTextBox in a particular case

It is hard to describe the issue. Let me illustrate it with a very simple example.
Start from a new solution of winform(.net framework 4.8).
Add a menustrip with a textbox, then a datagridview.
And let's handle KeyDown event of datagridview.
if (e.KeyCode == Keys.F3)
toolStripTextBox1.Focus();
Ok, now we start the program.
Click the datagridview to focus on it.
Click the textbox to change focus.
Press Esc on your keyboard.
You can see that the datagridview gets the focus as expected.
But if you make a little change in step two, the result will be confusing.​ Press F3 instead of clicking the textbox.​ When you press Esc this time, the focus is just lost.​ I tried to print the name and the handle of focused control. It turned out to be the textbox itself.​ Can somebody explain it?
Now I'm quite sure it is some kind of bug. After Jimi's mention of hwndThatLostFocus, I had traced this variable for several hours. Although I failed to locate the exact position where hwndThatLostFocus was changed, something unreasonable was found: when Focus() was called in different cases, the result lost consistency.
Under most circumstances, if Focus() of ToolStripTextBox is called, hwndThatLostFocus of ToolStripTextBox will be set to 0, just like the example in my question. But if you click the datagridview and click the ToolStripTextBox and then click the datagridview agian, this time you call Focus(), hwndThatLostFocus will remain a pointer to the datagridview. In addition, this could be reproduced in .net 6.
Later I will report this to Microsoft. For now, there are three ways to avoid this.
Simulate a mouse click by SetCursorPos and mouse_event in user32.dll.
Use reflection like Jimi's advice.
Override ProcessCmdKey in Form, and take care of Keys.Escape yourself:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Escape && msg.HWnd == toolStripTextBox1.TextBox.Handle)
{
dataGridView1.Focus();
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
As I avoid using unnecessary reflect, and Dllimport is also some kind of ugly to me, I prefer the third one.

C# WPF backspace event?

I'm new to WPF and C# so what I'm asking is if there is a backspace event like TextChanged event for TextBoxes?
I made a small Library program with renting books and everything is viewed at a ListView.
What I currently did is that you can filter book names just by typing inside the textbox, so if you have 1000 books and you type the letter 'b' then you might have only 150 books starting with 'b'.
The problem is whenever i press backspace, I want it to previously restore it to what it was.
For example: typing "bob" and then deleted b, I get bo and now i want to present what every starts with "bo".
Now I get the idea. All I need is just another textChanged event. but something need to inform that the text was changed, and I need something better then
if (backspace key is pressed) { Invoke textChanged }
Thx guys!
Well, should i delete the post? maybe some one else will search it someday.
backspace is actually causing a TextChanged event automatically! damn. thx anyway!
https://msdn.microsoft.com/en-us/library/system.windows.forms.control.keydown(v=vs.110).aspx
Here is a reference on the msdn site.
http://csharp.net-informations.com/gui/key-press-cs.htm
On a different site (easier to read) This though looks like it is for Win Forms.
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
MessageBox.Show("Enter key pressed");
}
if (e.KeyChar == 13)
{
MessageBox.Show("Enter key pressed");
}
}
Looks like you need to create an event that fires on a key down, then get the value of that key. I think there is a Keys.Backspace but to know for sure let intellisense help you.

MessageBox without a default button

I have a form in my application where users can enter a list of serial numbers, each to be subjected to a quick check and then added to a list (this is for a stock take module). So the users typically use barcode scanners to scan the serial numbers from a pile of stock items.
I'm handling the KeyPress event of the TextBox that has focus while the users are scanning items and look for e.KeyChar == 13 (the enter key). Whenever enter is pressed, I know that I have a complete serial number which I can then validate before adding it to the list.
Here's where my problem occurs; Under certain conditions, I have to prompt the user at this point on whether he really wants the stock item added to the list or not. I'm using a MessageBox for that, like so:
if (MessageBox.Show("This is a special stock item.\r\nDo you want to add it to the list?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
// Add item to list
else
// Do not add item to list
But because this is something that only happens occasionally, the users often don't even see the MessageBox and simply scans the next serial number, which gets lost of course but which ends with an enter key, which fires the default button on the MessageBox and without having intended to do so, and sometimes not even aware that it's happened, the user adds an item to the list and misses a subsequent item.
Is there a way I can prevent the MessageBox from firing any buttons if enter is pressed? I don't mind if the user goes on scanning barcode after barcode and losing them all, as long as the MessageBox remains on screen until he realises his attention is required and deliberately select one of the two options.
Is there a way I can prevent the MessageBox from firing any buttons if
enter is pressed?
No.
I don't mind if the user goes on scanning barcode after barcode and
losing them all, as long as the MessageBox remains on screen until he
realises his attention is required and deliberately select one of the
two options.
Use the YesNoCancel option, and set the default to Button3, which will be the Cancel button. Now keep looping while the result is Cancel. When the loop drops out, the user will have selected either Yes or No:
DialogResult result;
do
{
result = MessageBox.Show("This is a special stock item.\r\nDo you want to add it to the list?", "Confirmation", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button3);
} while (result == DialogResult.Cancel);
if (result == DialogResult.Yes)
{
// yes
}
else
{
// no
}
---------- Edit ----------
I'm not overly fond of the idea of having a button on the dialog that
means nothing other than for some obscure application logic (from the
user's perspective).
Agreed...having a "noop" button isn't optimal. The solution above is a quick and dirty "fix".
When you get around to implementing your own custom MessageBox Form, here's an easy way to make it ignore the Enter key whenever a Button is currently focused:
public partial class frmVerify : Form
{
public frmVerify()
{
InitializeComponent();
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Enter && this.ActiveControl is Button)
{
return true; // suppress the keystroke
}
return base.ProcessCmdKey(ref msg, keyData);
}
// ... more code ...
}

Inserting a TAB space into a TextBox

I have seen few tutorials that claim to solve this issue online but they do not work. I would like to insert a TAB space when the TAB key is pressed, into my multiline TextBox.
A dudes response from Microsoft was that, by design, Metro apps will bring focus to the next control if you press TAB inside a TextBox. Now, this would make sense, if you were pressing TAB on a Single-line TextBox. But in a multiline TextBox? Don't you think it's more likely that the user will want to insert a TAB?
And yes, I know, you can insert a TAB space in a Metro TextBox by pressing Ctrl+TAB. But that is error prone, since most of us are used to just pressing TAB, and old habbits die hard sometimes.
Here is my issue. I have a text editor feature of my app where the user may need to enter large amounts of data. And you know what people are like, they like to separate things to make their text documents more readable and it's very uncomfortable and more tedious to use Ctrl+TAB. So I would like to know if anybody can help with a workaround for this (it can't involve a RichTextBox, though)?
Also, if I manage to find a workaround, will this increase the chances of my app release being rejected by the Store?
Subscribe to the KeyPress event of your TextBox, capture the Tab key by inspecting the KeyCode of the pressed key, and then set the Handled property of the KeyEventArgs to true so the key isn't passed onto any other controls.
Use SendKeys to send a "Tab" character to the TextBox to mimic the behavior of pressing "Ctrl+Tab", like you said:
TextBox_KeyPress(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab)
{
e.Handled = true;
SendKeys(^{TAB});
}
}
The carrot (^) represents the CTRL key.
richTextBox1.AcceptsTab = true;
in your KeyPress event of your textbox control. Make sure that you have the property set true for multiline on the textbox control
This would work if you are using a RichText Control which is what I would suggest
if (e.Key == Windows.System.VirtualKey.Tab)
{
e.Handled = true;
string SelectionText = "";
TextBox.Document.Selection.GetText(Windows.UI.Text.TextGetOptions.None, SelectionText);
TextBox.Document.Selection.TypeText(char(9) + SelectionText);
}

WPF MessageBox close without any action

I have a confirmation message box for the user in one of my apps. Below is the code for that,
MessageBoxResult res= System.Windows.MessageBox.Show("Could not find the folder, so the D: Drive will be opened instead.");
if (res == MessageBoxResult.OK)
{
MessageBox.Show("OK");
}
else
{
MessageBox.Show("Do Nothing");
}
Now, when the user clicks on the OK button, I want certain code to execute but when they click on the red cross at the upper right corner, I just want the messagebox to close without doing anything. In my case I get 'OK' displayed even when I click on the red cross icon at the upper right corner. Is there a way I can have 'Do Nothing' displayed when I click on the cross. I not want to add any more buttons.
Yes there is a very simple way, just add the "MessageBoxButtons.OKCancel" param to your MessageBox.Show method. this way you will have two buttons (OK and Cancel). this way if the user clicks the cancel button or the red cross the DialogResult.Cancel message will be returned. the following code described the solution:
System.Windows.Forms.DialogResult result = System.Windows.Forms.MessageBox.Show("Could not find the folder, so the D: Drive will be opened instead.",
"", System.Windows.Forms.MessageBoxButtons.OKCancel);
if (result == System.Windows.Forms.DialogResult.OK)
MessageBox.Show("OK");
else
MessageBox.Show("Do nothing.");
No, there isn't.
You could make your own custom dialog form.

Categories

Resources