By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,187 Members | 1,059 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,187 IT Pros & Developers. It's quick & easy.

Help extending textbox with an additional property

P: n/a
A beginners question to someone that isn't a beginner ;) I've forgotton
a lot.

The code below isn't what I want to do but it demonstrates succinctly
my problem. I want to drop a "MyTextBox" control on a form at design
time, set the "DummyProperty" property to be "test value1", and I want
to see it showing at design and runtime "test value1". It shows a
blank. If I do this with the "Text" property it works but not with my
"DummyProperty" property. When I set the "DummyProperty" at runtime, it
sets the value and updates the textbox correctly. Why? I wouldn't
normally ask someone to correct my code but on this occasion I would
really appreciate it this jump start. Sample code below.

Thanks in advance
Barry
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
namespace Test
{
public class MyTextBox : TextBox
{
public MyTextBox()
{
InitializeComponent();
this.Text="test value2";
}

[Browsable(true), NotifyParentProperty(true), Bindable(true),
Description("Dummy text control")]
public String Dummyproperty
{
set { this.Text = value; }
get { return this.Text; }
}

private void InitializeComponent()
{
this.SuspendLayout();
//Nothing here
this.ResumeLayout(false);
}

}
}

Jul 13 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
First of all, there should be no need to use the NotifyParentProperty
attribute. That is used for properties that have complex substructure.

Second, probably the reason this isn't working is that your
manipulating one Designer-visible property from inside another, and in
so doing you've created a sort of "race" situation. You may have
noticed that the Designer writes code to implement the settings you set
while in design mode. The way you've written this, if the Designer
decides to generate the code this way:

myTextBox1.DummyProperty = "test value 1";
myTextBox1.Text = "";

then both properties will end up being blank, whereas if it generates
code like this:

myTextBox1.Text = "";
myTextBox1.DummyProperty = "test value 1";

then both properties will contain "test value 1". This is because the
Designer doesn't know that modifying DummyProperty modifies Text, too,
and vice versa. Now, if you were to create a string field to store the
contents of DummyProperty, and thus separate it completely from the
Text property, you would probably see more rational behaviour:

public class MyTextBox : TextBox
{
private string _dummy;

[Browsable(true), Bindable(true), Description("Dummy text
control"), DefaultValue("")]
public String DummyProperty
{
set { this._dummy = value; }
get { return this._dummy; }
}

Finally, you can't just set a Designer-visible property in the
constructor and expect the Designer to sort that out. You need to use
the DefaultValue attribute on the property to indicate to the Designer
what its default value is.

Barry wrote:
A beginners question to someone that isn't a beginner ;) I've forgotton
a lot.

The code below isn't what I want to do but it demonstrates succinctly
my problem. I want to drop a "MyTextBox" control on a form at design
time, set the "DummyProperty" property to be "test value1", and I want
to see it showing at design and runtime "test value1". It shows a
blank. If I do this with the "Text" property it works but not with my
"DummyProperty" property. When I set the "DummyProperty" at runtime, it
sets the value and updates the textbox correctly. Why? I wouldn't
normally ask someone to correct my code but on this occasion I would
really appreciate it this jump start. Sample code below.

Thanks in advance
Barry
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
namespace Test
{
public class MyTextBox : TextBox
{
public MyTextBox()
{
InitializeComponent();
this.Text="test value2";
}

[Browsable(true), NotifyParentProperty(true), Bindable(true),
Description("Dummy text control")]
public String Dummyproperty
{
set { this.Text = value; }
get { return this.Text; }
}

private void InitializeComponent()
{
this.SuspendLayout();
//Nothing here
this.ResumeLayout(false);
}

}
}
Jul 13 '06 #2

P: n/a
Thanks for going to the effort of writing it this clearly Bruce. I
understand what you are saying up to a point. The bit I don't
understand is where (and when) I assign the value of _dummy to .Text.
Somewhere I must write this.Text=_dummy don't I ?
Thanks
Barry
Bruce Wood wrote:
First of all, there should be no need to use the NotifyParentProperty
attribute. That is used for properties that have complex substructure.

Second, probably the reason this isn't working is that your
manipulating one Designer-visible property from inside another, and in
so doing you've created a sort of "race" situation. You may have
noticed that the Designer writes code to implement the settings you set
while in design mode. The way you've written this, if the Designer
decides to generate the code this way:

myTextBox1.DummyProperty = "test value 1";
myTextBox1.Text = "";

then both properties will end up being blank, whereas if it generates
code like this:

myTextBox1.Text = "";
myTextBox1.DummyProperty = "test value 1";

then both properties will contain "test value 1". This is because the
Designer doesn't know that modifying DummyProperty modifies Text, too,
and vice versa. Now, if you were to create a string field to store the
contents of DummyProperty, and thus separate it completely from the
Text property, you would probably see more rational behaviour:

public class MyTextBox : TextBox
{
private string _dummy;

[Browsable(true), Bindable(true), Description("Dummy text
control"), DefaultValue("")]
public String DummyProperty
{
set { this._dummy = value; }
get { return this._dummy; }
}

Finally, you can't just set a Designer-visible property in the
constructor and expect the Designer to sort that out. You need to use
the DefaultValue attribute on the property to indicate to the Designer
what its default value is.

Barry wrote:
A beginners question to someone that isn't a beginner ;) I've forgotton
a lot.

The code below isn't what I want to do but it demonstrates succinctly
my problem. I want to drop a "MyTextBox" control on a form at design
time, set the "DummyProperty" property to be "test value1", and I want
to see it showing at design and runtime "test value1". It shows a
blank. If I do this with the "Text" property it works but not with my
"DummyProperty" property. When I set the "DummyProperty" at runtime, it
sets the value and updates the textbox correctly. Why? I wouldn't
normally ask someone to correct my code but on this occasion I would
really appreciate it this jump start. Sample code below.

Thanks in advance
Barry
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
namespace Test
{
public class MyTextBox : TextBox
{
public MyTextBox()
{
InitializeComponent();
this.Text="test value2";
}

[Browsable(true), NotifyParentProperty(true), Bindable(true),
Description("Dummy text control")]
public String Dummyproperty
{
set { this.Text = value; }
get { return this.Text; }
}

private void InitializeComponent()
{
this.SuspendLayout();
//Nothing here
this.ResumeLayout(false);
}

}
}
Jul 13 '06 #3

P: n/a
Barry wrote:
Thanks for going to the effort of writing it this clearly Bruce. I
understand what you are saying up to a point. The bit I don't
understand is where (and when) I assign the value of _dummy to .Text.
Somewhere I must write this.Text=_dummy don't I ?
Thanks
Barry
Well, that all depends upon what DummyProperty is supposed to do. What
is it supposed to do? If it's just an alias for ".Text" then you can't
do that without confusing the Designer, as you've seen. If it's
intended to do something else....

Jul 13 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.