Can someone help me I want to prevent a starting point from being written as seen here.
I have this code with which I validate so that only numbers and a point enter, but I want to prevent the point from being in the first position.
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
DataGridViewTextBoxEditingControl dText = (DataGridViewTextBoxEditingControl)e.Control;
dText.KeyPress -= new KeyPressEventHandler(dText_KeyPress);
dText.KeyPress += new KeyPressEventHandler(dText_KeyPress);
}
void dText_KeyPress(object sender, KeyPressEventArgs e)
{
if (dataGridView1.CurrentCell.ColumnIndex == 1 || dataGridView1.CurrentCell.ColumnIndex == 2)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
{
e.Handled = true;
}
if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
{
e.Handled = true;
}
}
}
The condition
(sender as TextBox).Text.IndexOf('.') > -1
has a logical error, if there is no point in the textbox the result will be false and the instruction will not be executede.Handled = true;
and therefore it will accept the point in the first position.You can evaluate that the length of the field is equal to 0 so that it does not allow the entry of points in the first position