I need to have a text box on which events of Delete and Backspace works.Is it possible to have such a text box in C#,or restrict the behavior of text box in such a way. Other keys do not work.
Use TextBox.KeyPress event:
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete || e.KeyCode == Keys.Back)
{
// your stuff
}
e.Handled = true;
}
For winforms you can do it like this:
protected void myTextBox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
e.Handled = !IsValidCharacter(e.KeyChar);
}
private bool IsValidCharacter(Keys c)
{
bool isValid = false;
if (c == Keys.Space)
{
isValid = true;
}
return isValid;
}
If you want delete key works ..
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
Keys k = e.KeyCode
If Not (k = Keys.Back Or k = Keys.Delete)
{
e.Handled = True
}
}
Related
I am working on my project and i need to add an action that is only performed while left CTRL is pressed
This is my code:
private void Izrada_kartice_KeyDown(object sender,KeyEventArgs e)
{
if(e.Control)
{
promijeni_veličinu_naslov = true;
this.BackColor = Color.Red;
}
}
private void Izrada_kartice_KeyUp(object sender, KeyEventArgs e)
{
if (e.Control)
{
promijeni_veličinu_naslov = false;
this.BackColor = Color.Green;
}
}
Now when I run this code and press CTRL all is OK, but when I release CTRL
nothing happened.
I was try this:
private void Izrada_kartice_KeyDown(object sender,KeyEventArgs e)
{
if(e.KeyCode == Keys.A)
{
promijeni_veličinu_naslov = true;
this.BackColor = Color.Red;
}
}
private void Izrada_kartice_KeyUp(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.A)
{
promijeni_veličinu_naslov = false;
this.BackColor = Color.Green;
}
}
This works fine but I need do this with CTRL
The KeyUp event will fire when you release Ctrl, but the e.Control boolean is not set to True because Ctrl is no longer being held down.
In short: Don't detect the release of Ctrl by inspecting e.Control, inspect the e.KeyCode instead; it will be Keys.ControlKey
if(e.KeyCode == Keys.ControlKey)
{
...
}
On your second code, replace Keys.A with Keys.ControlKey like this:
private void Izrada_kartice_KeyDown(object
sender,KeyEventArgs e)
{
if(e.KeyCode == Keys.ControlKey)
{
promijeni_veličinu_naslov = true;
this.BackColor = Color.Red;
}
}
private void Izrada_kartice_KeyUp(object sender,
KeyEventArgs e)
{
if(e.KeyCode == Keys.ControlKey)
{
promijeni_veličinu_naslov = false;
this.BackColor = Color.Green;
}
}
I want to make a TextBox that doesn't allow entering spaces. I disabled typing spaces with the keyboard:
void textBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Space)
{
e.Handled = true;
}
}
But if the user copies a string with spaces like "Hello world", he can paste it into the TextBox and there will be spaces in it.
you can add a TextChanged Event Handler for your TextBox and add below code in that TextChanged event:
TextBox1.Text = TextBox1.Text.Replace(" ", "");
A better control approach
private void textBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.V && e.Modifiers == Keys.Control) {
removeSpaces();
}
//Handle Ctrl+Ins
if (e.KeyCode == Keys.Control && e.KeyCode == Keys.Insert)
{
removeSpaces();
}
}
private void removeSpaces()
{
textBox.Text = textBox.Text.Replace(" ", string.Empty);
}
// To control Mouse Right click
private void textBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
textBox1.ContextMenu = new ContextMenu();
}
}
SIMPLE Solution for all
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text.Replace(" ", string.Empty);
}
A simple way is removing white space after entering data. Like:
txt_Box.Text = txt_Box.Text.Replace(" ","");
Hi I'm new in C# visual programming and I'm facing a problem in winform that is I want to make the textBox accepts numbers only when a checkBox is checked ... the problem is that I know how to use the code inside KeyPress event but it doesn't work with the idea of checkBox.
I have this code:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if(char.IsLetter(e.Keychar))
{
e.Handled = true;
}
}
Now the question is how to make this happens when a checkBox is checked???
on your key press event you could do:
if (this.checkBoxNumericOnly.Checked)
{
//your code to only allow numerics...
}
Thanks to all of you ..
I wrote this code to enter numbers but only one dot '.' and it works finally ... thanks a lot for help
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (this.checkBox1.Checked)
{
e.Handled = !char.IsDigit(e.KeyChar)&&(e.KeyChar != '.') && !char.IsControl(e.KeyChar);
if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1))
{
e.Handled = true;
}
}
}
Use the MaskedTextBox control and handle the checkbox event to change the mask property
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if(checkBox1.Checked == true)
{
maskedTextBox1.Mask = "000-000-0000";
}
else
{
maskedTextBox1.Mask = null;
}
}
Simply try this in your TextBox's keypress event :
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if(this.checkBox1.Checked)
{
//Allow only number
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
}
}
I have two textboxes, each with its own keypress event attached.
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if(e.KeyChar == '\r')
{
e.Handled = true;
// some other stuff
}
}
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
if(e.KeyChar == '\r')
{
e.Handled = true;
// some other stuff
}
}
Is there a way to connect the keypress event dynamically to the focused textbox?
EDIT:
Something like:
void KeyPress(object sender, KeyPressEventArgs e)
{
foreach(Control c in this)
{
if(c == TextBox && c.Focused)
{
if(e.KeyChar == '\r')
{
// do something
}
}
}
}
You can do that :
textBox1.KeyPress += new KeyPressEventHandler(textBox_KeyPress);
textBox2.KeyPress += new KeyPressEventHandler(textBox_KeyPress);
private void textBox_KeyPress(object sender, KeyPressEventArgs e)
{
if(e.KeyChar == '\r')
{
e.Handled = true;
// some other stuff
Console.WriteLine(((TextBox)sender).Name); //actual textbox name
}
}
I am trying to disable people from deleting a textbox in a richtextbox. The project is using windows form.
Here is the code I have:
private void Form1_Load(object sender, EventArgs e)
{
richTextBox1.KeyPress += new KeyPressEventHandler(richTextBox1_KeyPress);
}
void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)8)
{
e.Handled = true;
MessageBox.Show("Try not to delete... write freely and openly");
//The msgbox shows, but the delete still happens within the form.
}
}
Does not show messagebox and does not stop the delete:
private void Form1_Load(object sender, EventArgs e)
{
richTextBox1.KeyDown += new KeyEventHandler(richTextBox1_KeyDown);
}
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
e.Handled = true;
MessageBox.Show("Delete Pressed");
// Does not show message box...
}
}
Per the MSDN documentation on KeyPressEventArgs.KeyChar, you cannot get or set the DELETE key using that event. You will need to use the KeyEventArgs.KeyCode instead, subscribing to the KeyDown and KeyUp events.
My solution:
void richTextBox1_TextChanged(object sender, EventArgs e) {
richTextBox1.SelectAll();
richTextBox1.SelectionProtected = true;
richTextBox1.Select(richTextBox1.Text.Length, 0);
}
Side note: yes, this will flicker. Proof of concept only. To avoid the flicker, see How to append text to RichTextBox without scrolling and losing selection?
Instead Of KeyPress event use KeyDown In RichText Box.
try this to prevent from deleting text in RichText Box
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue == 46)
e.Handled = true;
}
If you want to disallow both delete and backspace You Can Change KeyDown Event as follows
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue == 8 || e.KeyValue == 46)
e.Handled = true;
}
You must add Back key to prevent delete :
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Back || e.KeyCode == Keys.Delete)
{
e.Handled = true;
MessageBox.Show("Delete Pressed");
// Does not show message box...
}
}
Edit:
Non-selectable RichTextBox :
public class ViewOnlyRichTextBox : System.Windows.Forms.RichTextBox {
// constants for the message sending
const int WM_SETFOCUS = 0x0007;
const int WM_KILLFOCUS = 0x0008;
protected override void WndProc(ref Message m) {
if(m.Msg == WM_SETFOCUS) m.Msg = WM_KILLFOCUS;
base.WndProc (ref m);
}
}
My solution is some kind of the combination of SerkanOzvatan's and LarsTech's answers. Here is the code:
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Back || e.KeyCode == Keys.Delete)
{
e.Handled = true;
MessageBox.Show("Try not to delete... write freely and openly");
// Does not show message box...
}
}
private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
richTextBox1.SelectionProtected = richTextBox1.SelectionLength > 0;
}
It works great :)
And here is another solution of my own which also works great, especially if you want to do with a TextBox (not a RichTextBox), it doesn't have a SelectionProtected, and this is used OK for both TextBox and RichTextBox (just change the class name in the following code accordingly):
public class WritableRichTextBox : RichTextBox
{
protected override bool ProcessKeyMessage(ref Message m)
{
int virtualKey = m.WParam.ToInt32();
if (SelectionLength > 0 || virtualKey == 0x08 || virtualKey == 0x2e)
{
if (virtualKey != 0x25 && virtualKey != 0x26 && virtualKey != 0x27 && virtualKey != 0x28)
return true;
}
return base.ProcessKeyMessage(ref m);
}
}