all groups > visual studio .net ide > may 2006 >
You're in the

visual studio .net ide

group:

Change label's Name when Text assigned at design time programmatically?


Change label's Name when Text assigned at design time programmatically? mnadig
5/24/2006 2:36:30 PM
visual studio .net ide:
I have my own control that inherits from Label. When I drop it on a
form, it defaults the object Name to MyLabel1 and the Text to MyLabel1.
I'd like the control to automatically rename itself if it detects, at
design time, the Text property changes; e.g.

private sub MyLabel_TextChanged.... handles Me.TextChanged
if me.Name.StartsWith( "MyLabel") then
me.Name = Me.Text (minus spaces)
end if
end sub

Is this possible?
Re: Change label's Name when Text assigned at design time programmatically? mnadig
5/24/2006 4:13:51 PM
I found the answer:
Import system.componentmodel

in text changed event:
If Me.DesignMode Then
Dim nameProp As PropertyDescriptor =
TypeDescriptor.GetProperties(Me)("Name")
nameProp.SetValue(Me, "lbl" + FixTextForName(Me.Text))
end if

(where fixTextForName strips out invalid chars for name prop)
AddThis Social Bookmark Button