textbox placeholder gives error

cfrank2000

Well-known member
Joined
Mar 6, 2021
Messages
71
Programming Experience
Beginner
Hi
I try to use in c# form textbox a placeholder following a tutorial but I've got 6 error, can I get help? thank you.
toto2:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Totogui2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void NameText_Enter(object sender, EventArgs e)
        {
            if (NameText_Enter== "12X12X12X12X12")
            {
                NameText_Enter = "";
                NameText_ForeColor = Color.Black;

            }
        }
        private void NameText_Leave(object sender, EventArgs e)
        {
            if (NameText_Enter == "")
            {
                NameText_Enter = "12X12X12X12X12";
                NameText_ForeColor = Color.Silver;

            }
        }
        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
tutorial
 
Firstly, if you expect us to help you with errors then you need to provide us with the error messages and where they occur in the code.

That said, you should absolutely not be doing what you're doing the way you're doing it. If you want a prompt displayed when the TextBox is empty then that functionality is built into Windows. It's officially known as a cue banner and it doesn't interact with the Text property directly, so there's no handling of events to change the Text under specific circumstances. There are three main ways that you could implement this and I will present them in descending order of dodginess. Firstly, just put it straight into your form:
C#:
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern int SendMessage(
    IntPtr hWnd,
    int msg,
    int wParam,
    [MarshalAs(UnmanagedType.LPWStr)] string lParam);

private const int EM_SETCUEBANNER = 0x1501;

private void Form1_Load(object sender, EventArgs e)
{
    SendMessage(textBox1.Handle,
                EM_SETCUEBANNER,
                0,
                "Type Here");
}
Secondly, you could create an extension method:
C#:
public static class TextBoxExtensions
{
    private const int EM_SETCUEBANNER = 0x1501;

    public static void SetCueBannerText(this TextBox source, string text)
    {
        SendMessage(source.Handle,
                    EM_SETCUEBANNER,
                    0,
                    text);
    }

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern int SendMessage(
        IntPtr hWnd,
        int msg,
        int wParam,
        [MarshalAs(UnmanagedType.LPWStr)] string lParam);
}
and then call that in your form:
C#:
private void Form1_Load(object sender, EventArgs e)
{
    textBox1.SetCueBannerText("Type Here");
}
Finally, you can build it into a custom control:
C#:
public class CueBannerTextBox : TextBox
{
    private const int EM_SETCUEBANNER = 0x1501;

    private string cueBannerText;

    public event EventHandler CueBannerTextChanged;

    [Category("Appearance")]
    public string CueBannerText
    {
        get => cueBannerText;
        set
        {
            if (cueBannerText != value)
            {
                cueBannerText = value;

                SendMessage(Handle,
                            EM_SETCUEBANNER,
                            0,
                            cueBannerText);

                OnCueBannerTextChanged(EventArgs.Empty);
            }
        }
    }

    protected virtual void OnCueBannerTextChanged(EventArgs e)
    {
        CueBannerTextChanged?.Invoke(this, e);
    }

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern int SendMessage(
        IntPtr hWnd,
        int msg,
        int wParam,
        [MarshalAs(UnmanagedType.LPWStr)] string lParam);
}
and then add that to your form instead of a regular TextBox. Once you've added that class and built your project, the new control will appear in the Toolbox automatically, or you can build it into a library and then add it to the Toolbox for all projects. If you already have regular TextBoxes on the form and don't want to have to delete them, you can edit the design code file by handle and simply change the type in code. You will then have access to the CueBannerText property in the designer and, when it is set, the text will apear at design time too.

For the record, there is an EM_GETCUEBANNER message too, so you don't have to store the text in the control and you probably shouldn't, because it will be inaccurate if an EM_SETCUEBANNER message is sent externally. I just couldn't be bothered doing that here and now. Consider it am exercise for you if you make the right choice and go with the custom control.
 
Firstly, if you expect us to help you with errors then you need to provide us with the error messages and where they occur in the code.

That said, you should absolutely not be doing what you're doing the way you're doing it. If you want a prompt displayed when the TextBox is empty then that functionality is built into Windows. It's officially known as a cue banner and it doesn't interact with the Text property directly, so there's no handling of events to change the Text under specific circumstances. There are three main ways that you could implement this and I will present them in descending order of dodginess. Firstly, just put it straight into your form:
C#:
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern int SendMessage(
    IntPtr hWnd,
    int msg,
    int wParam,
    [MarshalAs(UnmanagedType.LPWStr)] string lParam);

private const int EM_SETCUEBANNER = 0x1501;

