Inherit textbox questions

W

Wan Andii

Hello Group,

I have a simple derived class 'TextBoxTestMsg' from textbox class. So
far I was able to inherit textbox to 'TextBoxTestMsg' with a public
property 'EnableMsg'. When the 'EnableMsg' is set to true then the
ontextchange event will be called. When I add 'TextBoxTestMsg' to a
form and set the 'EnableMsg' to true it doesn't change text value to
"Add text...".
How can I set the text value on load if EnableMsg is true? and also
I'd like to save the EnableMsg value the next time I load the project.

Regards,
Wan
------------------------------------
class TextBoxTestMsg: TextBox
{
public Boolean enableMsg = false;
public Boolean EnableMsg
{get/set...}

public TextBoxTestMsg()
{
if (EnableMsg == false) return;
Text = "Add text here...";
}

protected override void OnTextChanged(EventArgs e)
{
if (EnableMsg == false) return;
base.OnTextChanged(e);
MessageBox.Show("text msg");
}
}
------------------------------------
 
P

Peter Duniho

Hello Group,

I have a simple derived class 'TextBoxTestMsg' from textbox class. So
far I was able to inherit textbox to 'TextBoxTestMsg' with a public
property 'EnableMsg'. When the 'EnableMsg' is set to true then the
ontextchange event will be called. When I add 'TextBoxTestMsg' to a
form and set the 'EnableMsg' to true it doesn't change text value to
"Add text...".

The EnableMsg property can't be set until after the constructor is
executed (as part of creating the object). But the only place you set the
text to "Add text..." is in the constructor.

If you really want this behavior, it seems to me you should move the code
you currently show in the constructor, so that it's in the set method of
your EnableMsg property instead.

Without seeing more of the design, I can't comment on whether the design
approach you're attempting makes sense. But assuming it does, the change
I suggest should improve things.

Pete
 
W

Wan Andii

... move the code you currently show in the constructor, so that it's in the set method of
your EnableMsg property instead.

Good point! Thanks Pete. Sorry for the late post.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top