Multiple TextChanged and KeyPress event - c#

I have a lot of TextBox controls. For every TextBox control I get their text_Changed and key_Press events. Because of this my Form.cs becomes too crowded. My question is this, is it possible to make this more space free? Some events only consist of one function.
Sample Code:
private void txtItem_TextChanged(object sender, EventArgs e)
{
showButtonSave();
}
private void txtItem_KeyPress(object sender, KeyPressEventArgs e)
{
showButtonSave();
char ch = e.KeyChar;
if (ch == (char)Keys.Enter)
e.Handled = true;
}
private void txtItem2_TextChanged(object sender, EventArgs e)
{
showButtonSave();
}
private void txtItem2_KeyPress(object sender, KeyPressEventArgs e)
{
showButtonSave();
char ch = e.KeyChar;
if (ch == (char)Keys.Enter)
e.Handled = true;
}

On your txtItem2 you can go to its properties and there where it says ontextchange you should see it is set to txtItem2_KeyPress change that to txtItem_KeyPress then they will both use
private void txtItem_KeyPress(object sender, KeyPressEventArgs e)
{
showButtonSave();
char ch = e.KeyChar;
if (ch == (char)Keys.Enter)
e.Handled = true;
}

You can create one event for each type(txtItem_TextChanged / txtItem_KeyPress)and map it to all TextBox. With the help of sender you can get the actual control and manipulate as you want.

Related

Enter key in C#

I have a code like below
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
if (!textBox1.AcceptsReturn)
{
button1.PerformClick();
}
}
}
After I hit Enter, it'll send a message to another textbox and begin a new line. Can anyone help me to bring the cursor back to its first line?
I tried textBox1.SelectionStart, SelectionLength and Focus but it doesn't work, is there any another way?
You can prevent that the keypress is passed on to the control by setting the KeyPressEventArgs.Handled property to true:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
if (!textBox1.AcceptsReturn)
{
button1.PerformClick();
e.Handled = true;
}
}
}
As you mentioned in a comment that you are implementing a chat app, you also might want to implement the typical behavior of Shift+Return inserting a new line:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue == 13 && !e.Shift)
{
if (!textBox1.AcceptsReturn && !string.IsNullOrEmpty(textBox1.Text))
{
button1.PerformClick();
textBox1.Text = "";
e.Handled = true;
}
}
}
To set cursor position to the beginning of a textbox, use the following...
I will hazard a guess that you didn't use these in combination with each other...
textBox1.SelectionStart = 0;
textBox1.SelectionLength = 0;

Editing Control showing can not key any character in datagridviewcell on cell formatting

I'm develop an window form with a datagridview.
I wish to let user key in the date in the datagridviewcell.
First I'm using this code to format the cell
private void EditingControl_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar))
{
Control editingControl = (Control)sender;
if (!Regex.IsMatch(editingControl.Text + e.KeyChar, pattern))
e.Handled = true;
}
}
private string pattern = "^[0-9]{0,2}$";
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
dataGridView1.EditingControl.KeyPress -= EditingControl_KeyPress;
dataGridView1.EditingControl.KeyPress += EditingControl_KeyPress;
}
But the cell only can let the user type in 3 integer.
So I using another pattern to format the cell
private void EditingControl_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar))
{
Control editingControl = (Control)sender;
if (!Regex.IsMatch(editingControl.Text + e.KeyChar, pattern))
e.Handled = true;
}
}
private string pattern = "^([0]?[1-9]|[1|2][0-9]|[3][0|1])[./-]([0]?[1-9]|[1][0-2])[./-]([0-9]{4}|[0-9]{2})$";
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
dataGridView1.EditingControl.KeyPress -= EditingControl_KeyPress;
dataGridView1.EditingControl.KeyPress += EditingControl_KeyPress;
}
But I cannot type anything on the cell any more using this pattern.
How to format the cell that can let user type in the date formatted like this yyyy-MM-dd?

Disable white space on key press in textbox

I am beginner in WinForms so i would like to learn that,
How can i disable white space keypress in textbox?
private void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
// MyTextBox.Text ...?
}
Any help will be appreciated.
that's what you want, disallows all spaces
private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = (e.KeyChar == (char)Keys.Space);
}
Use this code:
private void txt_keyPress(object sender, KeyPressEventArgs e) \
{
if ((sender as TextBox).SelectionStart == 0)
e.Handled = (e.KeyChar == (char)Keys.Space);
else
e.Handled = false;
}

C# WindowsForms - Radio button comment/uncomment content of textbox keypress, textchanged and focusleave

