I have a textbox.In that I have to allow the user to type some thing in that textbox and i also restrict the user not to select the typed text in that textbox.I have need not to allow the white space at the start of the text box and also i have need not allow the user to type more that 32 characters.Thsi is my code.
private void txtApplication_Title_KeyPress(object sender, KeyPressEventArgs e)
{
txtApplication_Title.Text = txtApplication_Title.Text.Remove(txtApplication_Title.SelectionStart, txtApplication_Title.SelectionLength);
if (txtApplication_Title.Text.Length== 0)
{
e.Handled = (e.KeyChar == (char)Keys.Space);
}
int count_charac = txtApplication_Title.Text.Length + 1 ;
if (count_charac > 32)
{
lblApplication_name.Text = data_variables.RES_TXT_STRING_EXCEEDING_APPLICATION_NAME;
timer1.Interval = 7000;
timer1.Enabled = true;
timer1.Tick += new System.EventHandler(OnTimerEvent_Application_name);
}
}
public void OnTimerEvent_Application_name(object sender, EventArgs e)
{
lblApplication_name.Text = " ";
timer1.Dispose();
}
Here in this code.I am able to restrict the user to 32 characters.And i try to press space at the start it does not allow me.It allows the selection of text using Shift+Arrow key.
I do know how to block the selection.Another thing is suppose I try
Hello and I select Hell and pressing the space directly it starts allowing the space in the text box.Can any one help me.Thanks in advance
Instead of KeyPress event, maybe use the textchanged event.
Text box has a max length attribute. Also, just call TextBox.Text=TextBox.Text.TrimStart() will remove any whitespace at beginning