some questions about controls

B

Brian

Hi,
here is exactly what I want to try and do. On a simple level, when the user
clicks on a textbox the matching label will change colors to hightlight the
which field they are editing. By Default, I always label my labels and
textboxes the same with the exception of the first 3 character....
txtTextbox and the label would be lblTextbox.
Hears the tricky part. I want to have one procedure to do this, this
procedure will be called on the gotfocus event of the textbox(?for each
textbox) and then check the readonly properity of that textbox, if it is
true colors will stay the same, otherwise change foreground and background.
What makes this complecated is this, I don't want to have to put this code
on each gotfocus event of a textbox, maybe sometype of global object
handling event? The other thing is I am taking the name of the textbox and
striping the first 3 character off and tring to set a temp object(label) by
that name to change the colors.
Any idea and or samples you could should me?
Thanks Brian
 
J

Jack Jackson

Hi,
here is exactly what I want to try and do. On a simple level, when the user
clicks on a textbox the matching label will change colors to hightlight the
which field they are editing. By Default, I always label my labels and
textboxes the same with the exception of the first 3 character....
txtTextbox and the label would be lblTextbox.
Hears the tricky part. I want to have one procedure to do this, this
procedure will be called on the gotfocus event of the textbox(?for each
textbox) and then check the readonly properity of that textbox, if it is
true colors will stay the same, otherwise change foreground and background.
What makes this complecated is this, I don't want to have to put this code
on each gotfocus event of a textbox, maybe sometype of global object
handling event? The other thing is I am taking the name of the textbox and
striping the first 3 character off and tring to set a temp object(label) by
that name to change the colors.
Any idea and or samples you could should me?
Thanks Brian

I would create a subclass of the Textbox class and put your code in
it, then use that class instead of Textbox on your form. Override the
OnGotFocus method. Alternatively you could wire up the GotFocus event
of all of the textboxes to one event handler routine.

To find the label corresponding to the textbox use the form's Controls
collection Find method.

Another approach would be to build a UserControl that contains a
Textbox and a Label.
 
R

rowe_newsgroups

With that said, my two favorite options are to put both the textbox
and the label into a user control, this encapsulates the entire
behavior and will make it easier on your developers if the spec
changes, for example half the labels get highlighted in green and the
others in red. My other recommended way would be to use Control.Find
to find the label, but this could get expensive processor wise so it
might not be viable (or you might look into doing the search
asynchronously).

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
 
P

Phill W.

Jack said:
I would create a subclass of the Textbox class and put your code in
it, then use that class instead of Textbox on your form. Override the
OnGotFocus method. Alternatively you could wire up the GotFocus event
of all of the textboxes to one event handler routine.

To find the label corresponding to the textbox use the form's Controls
collection Find method.

Another approach would be to build a UserControl that contains a
Textbox and a Label.

That's the route I went, encapsulating both Label and Textbox in a
single UserControl. OK, it's a bit of a pain mucking about aggregating
or overriding some of the properties to "get at" the TextBox and Label
via the UserControl wrapper, but IMHO, it's well worth the effort.

BTW, according to MSDN, we supposed to /avoid/ using OnGotFocus and
OnLostFocus (and their corresponding events) and should be using OnEnter
and OnLeave instead. No idea why; that's just what Our Friends in
Redmond now "recommend".

HTH,
Phill W.
 
T

Trevor Benedict

IMHO, Using a UserControl to encapsulate is a very clean approach .

Regards,

Trevor Benedict
MCSD
 
J

J.B. Moreno

Brian said:
really, i didn't get much from that, can you explain a bit more?

Since the names are similar, you have a procedure that does a one time
match -- set the tag of each textbox to the corresponding label, then
in your gotfocus event use the tag to set the label.
 

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