0

I have created a custom Textbox control, I try to set a default text for the Textbox. So in its constructor I say Text = "My Default Text", this seems to not affect the control in design mode.

Here is the custom Textbox code:

using System.Windows.Forms;

namespace MyNameSpace
{
    public class xTextBox : TextBox
    {
        public xTextBox()
        {
            BorderStyle = BorderStyle.None;
            Text = "My Default Text";
        }
    }
}

Then I drop that control over a usercontrol, no text :(

Control dropped

Here is what I see in the properties box: Text property empty

Properties box

Fabrice T
  • 648
  • 9
  • 23
  • I don't think that Visual Studio's design mode calls control constructors when displaying them. – Ian H. Sep 07 '17 at 08:24
  • Visual Studio calls the constructor, in my situation ``BorderStyle`` is applied. I observe that it is only the ``Text`` property that fails – Fabrice T Sep 07 '17 at 10:00
  • I tried a button custom control ``XButton`` , once dropped VS create the control and set ``"XButton1"`` as Text and ignore the text I put in its constructor. I added ``BackColor = Color.Yellow`` which worked – Fabrice T Sep 07 '17 at 10:08
  • @FabriceT hi, after 4 years I would like to ask you, did you find the solution that fix that problem – M.J Feb 03 '22 at 21:11

2 Answers2

0

The problem is explained here: Virtual member call in a constructor

In short to get what you want you need to have a sealed class:

public sealed class xTextBox : TextBox
{
    public xTextBox()
    {
        BorderStyle = BorderStyle.None;
        Text = "My Default Text";
    }
}

or sealed Text property:

public class xTextBox : TextBox
{
    public xTextBox()
    {
        BorderStyle = BorderStyle.None;
        Text = "My Default Text";
    }

    public sealed override string Text
    {
        get { return base.Text; }
        set { base.Text = value; }
    }
}

enter image description here

Renatas M.
  • 11,694
  • 1
  • 43
  • 62
  • It still not works, I think it is a Visual Studio issue, I observe that if I close the designer and open back the ``Text`` appears. – Fabrice T Sep 07 '17 at 08:06
  • It works. You need to build solution after you change something in controls. – Renatas M. Sep 07 '17 at 08:09
  • I did so, nothing changed; I cleaned the solution closed VS, restarted and re-built ; the same, the control text is empty once dropped. I observe that the problem is the ``Text`` property. Please read my comments on the question – Fabrice T Sep 07 '17 at 10:03
  • You doing something wrong. Added screenshot. It works. VS 2015. – Renatas M. Sep 08 '17 at 08:03
  • Sorry but it still not work for me VS 2017. Sure you are putting the custom textbox and get default text without having to close and open back the design mode ? – Fabrice T Sep 08 '17 at 17:42
0

The text needs to be a variable outside of a function.

Public string Text {
            get { return this;}
            set{value = this;}
 }

So something like that. There maybe be errors in the code above because I've typed it on my mobile. After you have something like that, set the text value wherever you want.

Euan Hollidge
  • 587
  • 2
  • 5
  • 21