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...
Set a selected text in a richTextBox
Page 1 of 18 Replies - 341 Views - Last Post: 08 February 2013 - 07:26 AM
Replies To: Set a selected text in a richTextBox
#2
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
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
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
#3
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 forum should notify user when the new message was posted
This post has been edited by Michael26: 08 February 2013 - 05:34 AM
#4
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
#5
Re: Set a selected text in a richTextBox
Posted 08 February 2013 - 06:00 AM
Skydiver, 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.
#6
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.
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.
#7
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.
#8
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.
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.
#9
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.
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.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote





|