IExtenderProvider [modified]
-
[modification @ 10:12 Monday 26th February, 2007] Moved to the C# forum: http://www.codeproject.com/script/comments/forums.asp?forumid=1649&select=1912526#xx1912526xx[^] [/modification] I am implementing a custom IExtenderProvider derived class so I can dynamically set the following properties on a control: Required (using an ErrorProvider) Description (ToolTip) Errors (using an ErrorProvider) Caption I have everything working except for the caption. Ideally, I would like to add a Label control to the form for the given UI element. I know how to draw the label text myself, but that doesn't give me any of the accessability features that I gain when using a label. The problem I'm running in to is that I can get the label to appear at design time but not at run time. Essentially, when I detect that a new control has been added I try to add a new label control (positioned appropriately) to the parent's Controls array. This works fine at design time but at run time the Parent property of the control I'm extending is null. I'm trying to do this using an IExtenderProvider so I don't have to create subclassed versions of the UI controls to add a caption. In case you want the background, the project I'm working on needs to allow a dynamically rendered UI based on various pieces of meta data contained in the business object. The business object can be customized by the end-user through an admin interface, so I will never know what fields will be present and what their captions should be until it is time to actually render the screen. Has anybody done anything similar to this? I've pretty much exhausted the information available on Google, which isn't much beyond creating a simple provider.
----------------------------- In just two days, tomorrow will be yesterday.
-
[modification @ 10:12 Monday 26th February, 2007] Moved to the C# forum: http://www.codeproject.com/script/comments/forums.asp?forumid=1649&select=1912526#xx1912526xx[^] [/modification] I am implementing a custom IExtenderProvider derived class so I can dynamically set the following properties on a control: Required (using an ErrorProvider) Description (ToolTip) Errors (using an ErrorProvider) Caption I have everything working except for the caption. Ideally, I would like to add a Label control to the form for the given UI element. I know how to draw the label text myself, but that doesn't give me any of the accessability features that I gain when using a label. The problem I'm running in to is that I can get the label to appear at design time but not at run time. Essentially, when I detect that a new control has been added I try to add a new label control (positioned appropriately) to the parent's Controls array. This works fine at design time but at run time the Parent property of the control I'm extending is null. I'm trying to do this using an IExtenderProvider so I don't have to create subclassed versions of the UI controls to add a caption. In case you want the background, the project I'm working on needs to allow a dynamically rendered UI based on various pieces of meta data contained in the business object. The business object can be customized by the end-user through an admin interface, so I will never know what fields will be present and what their captions should be until it is time to actually render the screen. Has anybody done anything similar to this? I've pretty much exhausted the information available on Google, which isn't much beyond creating a simple provider.
----------------------------- In just two days, tomorrow will be yesterday.
Scott Sorry that it has taken so long to get back to you, but the way to do this is fairly straight forward. In your Set... method, add a handler for the ParentChanged event like this:
txtItem.ParentChanged += new EventHandler(txtItem_ParentChanged);
Then, your method will look like this:
void txtItem_ParentChanged(object sender, EventArgs e) { TextBox control = sender as TextBox; string labelText = Get...(control); LabelExtender(labelText, control); }
Finally, create a method for the label extender:
private void LabelExtender(string labelText, TextBox txtBox) { Point loc = txtBox.Location; Label label = new Label(); label.Name = string.Format("label{0}", Guid.NewGuid().ToString()); label.Location = loc; label.Text = labelText; label.AutoSize = true; label.TabIndex = 0; label.Size = new System.Drawing.Size(35, 13); label.Visible = true; if (txtBox.Parent != null) { txtBox.Parent.Controls.Add(label); } txtBox.Left += label.Width; if (txtBox.Parent != null) txtBox.Parent.Invalidate(); }
the last thing I want to see is some pasty-faced geek with skin so pale that it's almost translucent trying to bump parts with a partner - John Simmons / outlaw programmer
Deja View - the feeling that you've seen this post before. -
Scott Sorry that it has taken so long to get back to you, but the way to do this is fairly straight forward. In your Set... method, add a handler for the ParentChanged event like this:
txtItem.ParentChanged += new EventHandler(txtItem_ParentChanged);
Then, your method will look like this:
void txtItem_ParentChanged(object sender, EventArgs e) { TextBox control = sender as TextBox; string labelText = Get...(control); LabelExtender(labelText, control); }
Finally, create a method for the label extender:
private void LabelExtender(string labelText, TextBox txtBox) { Point loc = txtBox.Location; Label label = new Label(); label.Name = string.Format("label{0}", Guid.NewGuid().ToString()); label.Location = loc; label.Text = labelText; label.AutoSize = true; label.TabIndex = 0; label.Size = new System.Drawing.Size(35, 13); label.Visible = true; if (txtBox.Parent != null) { txtBox.Parent.Controls.Add(label); } txtBox.Left += label.Width; if (txtBox.Parent != null) txtBox.Parent.Invalidate(); }
the last thing I want to see is some pasty-faced geek with skin so pale that it's almost translucent trying to bump parts with a partner - John Simmons / outlaw programmer
Deja View - the feeling that you've seen this post before.Pete, No worries. Since this forum is still rather new, I wasn't sure if it has the same amount of regular "readership" as the other forums. Between your post and Mike's posts in the other forum I was able to get everything working. (That's actually why it has taken so long to respond...I've been heads-down coding the last two days.) Your post did point out to me that I was missing an event handler for when the extended control's parent changes, which I have added. Now that everything is working and getting the finishing touches tomorrow, I'm going to ask if I can do an article on this (at least a slimmed down version), which I don't think I should encounter any objections. Thanks, Scott.
----------------------------- In just two days, tomorrow will be yesterday.
-
Pete, No worries. Since this forum is still rather new, I wasn't sure if it has the same amount of regular "readership" as the other forums. Between your post and Mike's posts in the other forum I was able to get everything working. (That's actually why it has taken so long to respond...I've been heads-down coding the last two days.) Your post did point out to me that I was missing an event handler for when the extended control's parent changes, which I have added. Now that everything is working and getting the finishing touches tomorrow, I'm going to ask if I can do an article on this (at least a slimmed down version), which I don't think I should encounter any objections. Thanks, Scott.
----------------------------- In just two days, tomorrow will be yesterday.
Cool - I'll look forward to seeing your article.:-D
Deja View - the feeling that you've seen this post before.