0

I maybe can't explain this problem properly but let's say that my TextBox has a Text inside "MM/DD/YYYY" that was edited on its Properties. I want to know how will the TextBox go back to that Text again once it was not selected. I tried coding it for once the TextBox is Click the Text will clear itself but I want to know how will it go back to that Text again. Sorry my English sucks but hope you understand.

 Private Sub birthdate_Click(sender As Object, e As EventArgs) Handles birthdate.Click
    birthdate.Clear()
End Sub

Here's the code when I click the TextBox it will clear the Text itself. This will give you idea of what I'm saying

Jimi
  • 29,621
  • 8
  • 43
  • 61
Mine Dyse
  • 1
  • 4
  • There are various examples about of custom `TextBox` controls that add that functionality. [Here](https://www.mking.net/blog/cue-banners-in-winforms) is one in C# that can be easily converted to VB. I would suggest installing [this](https://www.tangiblesoftwaresolutions.com/product_details/csharp-to-vb-converter.html) to do the conversion. – jmcilhinney Jan 18 '20 at 13:28
  • Does this answer your question? [watermark Text-box Align Center](https://stackoverflow.com/questions/41485621/watermark-text-box-align-center). See accepted answer for all the detail you need to enable the Cue Banner feature on Windows platforms. – Peter Duniho Oct 28 '20 at 05:24
  • Does this answer your question? [Placeholder in TextBox in Window Forms using VB.NET](https://stackoverflow.com/questions/50924572/placeholder-in-textbox-in-window-forms-using-vb-net) – Peter Duniho Oct 28 '20 at 05:28
  • Does this answer your question? [Watermark TextBox in WinForms](https://stackoverflow.com/questions/4902565/watermark-textbox-in-winforms) – Peter Duniho Oct 28 '20 at 05:29

1 Answers1

2

This is an extension module that adds a method, to both TextBox and ComboBox Controls, that activates the Cue Banner1 internal functionality. For the TextBox, in the two available flavors: the Cue Banner is visible: a) until the control gets focus, b) after the first char is entered.
The ComboBox control removes the Cue Banner as soon as it receives focus or the DropDown is opened.

The Win32 function SendMessage is used modify the behavior of the Edit control - the TextBox control and the Edit child control of a ComboBox, sending the EM_SETCUEBANNER and CB_SETCUEBANNER messages, respectively.

1 Sets the textual cue, or tip, that is displayed by the edit control to prompt the user for information.


Since these are extension methods, each method extends the controls functionality and can be called directly:

' The Cue Banner is visible until the control gets focus
TextBox1.SetCueBanner("MM/DD/YYYY", False)

' The Cue Banner is visible until a character is entered
TextBox1.SetCueBanner("User Name...", True)

ComboBox1.SetCueBanner("Select an item...")

Add a Module to the Project and substitute the code generated by the template with this:

VB.Net version:

Public Module ModExtentions

    Private Const EM_SETCUEBANNER As Integer = &H1501
    Private Const CB_SETCUEBANNER As Integer = &H1703

    <DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
    Private Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As Integer, ByVal lParam As String) As Integer
    End Function

    <Extension()>
    Public Sub SetCueBanner(tbox As TextBox, ByVal text As String, ByVal showOnFocus As Boolean)
        If tbox.IsHandleCreated Then SendMessage(tbox.Handle, EM_SETCUEBANNER, If(showOnFocus, 1, 0), text)
    End Sub

    <Extension()>
    Public Sub SetCueBanner(cbo As ComboBox, ByVal text As String)
        If cbo.IsHandleCreated Then SendMessage(cbo.Handle, CB_SETCUEBANNER, 0, text)
    End Sub
End Module

C# Version:

using System.Runtime.InteropServices;
using System.Windows.Forms;

public static class ExtControls
{
    private const int EM_SETCUEBANNER = 0x1501;
    private const int CB_SETCUEBANNER = 0x1703;

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern int SendMessage(IntPtr hWnd, int msg, int wParam, string lParam);

    public static void SetCueBanner(this TextBox tbox, string text, bool showOnFocus) {
        if (tbox.IsHandleCreated) {
            SendMessage(tbox.Handle, EM_SETCUEBANNER, showOnFocus ? 1 : 0, text);
        }
    }

    public static void SetCueBanner(this ComboBox cbo, string text) {
        if (cbo.IsHandleCreated) {
            SendMessage(cbo.Handle, CB_SETCUEBANNER, 0, text);
        }
    }
}
Jimi
  • 29,621
  • 8
  • 43
  • 61