JPC16

hey

i have in a webbrower i am making , a skin function allowing you to change the color off all buttons backcolors , or all form backcolors, ect (in construction)

but i have lots of forms , lots of objects , having 1 line code to change one color property of all my objects would take me ages.

for example :

dim a as color = color.red

ok so isnt there a method to set all the buttons backcolor of my project to red in a few code lines


i need help please




Re: Visual Basic Express Edition changing color of all buttons of my project in a few code lines

Chris

Dim button As New System.Windows.Forms.Button

For Each button In Me.Controls

button.BackColor = Color.Red

Next

Dim form As New System.Windows.Forms.Form

For Each form In My.Application.OpenForms

form.BackColor = Color.Red

Next

Please note the forms one will only change the background color of all the open forms.





Re: Visual Basic Express Edition changing color of all buttons of my project in a few code lines

Solitaire

Wasn't this problem already addressed in another thread yesterday Is this some kind of homework assignment

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=2171271&SiteID=1






Re: Visual Basic Express Edition changing color of all buttons of my project in a few code lines

ReneeC

Darn Solitaire, you beat me to it......




Re: Visual Basic Express Edition changing color of all buttons of my project in a few code lines

JPC16

ok thx for your help guys
sorry if i didnt see the answer was already in another post






Re: Visual Basic Express Edition changing color of all buttons of my project in a few code lines

ReneeC

Actually, you've been ta;ling to two women.




Re: Visual Basic Express Edition changing color of all buttons of my project in a few code lines

JPC16

Hey but wait got a little problem : when i try :

For Each ctl As Control In Controls

If TypeOf ctl Is Button Then

ctl.BackColor = Color.Red

End If

Next ctl



nothing happens, my buttons stay the same color
and when i try :

Dim button As New System.Windows.Forms.Button

For Each button In Me.Controls

button.BackColor = Color.Red

Next

it bugs while running

how come
is there a tag to put on top of the project ect


but the forms one works perfectly : )

and this isnt for a homework assignement lol








Re: Visual Basic Express Edition changing color of all buttons of my project in a few code lines

Dave299

Dim button As New System.Windows.Forms.Button

For Each button In Me.Controls

button.BackColor = Color.Red

Next

was never going to work if you had anything other than buttons on the form.

Your first code should work - but it won't if you have the buttons in another container such as a panel or groupbox.





Re: Visual Basic Express Edition changing color of all buttons of my project in a few code lines

JPC16

which i do
and without these panels i would have lots of code to do , more than with the colors

isnt there a way like :


Dim button As New System.Windows.Forms.Button

For Each button In Me.Controls ' which is not panel
button.BackColor = Color.Red

Next






Re: Visual Basic Express Edition changing color of all buttons of my project in a few code lines

Dave299

Try something like this:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

MakeButtonsRed(Me)

End Sub

Private Sub MakeButtonsRed(ByVal Container As Control)

For Each C As Control In Container.Controls

If TypeOf C Is Button Then

C.BackColor = Color.Red

ElseIf C.HasChildren Then

MakeButtonsRed(C)

End If

Next

End Sub





Re: Visual Basic Express Edition changing color of all buttons of my project in a few code lines

Solitaire

This will work for every button on your form, including all the containers, using a single Click event:

Code Snippet

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

For Each ctl As Control In Controls

If TypeOf ctl Is Button Then

ctl.BackColor = Color.Red

End If

For Each child As Control In ctl.Controls

If TypeOf child Is Button Then

child.BackColor = Color.Red

End If

Next child

Next ctl

End Sub






Re: Visual Basic Express Edition changing color of all buttons of my project in a few code lines

Dave299

Solitaire

That will not change the colour of any buttons in a container which is itself in another container

My code will - have you actually tried it





Re: Visual Basic Express Edition changing color of all buttons of my project in a few code lines

ReneeC

Nice use of recursion dave!

We don't so much of that around here. Wink






Re: Visual Basic Express Edition changing color of all buttons of my project in a few code lines

Dave299

Thanks Renee - I liked it even if Solitaire didn't. I can't believe it's particularly original though.





Re: Visual Basic Express Edition changing color of all buttons of my project in a few code lines

Solitaire

Actually, I just saw that same recursion (using different variable names) in a new book by Apress (I think it was "VB 2005 Recipes" or some similar title). I decided to redo the code in a single sub and it seemed to work, but I didn't try it with nested containers. So I guess the code I posted has its limitations.