i have 3 radio buttons:
rbtPercentualmedioanual
rbtPercentualmensal
rbtValorfixo
I would like to change options events for textbox1 according choosed option
If chose rbtValorfixo,
It will uncomment:
private void textbox1_TextChanged(object sender, EventArgs e)
{
//substituipontovirgula_textBox(sender as TextBox, e);
}
private void textbox1_Leave(object sender, EventArgs e)
{
//formatamoeda_textBox(sender as TextBox, e);
}
private void textbox1_KeyPress(object sender, KeyPressEventArgs e)
{
//numeropontoouvirgula_textBox(sender as TextBox, e);
formatarporcentagem_textBox(sender as TextBox, e);
}
and will comment
private void textbox1_KeyPress(object sender, KeyPressEventArgs e)
{
numeropontoouvirgula_textBox(sender as TextBox, e);
//formatarporcentagem_textBox(sender as TextBox, e);
}
If choose option rbtPercentualmedioanual or rbtPercentualmensal, it will comment:
private void textbox1_TextChanged(object sender, EventArgs e)
{
substituipontovirgula_textBox(sender as TextBox, e);
}
private void textbox1_Leave(object sender, EventArgs e)
{
formatamoeda_textBox(sender as TextBox, e);
}
private void textbox1_KeyPress(object sender, KeyPressEventArgs e)
{
numeropontoouvirgula_textBox(sender as TextBox, e);
//formatarporcentagem_textBox(sender as TextBox, e);
}
and will uncomment: formatarporcentagem_textBox
private void textbox1_KeyPress(object sender, KeyPressEventArgs e)
{
//numeropontoouvirgula_textBox(sender as TextBox, e);
//formatarporcentagem_textBox(sender as TextBox, e);
}
i dont know how comment/uncomment keypress, textchanged or focusleave event using radiobutton check, only say how, not need make all, i can do it, but i have to know if its possible and if is, how ?
Thanks
You can determine if a radiobutton or checkbox is checked by the radioButton.Checked property.
In you case it will be something like this:
private void textbox1_TextChanged(object sender, EventArgs e)
{
if(!rbtValorfixo.Checked)
substituipontovirgula_textBox(sender as TextBox, e);
}
or:
private void textbox1_KeyPress(object sender, KeyPressEventArgs e)
{
if(rbtValorfixo.Checked)
numeropontoouvirgula_textBox(sender as TextBox, e);
else
formatarporcentagem_textBox(sender as TextBox, e);
}
and so on.
You don't need to comment or uncommment code. Being within a programming language, you need to look out for right structures.
In this rather simple case, you just need an "if" block.
This might help
You may want to use something like this? Not sure as it's quite unclear what you are trying to say.
private void Operation_Changed(object sender, EventArgs e)
{
RadioButton rbt = sender as RadioButton;
if (btn != null)
{
case "rbtPercentualmedioanual":
_operation = new example2();
example1.Visible = true;
example2.Visible = true;
example3.Visible = false;
return;
case "rbtPercentualmensal":
_operation = new example3();
example1.Visible = true;
example2.Visible = true;
example3.Visible = false;
return;
case "rbtValorfixo":
_operation = new example1();
example1.Visible = true;
example2.Visible = true;
example3.Visible = false;
default:
break;
And change it to what you want to show

How to stop the first character in a text box from being '.'?

This is the code I currently have:
private void textBox_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar) && e.KeyChar != '.';
if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1) e.Handled = true;
}
KeyPress isn't good enough to do this kind of validation. A simple way to bypass it is to paste text into the text box with Ctrl+V. Or the context menu, no key event at all.
In this specific case, the TextChanged event will get the job done:
private void textBox_TextChanged(object sender, EventArgs e) {
var box = (TextBox)sender;
if (box.Text.StartsWith(".")) box.Text = "";
}
But there's a lot more to validating numeric values. You also need to reject stuff like 1.1.1 or 1.-2 etcetera. Use the Validating event instead. Drop an ErrorProvider on the form and implement the event like this:
private void textBox_Validating(object sender, CancelEventArgs e) {
var box = (TextBox)sender;
decimal value;
if (decimal.TryParse(box.Text, out value)) errorProvider1.SetError(box, "");
else {
e.Cancel = true;
box.SelectAll();
errorProvider1.SetError(box, "Invalid number");
}
}
You probably want to use the TextChanged event, since the user could paste in values. For the best experience given the requirements, I'd suggest simply removing any leading . characters.
void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.Text.StartsWith("."))
{
textBox1.Text = new string(textBox1.Text.SkipWhile(c => c == '.').ToArray());
}
}
This does not address a requirement to use only digits -- wasn't clear in the question if that is the case.
This works for copy and pasting too.
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
int decimalCount=0;
string rebuildText="";
for(int i=0; i<textBox1.Text.Length; i++)
{
if (textBox1.Text[i] == '.')
{
if (i == 0) break;
if (decimalCount == 0)
rebuildText += textBox1.Text[i];
decimalCount++;
}
else if ("0123456789".Contains(textBox1.Text[i]))
rebuildText += textBox1.Text[i];
}
textBox1.Text = rebuildText;
textBox1.SelectionStart = textBox1.Text.Length;
}
You can try this:
private void TextBox_TextChanged(object sender, EventArgs e)
{
TextBox.Text = TextBox.Text.TrimStart('.');
}

Categories