How to create a watermarked textbox? [duplicate] - c#

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.

Related

How to change the '...' text on the button of UITypeEditor? [duplicate]

I have a Windows forms PropertyGrid and a customer UITypeEditor per http://msdn.microsoft.com/en-us/library/system.drawing.design.uitypeeditoreditstyle.aspx
Displays an ellipsis (...) button to start a modal dialog box, which requires user input before continuing a program, or a modeless dialog box, which stays on the screen and is available for use at any time but permits other user activities.
That ellipsis button is pretty small. How can I relabel it with a more helpful label?
Long story short, you can't. The basic MS PropertyGrid does not allow you to customize this text. It even uses a bitmap with 3 dots instead of text anyway and the width of the control is fixed.
Disclaimer: I'm the developer behind Smart PropertyGrid.Net, which can do what you want to achieve.

How Can I Select One Word In TextBox and change its color in C# [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
C# How can I set the color of text in a richtextbox?
In Visual C#.NET, I want to change the color of specific text when it is typed in to a rich text box.
For example, I might want to color the world "hi", so when the user is typing in the rich text box, if they type "hi" it would change colors and the word "hi" would be a different color from all the other text in the rich text box.
How would I do that? Is there a better way to do that, not in a rich textbox?
in your TextChanged Event handler
txtRichTextBox.Select(txtRichTextBox.Text.IndexOf("hi"), "hi".Length);
txtRichTextBox.SelectionColor = YourColor;
txtRichTextBox.SelectionFont = new Font("Times New Roman",FontStyle.Bold);
I remember doing something like this in Flex. We had to extend the richtextbox control, and make our own custom control, that would change the text color depending on what was being entered. We used data change events to achieve this. But my gut guess is you'll have to create a custom control to do this.

Is it possible to place "hints" in Windows Phone 7 TextBoxes?

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?

how to implement a search box in c#

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.

.NET (C#) winforms "tweet" UI control question [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm a real dummy at UI building, so I'm wondering - what .NET winforms control would be best to use if aiming to display tweets in a list fashion that updates with like boxes which contain the text of the tweet and its metadata below it. Is there a specific winforms list function for this type of thing? Or do you recommend I look for/use a third party winforms control? Thanks! Hope my question wasn't too silly.
UPDATE: I am getting indication from numerous responses that it would be better to just build a simple winform control myself, but can anyone point me to a tutorial for doing so? Also, if i were to build such a control, does there exist a convenient built-in control for making like a list of custom controls that is scrollable - maybe Panel? Thanks in advance!
I'd build a user control that would display the information for a single tweet. I'd then repeat that user control in a panel.
You could just use the same layout in your user control that is standard for "tweets".
Left justified picture box to display the user image.
LinkLabel for user name.
Label for tweet text.
Update: Here are a couple pages on UserControls
http://msdn.microsoft.com/en-us/library/a6h7e207(VS.71).aspx
http://www.akadia.com/services/dotnet_user_controls.html
Create a usercontrol using the designer and also a Tweet class that is
just a dumb data structure to hold each tweet's information. Also create a Tweet
property on the custom user control that would handle setting the tweet and assigning information
to the standard controls contained in it (Labels, Textboxs, PictureBox, etc).
In the form where you want to host your user control create a panel that is empty. Somewhere in your code you
would do something similar to this code block.
pnlHost.Controls.Clear();
List<Tweet> tweets = new List<Tweet>();
//Populate your collection of tweets here
foreach (Tweet tweet in tweets)
{
TweetControl control = new TweetControl();
control.Tweet = tweet;
pnlHost.Controls.Add(control);
control.Dock = DockStyle.Top;
}
Hi I'd go for a third party grid control from companies like Infragistics, DevExpress or Telerik. Another option would be to build a usercontrol that is able to display one post and put that control into a vb.net repeater control (which ships with the vb.net powerpack). However I'm not sure how good the last solution actually works, as I just read about it, but never tried it.
Try using DataGridView (and a DataTable behind it) and overwrite the CellPainting event to draw the tweets the way you like it.
Edit
I think using a custom control is a bit heavy just to display the tweets. Basically this depends on the desired functionality of the displayed tweets - what would you like to do with them. For example, if you just want the user to click the tweet and then redirect him to some webpage in the browser then the DataGridView will do just fine since you can attach this action to the CellClick event. Plus you already have the sorting/searching capabilities of the DataTable (and the related DefaultView)behind it. Hell, you can even attach a ContextMenuStrip to the tweet and have unlimited options with the RightClick.
On the other hand if you need some functionality that is not available in the DataGridView (for example, you would like 5 different buttons at the bottom of the tweet and 3 checkboxes on the right) then the custom control would do you better because you could build practically everything you wanted. In this case OG explains this nicely.
Try listbox, it's easy to use and seems like it will suit your needs.

Categories