As you have not described briefly and if I am not wrong you wanted to trim the spaces at beginning right ?
then my answer is, you can handle it many way and one possible way is to handle this in the following way also. Some sample code I have written here in below, you can check with that, its running perfectly in my sample application:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if ((textBox1.SelectionStart == 0) && (e.KeyChar == (char)Keys.Space))
{
e.Handled = true;
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
//Store the back up of Current Cursor Possition.
int cusorpos = textBox1.SelectionStart;
if (false == string.IsNullOrEmpty(textBox1.Text))
{
if (textBox1.Text[0] == ' ')
{
//Trim Spaces at beginning.
textBox1.Text = textBox1.Text.TrimStart(' ');
//Set the Cursor position to current Position.
textBox1.SelectionStart = cusorpos;
}
}
}
as you can see here i wrote two events its because if any body paste a text with spaces at beginning, in your textbox control then it will work perfectly to remove the spaces from the beginning.