bharathi
bharathi

Reputation: 6271

Remove the Selection of text in textBox in C#

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

Upvotes: 0

Views: 2972

Answers (4)

Jalal Said
Jalal Said

Reputation: 16162

Answering your question that in the title: To remove a selected text from a TextBox, use: myTextBox.Text = myTextBox1.Text.Substring(myTextBox1.SelectionStart, myTextBox1.SelectionLength);

If you want to prevent the user from selecting a text:

  1. Use Label instead of the TextBox.
  2. Handle the label.KeyPress event and then add only allowed text and your custom functionality that you want at it.

Upvotes: 1

Vano Maisuradze
Vano Maisuradze

Reputation: 5899

You need to set SelectionLength to 0 on textbox KeyPress, MouseUp and MouseMove events.

Upvotes: 0

scwagner
scwagner

Reputation: 4005

You will want to watch the TextChanged event -- that will let you know whenever the contents of the textbox have changed. Rather than checking to see if the length is 0, you can use the TrimStart() method suggested by user623879.

When you say remove the selection, do you mean remove the text that the user has selected? If you want to just make the selection "go away", you can set the SelectionLength property to 0.

Upvotes: 0

user623879
user623879

Reputation: 4142

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

Upvotes: 0

Related Questions