8 Replies - 5097 Views - Last Post: 11 November 2011 - 07:29 PM Rate Topic: -----

#1 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,689
  • Joined: 13-March 10

RichTextBox disable input

Posted 10 November 2011 - 09:41 AM

Im getting into grips with Winforms at the moment.
I have a Richtextbox and i want to disable the user input, but i dont want to do it through Enabled property.

Code:


 private void box_TextChanged(object sender, System.EventArgs e)
    {
        int currentNumber = MAX - box.Text.Length;
        countLabel.Text = "Left characters: " + currentNumber;
        if (currentNumber <= 0)
        {
            System.Console.Beep();
           // box.Enabled = false;   <-- does not do the trick
            
        }
   }



Is This A Good Question/Topic? 0
  • +

Replies To: RichTextBox disable input

#2 ragingben  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 170
  • View blog
  • Posts: 637
  • Joined: 07-October 08

Re: RichTextBox disable input

Posted 10 November 2011 - 09:44 AM

Setting its property ReadOnly should do the trick :)
Was This Post Helpful? 1
  • +
  • -

#3 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,689
  • Joined: 13-March 10

Re: RichTextBox disable input

Posted 10 November 2011 - 09:49 AM

Well.. in this case its not :P. The idea is: i have a box where the user types in some input. I have a MAX const that specifies number of characters that the user is able to input. if they type in the MAX value of characters, there should be a beep and no more input allowed BUT the box should be still enabled since they can press backspace in which case the amount of allowed characters is gonna go up.

countLabel is only there for displaying the number of allowed characters and is gonna be changing dynamically as the user types in.

Sorry for not being clear in the first place.

This post has been edited by darek9576: 10 November 2011 - 09:49 AM

Was This Post Helpful? 0
  • +
  • -

#4 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: RichTextBox disable input

Posted 10 November 2011 - 09:56 AM

In that case you will want to handle the .KeyPress event of the RichTextBox.

In the event handler check that the length of the text currently in the textbox is that of the maximum constant i.e - no more text can be inserted.

If the maximum length has been reached, set the .Handled property of the KeyPressEventArgs to true to stop the character from being added.

As a side not I just remembered that there is actually a MaxLength property in the Richtextbox. Just set that to the maxiumum length held in the constant.
Was This Post Helpful? 2
  • +
  • -

#5 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,689
  • Joined: 13-March 10

Re: RichTextBox disable input

Posted 10 November 2011 - 09:59 AM

Sweet. It worked. Thank you.
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5522
  • View blog
  • Posts: 11,830
  • Joined: 02-June 10

Re: RichTextBox disable input

Posted 10 November 2011 - 03:44 PM

SO what was wrong with just setting .enabled to false when you didn't want input? Why was that undesirable?
Was This Post Helpful? 0
  • +
  • -

#7 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,689
  • Joined: 13-March 10

Re: RichTextBox disable input

Posted 10 November 2011 - 06:08 PM

.enabled would not allow the user to press backspace.
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5522
  • View blog
  • Posts: 11,830
  • Joined: 02-June 10

Re: RichTextBox disable input

Posted 10 November 2011 - 08:14 PM

Ah - post 3, I missed that. Skimmed the thread when I should have read it more carefully.
Was This Post Helpful? 0
  • +
  • -

#9 giuseppe105  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 444
  • Joined: 15-May 08

Re: RichTextBox disable input

Posted 11 November 2011 - 07:29 PM

i know this question has already been solved

but to help people who come by the forums searching for an example here is one.

this is what Ryano121 said about killing the key

this example only allows the backspace key to be pressed

private void Form1_Load(object sender, EventArgs e)
{
     richTextBox1.KeyPress += new KeyPressEventHandler(richTextBox1_KeyPress);
}

void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
     int c = e.KeyChar;
     if (c != 8)
     {
         e.Handled = true;
     }
}



This post has been edited by giuseppe105: 11 November 2011 - 07:33 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1