John Brooks
John Brooks

Reputation: 69

Custom Component Properties in Visual Studio

I made a custom component that inherits TextBox. I gave the component a property called "Watermark". The property accessible in the Properties window after dragging one of these components from the toolbox. However, if I assign a value to Watermark, it doesn't carry over to when i have the program launched.

[Browsable(true)]
[Category("Extended Properties")]
[Description("Set TextBox's watermark")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public string Watermark
{
    get; set;
}

public TextBoxWithWatermark()
{
    InitializeComponent();
    InitializeWatermark();
}

public TextBoxWithWatermark(IContainer container)
{
    container.Add(this);
    InitializeComponent();
    InitializeWatermark();
}

private void InitializeWatermark()
{
    Console.WriteLine("Watermark: " + Watermark);
    Text = Watermark;
}

The Console#WriteLine claims that its blank no matter what. Is there something I am missing?

Upvotes: 1

Views: 333

Answers (1)

John Brooks
John Brooks

Reputation: 69

Thanks to Lei Yang in the comments, it's solved.

I had to change to this..

    private string watermark;
    public string Watermark
    {
        get
        {
            return watermark;
        }
        set
        {
            Text = value;
            watermark = value;
        }
    }

Upvotes: 1

Related Questions