|
-
Mar 5th, 2008, 06:15 AM
#1
Thread Starter
Member
[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
-
Mar 5th, 2008, 09:45 AM
#2
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.
-
Mar 5th, 2008, 09:56 AM
#3
Fanatic Member
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?
-
Mar 5th, 2008, 10:00 AM
#4
Thread Starter
Member
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
-
Mar 5th, 2008, 10:04 AM
#5
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.
-
Mar 5th, 2008, 10:16 AM
#6
Fanatic Member
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:
<Description("Toma o establece el control TreeView")> _ Property myLabel() As Label Get Return me._myLabel End Get Set(ByVal value As Label) _myLabel = value End Set End Property Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load dim myUserControl as New UserControl dim label as New Label label.ForeColor = Color.Red label.BorderStyle = BorderStyle.Fixed3D myUserControl.myLabel = Label End Sub
I think that after this, the label in my UserControl will be changed
-
Mar 5th, 2008, 10:20 AM
#7
Fanatic Member
Re: [2005] A element of a User Control as a Property
 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
-
Mar 5th, 2008, 10:27 AM
#8
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.
-
Mar 5th, 2008, 10:29 AM
#9
Thread Starter
Member
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!
-
Mar 5th, 2008, 10:38 AM
#10
Re: [2005] A element of a User Control as a Property
 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:
<Description("Toma o establece el control TreeView")> _ Property myLabel() As Label Get Return me._myLabel End Get Set(ByVal value As Label) _myLabel = value End Set End Property Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load dim myUserControl as New UserControl dim label as New Label label.ForeColor = Color.Red label.BorderStyle = BorderStyle.Fixed3D myUserControl.myLabel = Label 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
-
Mar 5th, 2008, 10:40 AM
#11
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
-
Mar 5th, 2008, 10:56 AM
#12
Fanatic Member
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.
-
Mar 5th, 2008, 12:34 PM
#13
Thread Starter
Member
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
-
Mar 6th, 2008, 09:39 AM
#14
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|