I am using the below code and when testing it, the textBoxes will allow number input, but you have to enter the complete number, then arrow back to enter the decimal. Otherwise the cursor jumps in front of the first number and will not allow the decimal to be entered. I would also like to allow the $ to be inputted first.
Any ideas how I can customize this textBox to do these functions allowing the calculations to function properly given below?
private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
//Adding ToolTip
ToolTipService.SetToolTip(textBox1, "Enter Price, numbers first, then arrow back to add decimal.");
if (Double.TryParse(textBox1.Text, out value1))
{
textBox1.Text = value1.ToString();
}
textBox69.Text = value69.ToString();
if (textBox1.Text == null || textBox1.Text == "") value1 = 0;
{
textBox1.Text = value1.ToString();
if (textBox1.Text.EndsWith(".97") == true)
{
value20 = 0; value36 = 0; value69 = 0;
textBox20.Text = value20.ToString();
textBox36.Text = value36.ToString();
textBox69.Text = value69.ToString();
}
if (textBox1.Text.EndsWith("1") || textBox1.Text.EndsWith("2") || textBox1.Text.EndsWith("3") || textBox1.Text.EndsWith("4") || textBox1.Text.EndsWith("5") || textBox1.Text.EndsWith("6") || textBox1.Text.EndsWith("8") || textBox1.Text.EndsWith("9") || textBox1.Text.EndsWith("0") == true && (value1 < 100.00))
{
value69 = 1;
textBox69.Text = value69.ToString();
}
if (value1 > 100.01 && value1 < 149.99)
value69 = 2;
textBox69.Text = value69.ToString();
}
}
Related
I am doing a selling invoice and i want the system to check if the quantity entered is a double value or no and if double value the quantity entered must be smaller than the quantity in the database.The validation of checking if the user enter a double or no is working fine but the other part of the code is not working fine.the code works after the user leaved the cell and enter it again and if he edit it the system is keep saving the old value(wrong value) but for the first part it is working fine if he enters letters the system give him an error and when he correct it the system take the new value and let him leave the sell normally the problem is in the second part here is my code:
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (e.ColumnIndex == 3 || e.ColumnIndex == 4 || e.ColumnIndex == 5 || e.ColumnIndex == 6 || e.ColumnIndex == 7) // 1 should be your column index
{
if (Convert.ToString(e.FormattedValue) != "")
{
double i;
if (!double.TryParse(Convert.ToString(e.FormattedValue), out i))
{
e.Cancel = true;
MessageBox.Show("Insert correct numbers");
}
else
{
if (e.ColumnIndex == 5)
{
qty3 = Convert.ToDouble(dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[5].Value);
string code = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[0].Value.ToString();
sqlClass c = new sqlClass();
con = c.getcon();
con.Open();
c.command("select quantity from items where code='" + code + "'");
cmd = c.getcmd();
double qty2 = Convert.ToDouble(cmd.ExecuteScalar().ToString());
if (qty3 > qty2)
{
e.Cancel = true;
MessageBox.Show("You don't have enough quantity \n Quantity available is:" + qty2);
}
else
{
dataGridView1[e.ColumnIndex, e.RowIndex].ErrorText = null;
}
con.Close();
}
}
}
}
}
I am working on a database application and using this class to validate numeric numbers on KeyPress event of TextBox.
The numbers may have (-) negative values with fixed decimal places (third parameter dPlaces) e.g. 10000, -1000, 12345.45, -12345.45
After adding a decimal, I am not able to edit other digits although without a decimal it is working perfectly.
Thanks in advance
public static class Util
{
public static void NumInput(object sender, KeyPressEventArgs e, int dPlaces)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.' && (e.KeyChar != '-'))
{
e.Handled = true;
}
// only allow one decimal point
if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
{
e.Handled = true;
}
var a = (sender as TextBox).SelectionLength;
// only allow minus sign at the beginning
var x = (sender as TextBox).Text.IndexOf('-');
if (e.KeyChar == '-' && (sender as TextBox).Text.IndexOf('-') > 0)
{
e.Handled = true;
}
if (!char.IsControl(e.KeyChar))
{
TextBox textBox = (TextBox)sender;
if (textBox.Text.IndexOf('.') > -1 &&
textBox.Text.Substring(textBox.Text.IndexOf('.')).Length >= dPlaces + 1)
{
e.Handled = true;
}
}
}
}
Its because of Logical operation in your IF BLOCK for comparing length =3 and char = '.'.
Change that last part of your code with : (EDIT : To handle the issue of inserting text before '.')
if (!char.IsControl(e.KeyChar))
{
TextBox textBox = (TextBox)sender;
// get position of new char to be inserted
int position = textBox.SelectionStart;
if (textBox.Text.IndexOf('.') > -1 && position > textBox.Text.IndexOf('.')) // check location of new char
if(!(textBox.Text.Substring(textBox.Text.IndexOf('.')).Length <= dPlaces + 1))
{
e.Handled = true;
}
}
This will do your job..!!!
EDIT : Also Do the following to STOP Copy/Past in textbox
textbox.ShortcutsEnabled = false;
I created a textbox dynamically with a TextChangedEventArgs to restrict the textbox to enter only numbers and decimal point.
Following is the code in c#
const char Delete = (char)8;
if (Char.IsDigit(e.KeyChar))
{
e.Handled = false;
}
else if (e.KeyChar == Delete)
{
e.Handled = false;
}
else if (e.KeyChar == '.')
{
if (!(amt.Text.Contains(".")))
e.Handled = false;
else
{
e.Handled = true;
}
}
else
{
e.Handled = true;
}
But I can't use this in wpf.
I tried to change the code with e.key or e.Text. But both these are not available. It is showing the following error Are u missing an assembly or directive.
Please anyone help me.
// one solution for filtering characters in a textbox.
// this is the PreviewKeyDown handler for a textbox named tbNumerical
// Need to add logic for cancelling repeated decimal point and minus sign
// or possible notation like 1.23e2 == 123
private void tbNumerical_PreviewKeyDown(object sender, KeyEventArgs e)
{
System.Windows.Input.Key k = e.Key;
// to see the key enums displayed, use a textbox or label
// someTextBox.Text = k.ToString();
// filter out control keys, not all are used, add more as needed
bool controlKeyIsDown = Keyboard.IsKeyDown(Key.LeftShift);
if (!controlKeyIsDown &&
Key.D0 <= k && k <= Key.D9 ||
Key.NumPad0 <= k && k <= Key.NumPad9 ||
k == Key.OemMinus || k == Key.Subtract ||
k == Key.Decimal || k == Key.OemPeriod) // or OemComma for european decimal point
else
{
e.Handled = true;
// just a little sound effect for wrong key pressed
System.Media.SystemSound ss = System.Media.SystemSounds.Beep;
ss.Play();
}
}
I got 2 datagrid views, First datagrid 1st col or Index 0 & the Second datagrid 1st col or Index 0.
How can i loop through a specific col in the datagridviews and look for the string values,if it matches with the list then go to a function.
My approach is not working. How can i do this?
private void b_calculate_Click(object sender, EventArgs e)
{
List<string> value = new List<String>() { "AE0", "AT1", "AT2", "AT3"};
value = new List<string>(datagridview1.Columns[0].Index);
List<string> value2 = new List<String>() { "BE0", "BT1", "BT2", "BT3"};
value2 = new List<string>(datagridview2.Columns[0].Index);
//First Combination
if((value.ToString() == "AT1" || value.ToString() == "AE0" ||
value.ToString() == "AT2")
&&
(value2.ToString() == "BT1" || value2.ToString() == "BE0"))
{
gottoFunction1();
}
//Second Combination
if((value.ToString() == "AT1" || value.ToString() == "AT2" )
&&
(value2.ToString() == "BT1" || value2.ToString() == "BT2"))
{
gottoFunction2();
}
}
Here's a routine that iterates through all available rows in both DataGridViews and does the processing shown in your method:
private void b_calculate_Click(object sender, EventArgs e)
{
for (var i = 0; i < dataGridView1.RowCount; i++)
{
if (dataGridView2.RowCount <= i)
break;
var cellFromDG1 = dataGridView1.Rows[i].Cells[0];
var cellFromDG2 = dataGridView1.Rows[i].Cells[0];
if (cellFromDG1.Value == null || cellFromDG2.Value == null)
{
// this could be the empty row that allows you to
// enter a new record
continue;
}
var value = cellFromDG1.Value.ToString();
var value2 = cellFromDG2.Value.ToString();
if ((value == "AT1" || value == "AE0" || value == "AT2") &&
(value2 == "BT1" || value2 == "BE0"))
{
gottoFunction1();
}
if ((value == "AT1" || value == "AT2") &&
(value2 == "BT1" || value2 == "BT2"))
{
gottoFunction2();
}
}
}
I feel like I am just missing a simple property, but can you set the cursor to the end of a line in a textbox?
private void txtNumbersOnly_KeyPress(object sender, KeyPressEventArgs e)
{
if (Char.IsDigit(e.KeyChar) || e.KeyChar == '\b' || e.KeyChar == '.' || e.KeyChar == '-')
{
TextBox t = (TextBox)sender;
bool bHandled = false;
_sCurrentTemp += e.KeyChar;
if (_sCurrentTemp.Length > 0 && e.KeyChar == '-')
{
// '-' only allowed as first char
bHandled = true;
}
if (_sCurrentTemp.StartsWith(Convert.ToString('.')))
{
// add '0' in front of decimal point
t.Text = string.Empty;
t.Text = '0' + _sCurrentTemp;
_sCurrentTemp = t.Text;
bHandled = true;
}
e.Handled = bHandled;
}
After testing for '.' as first char, the cursor goes before the text that is added. So instead of "0.123", the results are "1230." without moving the cursor myself.
I also apologize if this is a duplicate question.
t.SelectionStart = t.Text.Length;
in WPF you should use :
textBox.Select(textBox.Text.Length,0);
where 0 is the number of characters selected
Setting the SelectionStart property on the textbox will control the cursor position.
Assuming you're using WinForms and not WPF...
void SetToEndOfLine(TextBox tb, int line)
{
int loc = 0;
for (int x = 0; x < tb.Lines.Length && tb <= line; x++)
{
loc += tb.Lines[x].Length;
}
tb.SelectionStart = loc;
}
This will usefull.
private void textBox_TextChanged(object sender, EventArgs e)
{
string c = "";
string d = "0123456789.";
foreach (char a in textBox.Text)
{
if (d.Contains(a))
c += a;
}
textBox.Text = c;
textBox.SelectionStart = textBox.Text.Length;
}