private void Form1_Load(object sender, EventArgs e)
{
    SendMessage(textBox1.Handle,
                EM_SETCUEBANNER,
                0,
                "Type Here");
}
Secondly, you could create an extension method:
C#:
public static class TextBoxExtensions
{
    private const int EM_SETCUEBANNER = 0x1501;

    public static void SetCueBannerText(this TextBox source, string text)
    {
        SendMessage(source.Handle,
                    EM_SETCUEBANNER,
                    0,
                    text);
    }

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern int SendMessage(
        IntPtr hWnd,
        int msg,
        int wParam,
        [MarshalAs(UnmanagedType.LPWStr)] string lParam);
}
and then call that in your form:
C#:
private void Form1_Load(object sender, EventArgs e)
{
    textBox1.SetCueBannerText("Type Here");
}
Finally, you can build it into a custom control:
C#:
public class CueBannerTextBox : TextBox
{
    private const int EM_SETCUEBANNER = 0x1501;

    private string cueBannerText;

    public event EventHandler CueBannerTextChanged;

    [Category("Appearance")]
    public string CueBannerText
    {
        get => cueBannerText;
        set
        {
            if (cueBannerText != value)
            {
                cueBannerText = value;

                SendMessage(Handle,
                            EM_SETCUEBANNER,
                            0,
                            cueBannerText);

                OnCueBannerTextChanged(EventArgs.Empty);
            }
        }
    }

    protected virtual void OnCueBannerTextChanged(EventArgs e)
    {
        CueBannerTextChanged?.Invoke(this, e);
    }

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern int SendMessage(
        IntPtr hWnd,
        int msg,
        int wParam,
        [MarshalAs(UnmanagedType.LPWStr)] string lParam);
}
and then add that to your form instead of a regular TextBox. Once you've added that class and built your project, the new control will appear in the Toolbox automatically, or you can build it into a library and then add it to the Toolbox for all projects. If you already have regular TextBoxes on the form and don't want to have to delete them, you can edit the design code file by handle and simply change the type in code. You will then have access to the CueBannerText property in the designer and, when it is set, the text will apear at design time too.

For the record, there is an EM_GETCUEBANNER message too, so you don't have to store the text in the control and you probably shouldn't, because it will be inaccurate if an EM_SETCUEBANNER message is sent externally. I just couldn't be bothered doing that here and now. Consider it am exercise for you if you make the right choice and go with the custom control.
thank you for help. firstly I get an error
Severity Code Description Project File Line Suppression State
Error CS1061 'Form1' does not contain a definition for 'NameText_Enter' and no accessible extension method 'NameText_Enter' accepting a first argument of type 'Form1' could be found (are you missing a using directive or an assembly reference?) Totogui2 C:\Users\Dell\source\repos\Totogui2\Totogui2\Form1.Designer.cs 67 Active
how can I have error on line 67 when I have 37 lines only.
then placing first part: I guess it should go right away under public form1 (constructor). please help me, thank you.
 
how can I have error on line 67 when I have 37 lines only.
Look closer at the error. The error is in the "Form1.Designer.cs" which is automatically generated by the VS WinForms Designer.
 
Firstly, please don't quote an entire, long post when you're basically ignoring the vast majority of it. You are actually responding to only the very first line of my post, so what point quoting anything but that line? In fact, what point quoting anything, when all you're doing is providing necessary information that should have been included to start with? You don't need to quote anything that you don't need others to specifically understand that you're responding to.
 
As for the issue, it appears that you have registered a handler for the Leave event of a TextBox and then you have deleted the method without unregistering it.

If you're used to VB (and even if you're not), it creates the registration as part of the method itself by adding a Handles clause. Deleting the method inherently unregisters it as an event handler. C# is different. Where VB has two mechanisms for registering event handlers (WithEvents/Handles and AddHandler/RemoveHandler), C# has only one (+=/-= operators). The += and -= operators are equivalent to AddHandler and RemoveHandler but, where they only get used in user code in VB, they get used in designer code too in C#. That means you have some code in your designer code file that is trying to register a method named NameText_Enter as an event handler when no such method exists.

In future, if you want to delete an event handler without deleting the control/component whose event it's handling, you need to unregister it first. You do that by right-clicking the event in the Properties window and resetting it, just as you would for a property. Now that you haven't done that, you'll have to open the designer code file and manually delete the line that registers the event handler. If you had simply double-clicked the error in the Error List window then it would have taken you straight to that line and you could have deleted it without fuss.
 
Back
Top Bottom