I have 2 textbox (textbox 1 & textbox 2 ); the textbox1 has numeric value such as $1,000. Now when i am in textbox2 and if i hit keystrokes like (= or + or pageup or pagedown etc...) the textbox1 value should appear in textbox2.
The reason behind this is to enable customers improve speed in processsing/ filling data.
I don't know if I understand what you need, anyway try this:
private void textBox2_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.PageDown ||
e.KeyCode == Keys.PageUp ||
e.KeyCode == Keys.Oemplus ||
e.KeyCode == Keys.Add ||
(e.KeyCode == Keys.D0 && e.Shift))
{
textBox2.Text = textBox1.Text;
e.Handled = true;
e.SuppressKeyPress = true;
}
}
How about adding a keyPress event on textbox2 with a function like this:
private void textbox2_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '+')
{
textbox2.Text = textbox1.Text;
}
}
You can try this:
protected void textbox2_TextChanged(object sender, EventArgs e)
{
if(textbox2.Text=="+"||textbox2.Text=="=")
textbox2.Text=textbox1.Text;
}
Related
I want to limit user to type just numbers in TextBox.
I add this code In keypress Event:
private void txtPartID_KeyPress(object sender, KeyPressEventArgs e)
{
if (((e.KeyChar >= '0') && (e.KeyChar <= '9')) == false)
{
e.Handled = true;
}
}
but after that BackSpace key don't work for this TextBox. How can I change this?
You can check for backspace using this,
if(e.KeyChar == '\b')
And better way to check only for numbers is
private void txtPartID_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = !(Char.IsNumber(e.KeyChar) || e.KeyChar == 8);
}
private void TxtBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!(Char.IsDigit(e.KeyChar) && (e.KeyChar == (char)Keys.Back)))
e.Handled = true;
}
I think you should handle both back key and delete key.
if (!(Char.IsDigit(e.KeyChar) && (e.KeyChar == (char)Keys.Back)&& (e.KeyChar == (char)Keys.Delete)))
e.Handled = true;
You can use it
private void txtColumn_KeyPress(object sender, KeyPressEventArgs e)
{
if (((e.KeyChar >= '0') && (e.KeyChar <= '9') || (e.KeyChar == (char)Keys.Back)) == false)
{
e.Handled = true;
}
}
I have a textbox with a OnKeyPress event. In this textbox I wish to input only numbers, and for some specific letters like t or m, I would want to execute a code without that letter being typed in the textbox. Small sample of what I am trying to do:
//OnKeyPressed:
void TextBox1KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.T || e.KeyCode == Keys.M) Button1Click(this, EventArgs.Empty);
}
This unfortunately does not prevent the input of the letter..
Set the SuppressKeyPress property from KeyEventArgs to true, like below:
private void TextBox1KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.T || e.KeyCode == Keys.M)
{
e.SuppressKeyPress = true;
Button1Click(this, EventArgs.Empty);
}
}
You could always run the TryParse on the keyDown event so as to validate as the data gets entered. It saves the user an additional UI interaction.
private void TextBox1KeyDown(object sender, KeyEventArgs e)
{
int i;
string s = string.Empty;
s += (char)e.KeyValue;
if (!(int.TryParse(s, out i)))
{
e.SuppressKeyPress = true;
}
else if(e.KeyCode == Keys.T || e.KeyCode == Keys.M)
{
e.SuppressKeyPress = true;
Button1Click(this, EventArgs.Empty);
}
}
Just want to ask how to set text to all caps when typing in textbox
I've tried this, but it's not working.
void txt_AllCaps(object sender, KeyPressEventArgs e)
{
string s = (sender as TextBox).Text.ToString().ToUpper();
(sender as TextBox).Text = s;
}
try:
YourTextBox.CharacterCasing = CharacterCasing.Upper;
You can change the e.KeyChar in the KeyPress event handler to what you want. Try this:
private void txt_AllCaps(object sender, KeyPressEventArgs e){
e.KeyChar = e.KeyChar.ToString().ToUpper()[0];
//Or this
//if (e.KeyChar > 96 && e.KeyChar < 123) e.KeyChar = (char) (e.KeyChar - 32);
}
You should choose the solution of Shree, it's much more convenient :)
I have a gridview in my C# windows application ... It allowed to be edited and I want a special cell (named "Price") to just allow number on keypress ... I use the code below for texboxes to just allow numbers ... in which event of grid view should I write this code?
private void txtJustNumber_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsDigit((char)(e.KeyChar)) &&
e.KeyChar != ((char)(Keys.Enter)) &&
(e.KeyChar != (char)(Keys.Delete) || e.KeyChar == Char.Parse(".")) &&
e.KeyChar != (char)(Keys.Back))
{
e.Handled = true;
}
}
You can use CellValidating event of DataGridView.
private void dataGridView1_CellValidating(object sender,
DataGridViewCellValidatingEventArgs e)
{
// Validate the Price entry.
if (dataGridView1.Columns[e.ColumnIndex].Name == "Price")
{
}
}
thx guys ... I used below code and my problem resolved ...
public Form1()
{
InitializeComponent();
MyDataGridViewInitializationMethod();
}
private void MyDataGridViewInitializationMethod()
{
gvFactorItems.EditingControlShowing +=
new DataGridViewEditingControlShowingEventHandler(gvFactorItems_EditingControlShowing);
}
private void gvFactorItems_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
e.Control.KeyPress += new KeyPressEventHandler(Control_KeyPress); ;
}
private void Control_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsDigit((char)(e.KeyChar)) &&
e.KeyChar != ((char)(Keys.Enter)) &&
(e.KeyChar != (char)(Keys.Delete) || e.KeyChar == Char.Parse(".")) &&
e.KeyChar != (char)(Keys.Back))
{
e.Handled = true;
}
}
I think you should take a look at this, it will help :-
DataGridView keydown event not working in C#
I use below code to not allowing any character except numbers in a textbox ... but it allows '.' character! I don't want it to allow dot.
private void txtJustNumber_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsDigit((char)(e.KeyChar)) &&
e.KeyChar != ((char)(Keys.Enter)) &&
e.KeyChar != (char)(Keys.Delete) &&
e.KeyChar != (char)(Keys.Back)&&
e.KeyChar !=(char)(Keys.OemPeriod))
{
e.Handled = true;
}
}
use this:
if (!char.IsDigit((char)(e.KeyChar)) &&
e.KeyChar != ((char)(Keys.Enter)) &&
(e.KeyChar != (char)(Keys.Delete) || e.KeyChar == Char.Parse(".")) &&
e.KeyChar != (char)(Keys.Back)
)
it is because Keys.Delete's char value is 46 which is the same as '.'. I do not know why it likes this.
You could try this instead (where textBox1 would be your textbox):
// Hook up the text changed event.
textBox1.TextChanged += textBox1_TextChanged;
...
private void textBox1_TextChanged(object sender, EventArgs e)
{
// Replace all non-digit char's with empty string.
textBox1.Text = Regex.Replace(textBox1.Text, #"[^\d]", "");
}
Or
// Save the regular expression object globally (so it won't be created every time the text is changed).
Regex reg = new Regex(#"[^\d]");
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (reg.IsMatch(textBox1.Text))
textBox1.Text = reg.Replace(textBox1.Text, ""); // Replace only if it matches.
}
//This is the shortest way
private void txtJustNumber_KeyPress(object sender, KeyPressEventArgs e)
{
if(!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
}
try this code for your problem in keypress event :
private void txtMazaneh_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsDigit(e.KeyChar) && (int)e.KeyChar != 8 ||(e.KeyChar= .))
e.Handled = true;
}