Design Time Controls

  • Thread starter Thread starter Derek Hart
  • Start date Start date
D

Derek Hart

Build a usercontrol that I want developers to use in Visual Studio. I need
to pass some variables into the control at design time, so the property grid
can pop up with specific properties. How could I pass, for example, the name
of the form I am on (at design time), to the property?
 
Build a usercontrol that I want developers to use in Visual Studio. I need
to pass some variables into the control at design time, so the property grid
can pop up with specific properties. How could I pass, for example, the name
of the form I am on (at design time), to the property?

Why don't you just back up through the Parent properties until you get
to the form?
 
I'm just not sure where to do this... the user control is self contained...
how can I talk to the live form at design time? For example, here is the
property I am modifying on an inherited TextBox: I need to pass the name of
the form at design time so the custom UITypeEditor has the name to get the
correct data to return to the UITypeEditor in the property.

<CategoryAttribute("Design"), _
DefaultValueAttribute(GetType(String), ""), _
DescriptionAttribute("Name of the data field."), _
Editor(GetType(MergeFieldNameBuilder),
GetType(System.Drawing.Design.UITypeEditor))> _
Public Property DataField() As String
Get
Return _DataField
End Get
Set(ByVal value As String)
_DataField = value
End Set
End Property
 
I think I have found something that puts me on the right track. This will
give me the name of the form inside the property:

Return Site.Container.Components(0).Site.Name

I can even get to the properties with correct casting.

Return CType(Site.Container.Components(0), System.Windows.Forms.Form).Text

Any comments on if this is the best way to get to properties at design time?
 
It looks like that would work.

I still don't see why you don't just use (in some method in your
UserControl) Me.FindForm() or use Me.Parent to back up through the
parents of your UserControl until you get to the form.

Even in the IDE, each control is added to its parent's Controls
collection, so starting with any control you can look through the tree
of all controls.
 
Jack,

I have a class that just inherits a control. Just for testing, I placed the
CType down below. This class is not part of a usercontrol directly. It is
just a textbox that is used inside the IDE toolbox. In the "Get" part of the
property I tried looking at the parent, but I don't really have access to
this. Do you know how that might look?

<ToolboxBitmapAttribute(GetType(TextBox))> _

Public Class MyTextBox

Inherits TextBox

Private _DataField As String = ""





<CategoryAttribute("Design"), _

DefaultValueAttribute(GetType(String), ""), _

DescriptionAttribute("Name of the data field."), _

Editor(GetType(MergeFieldNameBuilder),
GetType(System.Drawing.Design.UITypeEditor))> _

Public Property DataField() As String

Get

Return CType(Site.Container.Components(0),
System.Windows.Forms.Form).Text
End Get

Set(ByVal value As String)

_DataField = value

End Set

End Property

End Class



Derek
 
What does "I don't really have access to this" mean?

If you want the Get of the DataField property to return the form's
Text property, I would try:

Get
Dim frm As System.Windows.Forms.Form = Me.FindForm()

Return If(frm Is Nothing, '', frm.Text)
End Get
 
Me.FindForm() just has an error "FindForm is not a member of
myControls.TextBox".

Do you have specific code for this?
 
Derek said:
Me.FindForm() just has an error "FindForm is not a member of
myControls.TextBox".

Do you have specific code for this?

Are you sure it's a Winforms application? ;-) Every control has a FindForm
method because it's a member of System.Windows.Forms.Control. What's your
class' base class?


Armin
 
Back
Top