Trap Name Change in User Control?

J

Jeff Gaines

This was originally posted to
microsoft.public.dotnet.framework.windowsforms.controls but it's a very
sleepy group....

Is there a way to trap a change of name for a User Control?

I want to set some text based on the name of the control at design time
but I can't see 'OnNameChanged' or anything similar.

I can achieve what I want to do by adding a property but I'd like to do it
automagically if possible.
 
N

Neil Barnwell

I'm not sure I'd take this approach, personally. Typically the Name
property of a control is set by designers. If you want some
functionality to happen, it might be better to make it more explicit
by having a method or property on the UserControl that does what you
want.
 
F

Family Tree Mike

Jeff said:
This was originally posted to
microsoft.public.dotnet.framework.windowsforms.controls but it's a very
sleepy group....

Is there a way to trap a change of name for a User Control?

I want to set some text based on the name of the control at design time
but I can't see 'OnNameChanged' or anything similar.

I can achieve what I want to do by adding a property but I'd like to do
it automagically if possible.

Most often, this is just the name of the variable for the coders access
to changing properties behind the scenes. When you create a control in
code, rather than the designer, the Name property is not set. I agree
with Neil, that this seems like an odd approach.
 
J

Jeff Gaines

Most often, this is just the name of the variable for the coders access to
changing properties behind the scenes. When you create a control in code,
rather than the designer, the Name property is not set. I agree with
Neil, that this seems like an odd approach.

I have put together a control with a Label on the Left and Text Box on the
right. The intention is to save time when designing Forms with loads of
data on them, say an address book.

What I would like to do is to be able to name the instance of the control
jtxtHomeAddress and then parse that name and automatically extract 'Home
Address' to go in the label portion. It may sound lazy but it would be a
real time saver! The parsing is easy but I want to trap any name change so
I can change the label text automatically.
 
P

Peter Duniho

Jeff said:
[...]
What I would like to do is to be able to name the instance of the
control jtxtHomeAddress and then parse that name and automatically
extract 'Home Address' to go in the label portion. It may sound lazy but
it would be a real time saver! The parsing is easy but I want to trap
any name change so I can change the label text automatically.

It'll be a real time-saver, until someone has a good reason for naming
the control something other than something useful for the label text, or
someone needs some text in the label that can't be represented in the
name of the control, or someone wants to localize the UI.

IMHO, you ought to just require that the Label.Text property be set at
design-time to the correct text. Don't conflate program code with UI.

If, for example, this is a UserControl with the Label and TextBox
contained within, you _could_ use the UserControl's own Text property
and copy that programmatically to the Label. But it's not clear that
approach would in fact be more efficient than just setting the Label's
Text property directly.

Pete
 
J

Jeff Gaines

Jeff said:
[...]
What I would like to do is to be able to name the instance of the control
jtxtHomeAddress and then parse that name and automatically extract 'Home
Address' to go in the label portion. It may sound lazy but it would be a
real time saver! The parsing is easy but I want to trap any name change
so I can change the label text automatically.

It'll be a real time-saver, until someone has a good reason for naming the
control something other than something useful for the label text, or
someone needs some text in the label that can't be represented in the name
of the control, or someone wants to localize the UI.

It will only set the label text if the name begins with 'jtxt', otherwise
it just ignores it.

All my utilities for classes that show data/get data are based on strict
naming conventions based on the field names in the database, that allows
me to produce functions like ShowRecord, GetRecordFromForm etc. at the
press of a button instead of spending hours producing boiler plate code.

IMHO, you ought to just require that the Label.Text property be set at
design-time to the correct text. Don't conflate program code with UI.

If, for example, this is a UserControl with the Label and TextBox
contained within, you could use the UserControl's own Text property and
copy that programmatically to the Label. But it's not clear that approach
would in fact be more efficient than just setting the Label's Text
property directly.

I've added a property _DataName (so it appears below Name in the
properties palette) and do the parsing when that changes. This is for my
own use, it's unlikely to escape into the wild.

The next step is to write a form designer that will create the labels etc.
on being given a heap of field names:)
 
J

Jeff Johnson

This was originally posted to
microsoft.public.dotnet.framework.windowsforms.controls but it's a very
sleepy group....

Aw crap, ignore my multiposting reply then....
 

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