Button Control Properties

dreamdelerium

Active member
Joined
Mar 7, 2008
Messages
36
Programming Experience
1-3
hey everyone. in vb.net (express edition) im trying to change multiple button properties at the same time. if i want to change the button width, this works

VB.NET:
        For i = 1 To 42
            With Calendar.Panel1.Controls("Button" & i)
                .width= 12
            End With
        Next

but if i want to change the button border, this wont work:

VB.NET:
        For i = 1 To 42
            With Calendar.Panel1.Controls("Button" & i).FlatAppearance
                .bordersize = 12
            End With
        Next

whats the difference and how do i get it working?

thanks
jason
 
Controls(index) will return you any control object as type Control, you have to type cast to access type specific members, this can be done with CType/DirectCast:
VB.NET:
Dim b As Button = DirectCast(theButtonObject, Button)
 
of, i think i got it

VB.NET:
        For i = 1 To 42
            Dim b As Button = DirectCast(Calendar.Controls("Button" & i), Button)

            With b.FlatAppearance
                .BorderSize = 12
            End With
        Next

nevermind, that didnt work either
 
can you explain a little more how that will work with the above example.
It will work because you need to cast that "Control" to "Button" before you can use properties that only Button class has. The Controls collection contains all controls on form and they can be Button and Label and all kinds, they all inherits Control class and can be boxed in a variable of type control. So Controls will return the reference to the correct instance but you will only see as "Control" until you look closer, that what you do when you type cast the object to a more specific control class like Button, Label etc.
 
yeah it is. heres what worked. someone else answered on another post
VB.NET:
        For i = 1 To 42
            With DirectCast(Calendar.Panel1.Controls("Button" & i), Button).FlatAppearance
                .BorderSize = 1
            End With
        Next
 
There is no difference between that code and you previous code, except in one you use Calendar.Controls and the other Calendar.Panel1.Controls.
 
Back
Top