How to highlight text in a text box in a C # Winforms program?

I have a C # Winforms program with several text fields. I used properties for each field to place text in them, explaining to the user what value is included in them. I want the text to be highlighted whenever the user selects this field. Using tabs or mouse clicks. I don’t have to do this if there is a way to show what the value will be in the text box somewhere outside.

I tried the Textbox.select method, but this did not affect. Same thing with that .

Here is a screenshot of my program.

My code is:

private void grapplingText1_MaskInputRejected(object sender, MaskInputRejectedEventArgs e) { grapplingText1.SelectionStart = 0; grapplingText1.SelectionLength = grapplingText1.Text.Length; 

Will it do it or is it needed more?

+6
c # highlight winforms textbox
source share
7 answers

How about assigning a ToolTip to a TextBox and putting all the "talk what textbox" in it?

Just drag the ToolTip inside the form. And then in each TextBox property you should have an additional entry in the Misc ToolTip on toolTip1 (or whatever that name would be if you renamed it).

Then, when the user hovers over the TextBox (Read-Only / Disabled TextBox , the tooltip doesn't seem to be displayed) and stops there for 1 second, the ToolTip should show with the corresponding information.

You may, in the end or even better, have a Label next to the TextBox saying that it is, but having ToolTip also a good idea to explain additional information to the user.

To work with WaterMark you do not need to go a long way, setting events, taking care of SelectAll, etc., you can do it like this. Create a new watermark.cs file and replace it with this code. Make sure you change the namespace to match the program namespace.

 #region using System; using System.Runtime.InteropServices; using System.Windows.Forms; #endregion namespace Watermark { public static class TextBoxWatermarkExtensionMethod { private const uint ECM_FIRST = 0x1500; private const uint EM_SETCUEBANNER = ECM_FIRST + 1; [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)] private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, uint wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam); public static void SetWatermark(this TextBox textBox, string watermarkText) { SendMessage(textBox.Handle, EM_SETCUEBANNER, 0, watermarkText); } } } internal class WatermarkTextBox : TextBox { private const uint ECM_FIRST = 0x1500; private const uint EM_SETCUEBANNER = ECM_FIRST + 1; private string watermarkText; public string WatermarkText { get { return watermarkText; } set { watermarkText = value; SetWatermark(watermarkText); } } [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)] private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, uint wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam); private void SetWatermark(string watermarkText) { SendMessage(Handle, EM_SETCUEBANNER, 0, watermarkText); } } 

In your form, you activate it as follows:

 textBoxYourWhatever.SetWatermark("Text that should display"); 

It stays there while the TextBox empty. When users get into the TextBox , the text disappears. It appears again when the TextBox is cleared (by the user or automatically). No special events needed, etc.

EDIT:

I also added an inner WaterMarkTextBox class that gives you the ability to simply use the new WaterMarkTexBox, which is made available in Designer. Then drag it to your designer and use it. It behaves like a regular text block, just giving you the optional WaterMarkText field.

I still prefer the first method. Doesn't make you rebuild your gui again.

+8
source share

I think .select will work if you know the amount of text you want to select.

Try .SelectAll (); It should work for you.

+7
source share

You need to use TextBox.Focus () to focus on your control, and if it doesn't work automatically, just call the SelectAll () method in the Enter () event.

 private TextBox1_Enter(object sender, EventArgs e) { TextBoxTextHighlight(sender, null); } private TextBox2_Enter(object sender, EventArgs e) { TextBoxTextHighlight(sender, null); } private TextBox3_Enter(object sender, EventArgs e) { TextBoxTextHighlight(sender, null); } // And so forth... private void TextBoxTextHighlight(object sender, EventArgs e) { (sender as TextBox).SelectAll(); } 

This method allows you to call it from any TextBoxX_Enter () event handler.

Otherwise, you could even create a new UserControl, call it the way you want, when you create it, then when you create it in your project, edit the code and replace the inheritance of the UserControl class with the TextBox class, then define the default behavior in it. which you would like to have in the Enter () event, like this call to the SelectAll () method, and make it protected virtual, and inside the constructor you can subscribe to the event handler as follows:

 public partial class CustomTextBox : TextBox { public CustomTextBox() : base() { this.Enter += new EventHandler(Enter); } protected virtual Enter(object sender, EventArgs e) { this.SelectAll(); } } 

I wrote it on the fly, so it may take a few modifications, but you can get this idea. But I do not advise you to do this if it really is not worthy to do it for your correct situation. The simpler, the simpler and simpler to create an event handler for each of the TextBox forms, and then call the TextBoxTextHighlight () method.

+3
source share

Suppose your text name is "MyTextBox"

You can write a method for handling the Focus event:

 private void MyTextBox_GotFocus(object sender, EventArgs e) { MyTextBox.SelectionStart = 0; MyTextBox.SelectionLength = MyTextBox.Text.Length; MyTextBox.Select(); } 

You will also need to add an event handler:

 this.MyTextBox.GotFocus += new System.EventHandler(this.MyTextBox_GotFocus); 

This should work ...

EDIT

You can use the same method for another text field, just add an event handler:

 this.MyTextBox2.GotFocus += new System.EventHandler(this.MyTextBox_GotFocus); this.MyTextBox3.GotFocus += new System.EventHandler(this.MyTextBox_GotFocus); this.MyTextBox4.GotFocus += new System.EventHandler(this.MyTextBox_GotFocus); //... 
+2
source share

Typically, if you want to explain to the user what is going on in the text field, you place the Label directly above it (or from the side).

Sometimes, to preserve screen real estate, you can put a description of the text inside the text field itself. This is called a watermark .

+2
source share

The easiest way to show some additional information when you hover over an element is to use a tooltip. I tried a similar watermarked approach, but not finding there an immediate way to implement it, a tooltip seemed like a suitable alternative.

You can see how to implement it at the following link: Implementing a tooltip

0
source share

I found that in my application, binding the selection method to the Focus β†’ Enter event does not work very well with the SelectAll () method. Instead, I used Action -> Click (which I believe works only for mice) and added my selection method. Now it works like a charm.

-one
source share

All Articles