Question UserControl - Show properties of inner-control in design-mode properties windows.

aaron1127

New member
Joined
Mar 6, 2011
Messages
3
Programming Experience
1-3
Hi all,
first, im poor in writing english, i hope all can understanding what im writed below. thanks.. :stung:

Q: i got a UserControl name UC1, which contain 2 controls (textbox, label). How can i read and write ALL properties of textbox and label (inside UC1) in "Properties Windows" in design-mode? currently, i only can see UC1's properties, but properties of textbox and label never show out. please ... thanks..
REMARKS: No want create 1 by 1 of properties of control for example: Public Overrides Property blabla As String... End Property
 
Hi,

As far as I am aware, if you want to set these Properties at Design time through the Properties window then you have no other option than to create an additional Property within your User Control and then set your Internal Control properties accordingly. i.e:-

VB.NET:
Public Class myControl
  'This User Control contains 1 TextBox called TextBox1
  Private _TextBox_Text As String
 
  Public Property TextBox_Text As String
    Get
      Return _TextBox_Text
    End Get
    Set(value As String)
      _TextBox_Text = value
      TextBox1.Text = _TextBox_Text
    End Set
  End Property
End Class

This TextBox_Text Property is then visible for you to set in the Properties window.

Alternatively, you do have full access to every Property and Method of every Child Control in your User Control through code. i.e:-

VB.NET:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  MyControl1.Label1.Text = "The Text To Display Is :"
  MyControl1.TextBox1.Text = "Hello"
End Sub

Hope that helps.

Cheers,

Ian
 
thanks Ian,

you try to look at control [[FONT=Segoe UI, Helvetica, Garuda, Arial, sans-serif]SplitContainer], it got 2 Panel. In design-mode > when i click on it (SplitContainer), properties box show "[+] Panel1" and "[+] Panel2" > click on [+] Panel1, it list out all properties of Panel1. If i using 1 by 1 which to create Public Property ..... End Property, many property need to create and will missing up some. [/FONT][FONT=Segoe UI, Helvetica, Garuda, Arial, sans-serif]
[/FONT][FONT=Segoe UI, Helvetica, Garuda, Arial, sans-serif]Any idea more?

Cheers,
Aaron[/FONT]
 
You need to add a property to the UC for each child control and decorate it with the DesignerSerializationVisibility attribute, e.g.
Imports System.ComponentModel

Public Class UserControl1

    <DesignerSerializationVisibility(DesignerSerializationVisibility.Content)>
    Public ReadOnly Property ChildTextBox() As TextBox
        Get
            Return TextBox1
        End Get
    End Property

End Class
EDIT: Actually, I just checked and the attribute doesn't seem to matter. Just the property is enough to expose the control and its properties to the Properties window.
 
You need to add a property to the UC for each child control and decorate it with the DesignerSerializationVisibility attribute, e.g.
Imports System.ComponentModel

Public Class UserControl1

    <DesignerSerializationVisibility(DesignerSerializationVisibility.Content)>
    Public ReadOnly Property ChildTextBox() As TextBox
        Get
            Return TextBox1
        End Get
    End Property

End Class
EDIT: Actually, I just checked and the attribute doesn't seem to matter. Just the property is enough to expose the control and its properties to the Properties window.

Very Nice jmcilhinney, I had a funny feeling there should have been a better way to access this which is why I used the phrase "As far as I am aware".

Cheers,

Ian
 
Hi All,

Just been playing with the UserControl comments made by jcmilhinney above. jcmilhinney mentioned:-

Actually, I just checked and the attribute doesn't seem to matter. Just the property is enough to expose the control and its properties to the Properties window.

If you do NOT add the attribute demonstrated then the project WILL allow you to set properties in the Property window, but as soon as you run the project the property is NOT displayed (i.e Text Property) and when you re-check the property window after debugging your application you will find that the property has been REMOVED from the properties window?

Not sure why as yet, but all works fine and as expected if you make sure you have supplied the correct attribute as demonstrated by jmcilhinney.

One to remember.

Cheers,

Ian
 
Back
Top