none
Displaying some guidance in the textbox RRS feed

  • Question

  • I need to display some guidance message, ie "Text here!", in the textbox during input text is null string. 

    If user enter some text, that guidance message should disappear.

    How to do this?

    Saturday, July 6, 2019 7:41 PM

All replies

  • Hello,

    There are many flavors of this, here is mine.

    • Compile the following class
    • Once compiled it will be in the toolbox when you have the form showing double click on the new text box to add it to the form..
    • Set the text for when there is no text via the image shown below or in the code block below.

    In code

    cueTextBox1.CueBannerText = "Enter some text";

    The custom TextBox.

    [ToolboxBitmap(typeof(CueTextBox))]
    public class CueTextBox : System.Windows.Forms.TextBox
    {
        private const int EM_SETCUEBANNER = 0x1501;
        private const int EM_GETCUEBANNER = 0x1502;
    
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern Int32 SendMessage(
            IntPtr hWnd, 
            int msg, 
            int wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam);
    
        private string _cueBannerText = string.Empty;
    
        /// <summary>
        /// Gets or sets the cue text that is displayed on the TextBox control.
        /// </summary>
        [Description("Text that is displayed as Cue banner."), Category("Appearance"), DefaultValue("")]
        public string CueBannerText
        {
            get => _cueBannerText;
            set
            {
                _cueBannerText = value;
                UpdateControl();
            }
        }
    
        private bool _showCueFocused = false;
    
        /// <summary>
        /// Gets or sets whether the Cue text should be displyed even
        /// when the control has keybord focus.
        /// </summary>
        /// <remarks>
        /// If true, the Cue text will disappear as soon as the user starts typing.
        /// </remarks>
        [Description("If true, the Cue text will be displayed even when the control has keyboard focus."), 
         Category("Appearance"), DefaultValue(false)]
        public bool ShowCueFocused
        {
            get => _showCueFocused;
            set
            {
                _showCueFocused = value;
                UpdateControl();
            }
        }
    
        private void UpdateControl()
        {
            if (IsHandleCreated)
            {
                SendMessage(Handle, (int)EM_SETCUEBANNER, (_showCueFocused) ? 1 : 0, _cueBannerText);
            }
        }
    
        protected override void OnHandleCreated(EventArgs e)
        {
            base.OnHandleCreated(e);
    
            UpdateControl();
        }
    
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, July 6, 2019 9:45 PM
    Moderator
  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer. So that it will help other members to find the solution quickly if they face the similar issue.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, July 11, 2019 5:42 AM
    Moderator