8 Replies - 804 Views - Last Post: 08 February 2013 - 07:26 AM Rate Topic: -----

#1 vbabey  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 30-July 12

Set a selected text in a richTextBox

Posted 07 February 2013 - 10:36 PM

I want to create a richTextBox which shows the text "Enter your text here..." and it has to be a selected text and once I type on the text box it should vanish and the entered text should have to be shown, I am new to C#...can you help me.I worked with some java stuff..but I can't get to it here...
Is This A Good Question/Topic? 0
  • +

Replies To: Set a selected text in a richTextBox

#2 oyyou  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 184
  • Joined: 26-April 10

Re: Set a selected text in a richTextBox

Posted 08 February 2013 - 05:30 AM

What you wanna do is create an event handler for when you text box has focus.
When it has focus, remove whatever is written inside of the text box. See below

private void txtUserInput_GotFocus(object sender, RoutedEventArgs e)
{
    txtUserInput.Text = string.Empty;
}



So here txtUserInput is the name of the text box and GotFocus is the event handler.
When the user selects the text box, all text will be removed.

I think that's what you're looking for.

- Oyy

This post has been edited by oyyou: 08 February 2013 - 05:31 AM

Was This Post Helpful? 2
  • +
  • -

#3 Michael26  Icon User is online

  • DIC-head, major DIC-head
  • member icon

Reputation: 355
  • View blog
  • Posts: 1,518
  • Joined: 08-April 09

Re: Set a selected text in a richTextBox

Posted 08 February 2013 - 05:32 AM

set a click event for the richtextbox and in there type something like this richtextbox1.text = "";

this forum should notify user when the new message was posted

This post has been edited by Michael26: 08 February 2013 - 05:34 AM

Was This Post Helpful? 2
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3539
  • View blog
  • Posts: 10,952
  • Joined: 05-May 12

Re: Set a selected text in a richTextBox

Posted 08 February 2013 - 05:51 AM

But what happens when the user types something into the text box, and then tabs or clicks away, and then tabs or clicks back into the text box?

This post has been edited by Skydiver: 08 February 2013 - 05:51 AM

Was This Post Helpful? 1
  • +
  • -

#5 oyyou  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 184
  • Joined: 26-April 10

Re: Set a selected text in a richTextBox

Posted 08 February 2013 - 06:00 AM

View PostSkydiver, on 08 February 2013 - 05:51 AM, said:

But what happens when the user types something into the text box, and then tabs or clicks away, and then tabs or clicks back into the text box?


The text would go.

I guess there could be a bool going on.
bool entered = false;
private void txtUserInput_GotFocus(object sender, RoutedEventArgs e)
{
    if(!entered)
        txtUserInput.Text = string.Empty;

    entered = true;
}



That could work.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3539
  • View blog
  • Posts: 10,952
  • Joined: 05-May 12

Re: Set a selected text in a richTextBox

Posted 08 February 2013 - 06:25 AM

And if the user never types anything into the textbox? It'll still be empty.

I'm asking this series of questions because I've had to fight with this set of issues before and they are cascading set of problems that in the end result in about 4-8 hours of work for a feature that is a bit of visual flash. If you've got time budgeted to work on the feature, more power to you. If you don't have time budgeted for it, get the rest of your program running first, and then come back to this later if you have extra time.
Was This Post Helpful? 2
  • +
  • -

#7 teyrebaz  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 14-January 12

Re: Set a selected text in a richTextBox

Posted 08 February 2013 - 06:26 AM

        private void Form1_Load(object sender, EventArgs e)
        {
            richTextBox1.Text = "Enter your text here...";
            richTextBox1.Select(0, richTextBox1.TextLength);
            richTextBox1.SelectionBackColor = Color.Red;

        }

        private void richTextBox1_Enter(object sender, EventArgs e)
        {
            richTextBox1.Text = "";

        }


This will solve your problem, i guess at least if i got your point.
Was This Post Helpful? 1
  • +
  • -

#8 vbabey  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 30-July 12

Re: Set a selected text in a richTextBox

Posted 08 February 2013 - 06:42 AM

Thank you very much guys for this help...btw skydiver...he says something which is very important.
Sometimes I go for other most important stuff and come back if enough time is left. That is great. A good lesson for a programmer to save his/her time. Thank you very much. I gathered what I needed. Thanks.
Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq  Icon User is offline

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

Reputation: 5469
  • View blog
  • Posts: 11,749
  • Joined: 02-June 10

Re: Set a selected text in a richTextBox

Posted 08 February 2013 - 07:26 AM

Alternatively if you leave behind doing this in Winforms (which Microsoft is leaving behind) and do this in WPF then you can simply use the WaterMarkTextBox

My suggestion to all rookies coders these days is to just go straight to WPF if its an option. All of this is equally new to you. So why invest 10,000 hours becoming really proficient with WinForms as it will leave you that much further behind the curve? You need WPF for Windows 8 (Metro) apps and WinPhone7/8 anyway. So just start with today's technology not yesterdays.
Was This Post Helpful? 3
  • +
  • -

Page 1 of 1