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.
Related
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);
}
im doing some work on a project that i need to pass my final exam.
So i choosed C#, Windows Forms, and i have something in my mind.
I need to help with event where i press keys on keyboard that represent word.
(like h-e-l-l-o) and if the keys will be pressed in this order something will happen, that i can figure out by myself, but i need help with the keypressed method or something.
TL;DR: I need help with event on my WinForm app that will work like when you type "AWESOME" to nowhere on youtube.
Here is some code from a project. Hook a function to the KeyPress event of the textfield. you can get the full contents inside the function or just the last pressed character.
private void IsKeyPressNumber(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (!(e.KeyChar >= '0' && e.KeyChar <= '9'))
e.Handled = true;
}
this.txtCode.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.IsKeyPressNumber);
I'm new here, and I may be blasted for not seeing the old posts about Keys, but I assure you I have read many of them and cannot find the answer I am looking for.
I have a C# program, a calculator, that correctly calculates equations, but I want to be able to call methods by both clicking and keyboard input. Like so if user types in 2 + 2 ENTER the textbox will show 4. The only way the program does that at the moment is if the user actually clicks those buttons. Researching how Keys work in C# I found a lot of information about KeyCode and Keys, but very little information about how to actually implement them within the program. One thing that I came across concerning implementing the code was this:
private void Calculator_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
{
MessageBox.Show("Up button was pressed");
}
}
The name of the form is Calculator. I can compile this code without a problem, but it doesn't seem to do anything. I can place the entire program for you guys if you want, but it is several pages long and has a lot of comments and other stuff. I just don't understand why I can't make my program read a key. I also tried KeyPress instead of KeyDown, still nothing. Any help would be much appreciated.
Edit:
Marcel N. gave a good answer and I was able to get it to work after enabling the KeyPreview and using the code:
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.KeyCode == Keys.D1)
cmd1.PerformClick();
if (e.KeyCode == Keys.D2)
cmd2.PerformClick();
if (e.KeyCode == Keys.D3)
cmd3.PerformClick();
if (e.KeyCode == Keys.D4)
cmd4.PerformClick();
......................
}
I am very happy that all the numbers work, but am still having issues getting Enter, Divide, Multiply, and the arrow keys to work. Thanks for the speedy help.
You need to set the KeyPreview property on the form to true.
This will cause all key events to be passed to the form first, before they are delegated to the focused control. Then, you can use your current KeyDown handler to call your methods/handlers.
Finally, if you don't want the key events to reach the focused control at all then make sure to set the KeyPressEventArgs.Handled property to true when you receive the event at the form level.
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;
}
}
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);
}