Results 1 to 14 of 14

Thread: [2005] A element of a User Control as a Property

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2007
    Posts
    49

    [2005] A element of a User Control as a Property

    Hello everybody!

    I am having some problems developing a user control.

    The control I'm developing is a control that has 4 buttons and a treeview.

    I want the developer who uses the control to be able to change de treeview properties. I have read in the internet, that I can set a "sub-control" of a user control as a property of the user control.

    So, in my control, I have two properties:

    Code:
    <Description("Toma o establece el color de fondo de los botones del control")> _
        Property ColorBotones() As Color
            Get
                Return B_Arriba.BackColor
            End Get
            Set(ByVal value As Color)
                B_Arriba.BackColor = value
                B_Abajo.BackColor = value
                B_Izquierda.BackColor = value
                B_Derecha.BackColor = value
            End Set
        End Property
    
        <Description("Toma o establece el control TreeView")> _
        Property Arbol() As TreeView
            Get
                Return TV_Arbol
            End Get
            Set(ByVal value As TreeView)
                TV_Arbol = value
            End Set
        End Property
    ColorBotones is used to change the backcolor of the four buttons (B_Arriba, B_Abajo, B_Izquierda, B_Derecha).
    Arbol() should be used to change the properties of the treeview of the user control.

    The ColorBotones proporty works perfectly, but Arbol() doesn't. In the designer view, I (the developer) can see the two properties correctly, but the changes I do in the Arbol property only changes the appearance in the designer view. When I execute the form that contains the usercontrol, all the changes I have done in the designer view disappear.

    Does anybody know where's the problem?

    Thanks in advance
    Last edited by sagoga69; Mar 5th, 2008 at 06:29 AM. Reason: Change the title

  2. #2
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: [2005] A element of a User Control as a Property

    That should be a readonly property because you are not going to be passing a new Treeview into it. Try that and see if it fixes your issues. With it readonly it will it readonly, it will pass the treeview that it has and allow for it's properties to be set.
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  3. #3
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: [2005] A element of a User Control as a Property

    By setting the property to be readonly, isn't only be able to get(read) the default values of the treeview and not set them?

    I have a concern though, at design time, are you able to view the individual properties? If you create a TreeView at runtime and then use your control TreeView property to set the new value What will happen?

  4. #4

    Thread Starter
    Member
    Join Date
    Sep 2007
    Posts
    49

    Re: [2005] A element of a User Control as a Property

    I have tried to set the Property Arbol as Readonly, but if I do it, VS doesn't let me to have the set method, so my code would be this:

    Code:
    ReadOnly Property Arbol() As TreeView
            Get
                Return TV_Arbol
            End Get
        End Property
    I have tried it, but it didn't work. Am I doing something wrong?

    May be, I didn't understand your post correctly. Do you mean what I have said?

    Thanks anyway, bmahler

  5. #5
    Frenzied Member
    Join Date
    May 2006
    Location
    Toronto, ON
    Posts
    1,093

    Re: [2005] A element of a User Control as a Property

    You don't want it to have a Set property. The treeview is already in the control. You can use the Get to access it and then make whatever changes you want to the treeview, but you don't want people to be able to create another treeview and add it to your control, so you eliminate the Set statement.

  6. #6
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: [2005] A element of a User Control as a Property

    With a readonly property, you can't change the value neither at runtime nor design time of your control. I don't think this is what you want.

    If I understood correctly, let's try this case
    I create UserControl in which I have a Label. but the label itself is a control with a set of properties. Now instead of changing each property one by one, I decide that I will make the whole label as a property and then set the label property of my user control.

    vb Code:
    1. <Description("Toma o establece el control TreeView")> _
    2.     Property myLabel() As Label
    3.         Get
    4.             Return me._myLabel
    5.         End Get
    6.         Set(ByVal value As Label)
    7.             _myLabel = value
    8.         End Set
    9.     End Property
    10.  
    11. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    12.   dim myUserControl as New UserControl
    13.   dim label as New Label
    14.   label.ForeColor = Color.Red
    15.   label.BorderStyle = BorderStyle.Fixed3D
    16.  
    17.   myUserControl.myLabel = Label        
    18. End Sub

    I think that after this, the label in my UserControl will be changed

  7. #7
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: [2005] A element of a User Control as a Property

    Quote Originally Posted by Tom Sawyer
    You don't want it to have a Set property. The treeview is already in the control. You can use the Get to access it and then make whatever changes you want to the treeview, but you don't want people to be able to create another treeview and add it to your control, so you eliminate the Set statement.
    is using the set in the property will add a new TreeView or change the current treeView in the Usercontrol.

    When I do create label at design time with a background color red and then at runtime I change it with the label.backgroundcolor = blue I'm just modifying the existing one not creating a new label

  8. #8
    Frenzied Member
    Join Date
    May 2006
    Location
    Toronto, ON
    Posts
    1,093

    Re: [2005] A element of a User Control as a Property

    It will change the current treeview to whatever you pass in. It really depends on how the control is set up to determine whether or not you want to give developers the ability to do this or not.

  9. #9

    Thread Starter
    Member
    Join Date
    Sep 2007
    Posts
    49

    Re: [2005] A element of a User Control as a Property

    talkro, thanks for your answer. I'm not sure whether I have understood you or not.

    Since I have understood that you want me to use the Arbol Property at runtime by setting it with a treeview object (created at runtime, too), I have tried this code:

    In the user control
    Code:
    <Description("Toma o establece el control TreeView")> _
        Property Arbol() As TreeView
            Get
                Return TV_Arbol
            End Get
            Set(ByVal value As TreeView)
                TV_Arbol = value
            End Set
        End Property
    In the form that contains the user control:
    Code:
    Public Sub New()
            Dim arbol2 As TreeView
            ' Llamada necesaria para el Diseñador de Windows Forms.
            InitializeComponent()
    
            arbol2 = New TreeView()
            arbol2.Nodes.Add("Hello, what's up?")
            arbol2.ForeColor = Color.Red
            Me.Arbol = arbol2
    
            End Sub
    It didn't work either, so I think that the solution must be kind of different.

    Tom Sawyer, thanks for your answer. I understand why you say that it must be readonly, but the problem persists. I change in the design view the properties and the appearance change correctly, but when I run the program the appearance continue unchanged.

    I don't know if I'm doing something wrong.

    I have a really openmind....so, anyother suggestions?

    Thanks again for your help!

  10. #10
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: [2005] A element of a User Control as a Property

    Quote Originally Posted by talkro
    With a readonly property, you can't change the value neither at runtime nor design time of your control. I don't think this is what you want.

    If I understood correctly, let's try this case
    I create UserControl in which I have a Label. but the label itself is a control with a set of properties. Now instead of changing each property one by one, I decide that I will make the whole label as a property and then set the label property of my user control.

    vb Code:
    1. <Description("Toma o establece el control TreeView")> _
    2.     Property myLabel() As Label
    3.         Get
    4.             Return me._myLabel
    5.         End Get
    6.         Set(ByVal value As Label)
    7.             _myLabel = value
    8.         End Set
    9.     End Property
    10.  
    11. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    12.   dim myUserControl as New UserControl
    13.   dim label as New Label
    14.   label.ForeColor = Color.Red
    15.   label.BorderStyle = BorderStyle.Fixed3D
    16.  
    17.   myUserControl.myLabel = Label        
    18. End Sub

    I think that after this, the label in my UserControl will be changed
    What you are doing here is creating a new object and overwriting it with this new object. You can accomplish the same thing with a ReadOnly Property and setting it like so
    Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim myUserControl as New UserControl
        myUserControl.myLabel.ForeColor = Color.Red       
        myUserControl.myLabel.BorderStyle = BorderStyle.Fixed3D
    End Sub
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  11. #11
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: [2005] A element of a User Control as a Property

    Ok, I created a usercontrol with a TreeView on it
    Code:
    Public Class UserControl1
    
        Public ReadOnly Property OutputTree() As TreeView
            Get
                Return Me.TreeView1
            End Get
        End Property
    End Class
    That is all of the code I have on it.

    Now i placed it on a form and called this in the load event
    Code:
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.UserControl11.OutputTree.BackColor = Color.Cyan
        End Sub
    End Class
    And the backcolor is indeed Cyan
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  12. #12
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: [2005] A element of a User Control as a Property

    bmahler you are right, my mistake. by returning the treeview, you return the control and by default can access its properties and set them.

  13. #13

    Thread Starter
    Member
    Join Date
    Sep 2007
    Posts
    49

    Re: [2005] A element of a User Control as a Property

    bmahler, I have tried the same, using the load event instead of the Sub new methog, and it works perfect.

    So, ok, we have proved that the property works perfectly setting it to readonly, but... Is it possible to edit the treeview attributes in the design view?

    Does anyone have any idea?

    As a favor, may I ask whether my english is too bad....

    Thanks again in advance

  14. #14

    Thread Starter
    Member
    Join Date
    Sep 2007
    Posts
    49

    Re: [2005] A element of a User Control as a Property

    I guess no one know the answer, isn't it?

    I have been trying, but I haven't had luck...may be there's no solution.

    Thanks everybody for reading the thread
    Last edited by sagoga69; Mar 6th, 2008 at 09:51 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width