I just got started programming for Windows Phone 7 after programming on Android for about half a year now. In Android, when I wanted textual input from a user, I would put a "hint" in the text box which would tell the user what they should input. When the text box was selected the hint would disappear. So far I've only seen ways to set text inside text boxes. The problem with this is that the text does not disappear when the user selects the text box forcing the user to erase the currently existing text.
I did some Google searches and skimmed through relevant documentation as well as a book I have but I've not found an answer yet. Thank you very much in advance for your time answering my question.
What you're looking for is normally called a "Watermarked" textbox. It's quite simple to create a control which implements this functionality.
Here are links to a few versions of implementations of this:
http://blogs.msdn.com/b/arun/archive/2010/03/29/watermarked-text-box-for-windows-phone.aspx
http://www.windowsphonegeek.com/articles/WP7-WatermarkedTextBox-custom-control
http://www.danielmoth.com/Blog/Watermark-TextBox-For-Windows-Phone.aspx
http://weblogs.asp.net/jdanforth/archive/2010/09/17/silverlight-watermark-textbox-behavior.aspx
WatermarkedTextBox for Windows Phone 7?
http://www.silverlight-zone.com/2011/03/wp7-watermarkedtextbox-custom-control.html
http://watermarktextbox.codeplex.com/
As I understand you want something similar to the default text in the searchbox (upper right corner of stackoverflow.com).
You have a few options.
Bool to check if user press the TextBox for the first time.
private bool m_textBoxPressedFirstTime = false;
and inside the event (double click the TextBox)
if(!m_textBoxPressedFirstTime)
{
myTextBox.Text = String.Empty;
m_textBoxPressedFirstTime = true;
}
Define your own template with a diffrent visual state.
You could use some WPF examples
How do you implement default text for a search box in WPF?
Related
I am using Visual Studio 2019 in order to create a Windows Form Application. I need some titles in my application, which means these strings will not be modified by the user.
For now, I created textboxes for these titles and made these textboxes "read-only". However, this does not satisfy my aesthetical expectations.
Therefore, I wonder if there is a way to add a string without adding a textbox, to the form. Is there a way?
Thanks in advance :)
Consider using a Label control rather than a TextBox.
The only time I would use a TextBox as a label is if I want the user to be able to copy the info, and I make it borderless, readonly and have the same colour as the background of the form. It's not superb UX though as there isn't anything that screams "you can highlight and copy this text" other than an I beam cursor, which is pretty much "mystery meat navigation" - better off putting a copy button next to it if you expect the user to copy info often
Why not use Label for your titles?
Since label, by default, cannot be modified by the user, thats what you want. Textbox is used for the user input, not for the titles.
Use Label control that is the right control to use for your requirement
I have two questions on a related problem. I combine them in one thread because I think separating them may result in dilutions. Here are the two problems:
Can I add a NEXT button (looks like an arrow) instead of an ENTER button normally, and, if yes, how can I do that?
How can I make a 'float' TextBox that goesup with the virtual keyboard when
I tap to TextBox (i.e. it looks like a 'chat space' of OTT apps, where we make the content to send, which always floats with the virtual keyboard and on the top of the keyboard)?
I really need your help; please help me! Thanks!
The answers:
You cannot customize the virtual keyboard.
you can move the textbox by setting margin in the InputPane(Showing & Hiding) events.
Hope that helps.
This question already has answers here:
Watermark in System.Windows.Forms.TextBox
(2 answers)
Watermark TextBox in WinForms
(11 answers)
Closed 9 years ago.
I am new to C#. I have a very simple Windows Forms Application that has a text Box, Label and a button. I got the user to enter their name in the text box and press the button and their name displays on the label.
Now I want to make the application more user friendly, so before the user enters their name in the text box, I want the text "Enter your name: " to appear in the text box but once the user click inside the text box (to enter their name), I want the "instruction" to disappear from the text box. Are there any built in functions that does that in C#?
What you are looking for, I think, is a watermark functionality you often see on web sites nowadays. See this SO question for multiple ways to do it:
Watermark in System.Windows.Forms.TextBox
However this is not beginner material by far. There is no built-in functionality for this behaviour in C#, so there is no 'easy' way to do it.
On the UI Designer mode, goto the textbox properties and type in the "enter your name" into the 'Text' Property,
this will then show 'enter your name' in your textbox
Over more, if you think this is not suited, you may want to look into tooltips, these are easy to create and use and can be displayed when the end user hovers over the textbox
What you're describing is a watermarked text box. I don't believe Winforms has a built-in one, but there are free solutions available. Maybe start here.
You'll be handling the click and focus event which is not a good way to go. In fact, just for something so simple, you'll end up with very complex code. And later on, if you wish to implement validations, your "instruction" text will become rather a pain.
I'm looking for a way to highlight (not select) words based on search terms inside a Windows.UI.Xaml.Controls.TextBox control.
There doesn't seem to any way to override the text rendering behaviour? Can this be done with the textbox control?
Edit:
I was originally trying to use the RichEditBox but was having a problem with being able to paste in formatted text which I was trying to prevent (the only event I can clear the formatting on is TextChanged which seems a little late). I also really need more control over the rendering of the highlights
Textboxes are very limited in regards to formatting and text selection.
You might want to use the RichTextBox control rather than a TextBox as it gives you greater selection and formatting capabilities to make highlighting multiple terms easier.
Here's a quick start: http://www.devx.com/dotnet/Article/34644
I am having the same issue. But Incase you didn't know, I'll post this anyway (it's not a complete answer but might get you started.)
You can highlight words by using:
int indexOfFoundKeyword = 1;
int lastIndexOfFoundKeyword = 12;
txtbox.SelectionStart = indexOfFoundKeyword;
txtbox.SelectionLength = lastIndexOfFoundKeyword;
If you place this inside a button click event and then type about 20 chars and click the button you'll see that you can highlight words in a Metro textbox. The problem that I'm having is getting the start and ending of the found keyword so that I know what values to assign to SelectionStart and SelectionLength and if I understand your question correctly, I think that's where you're stuck at, too.
Update
OK, this seems to be a little tempremental but works most of the time. (and accurate when it does work):
// find the word 'jason'.
string word = "jason";
int a, b;
a = genericBox.Text.IndexOf(word);
b = word.Length;
genericBox.SelectionStart = a;
genericBox.SelectionLength = b;
If all you are trying to do is find some text, then select it, then this proves that it can be done. Now I know this is not the best way but it's the only way I've been able to get something like this to work in a plain TextBox for metro apps.
Perhaps you could create a control based on the TextBox control or altogether create a custom control. See here and here for more help.
I want to implement a search box in a window form. In that window form, I have a few botton and a text box.
I want to support the use case when user enter a string in the search box and then we can find the that string and highlighted like firefox does. Is it hard to do this?
I googled and found this link that has the search box control. but I don't quite understand the code. If anyone is familiar with control.sendMessage, Could you please give me some help on understand that control.
here is the link:
http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/a07c453a-c5dd-40ed-8895-6615cc808d91/
Thanks
There is no single WinForms or Windows control that provides this functionality. You need to break the problem down into parts:
1) Create a search box
I believe the link you give adds the "Search" cue to a textbox but doesn't add the search button(?) - if that is the case you'll want to combine the textbox with a new button in a user control.
SendMessage sends a message to a Windows control or Window. In this case it tells the textbox to display the "Search" cue. You need to do this because this behaviour is not exposed by the WinForms controls.
2) Work out how to highlight sections of the text
If you are just using the WinForms controls you'll need to use a RichTextBox control and work out how to change the background color at various points in the text.