Changing back color of ALL web controls on a form.
In VB6 I would do something like:
Code:
Private Sub DoColors()
Dim FormControl As Control
For Each FormControl In Me.Controls
CType(FormControl, WebControl).BackColor = (New Color).FromArgb(RGB(CType(Rnd() * 255, Integer), CType(Rnd() * 255, Integer), CType(Rnd() * 255, Integer)))
Next
End Sub
But this doesn't work.
It errors on:
Code:
CType(FormControl, WebControl).BackColor
Saying it's an invalid typecast :(
Woka
Re: Changing back color of ALL web controls on a form.
Try Casting to the GetType() and then doing a test for the Property.
VB Code:
If CType(o,o.GetType()).BackColor Then
CType(o,o.GetType()).BackColor = System.Drawing.Color.White
End If
I haven't tried that, but it should work. The problem is the collection is of type Control and doesn't have to be of type WebControl.
Re: Changing back color of ALL web controls on a form.
o.GetType not defined :(
Woka
Re: Changing back color of ALL web controls on a form.
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Int32 = 0
Dim ctl As System.Web.UI.Control
While (i < Page.Controls.Count)
ctl = Page.Controls(i)
SetBackColor(ctl, System.Drawing.Color.Green)
i += 1
End While
End Sub
Public Sub SetBackColor(ByRef ctl As System.Web.UI.Control, ByVal c As System.Drawing.Color)
Try
ctl.GetType().GetProperty("BackColor").SetValue(ctl, c, Nothing)
Catch
'beep
End Try
If ctl.Controls.Count > 0 Then
Dim i As Int32 = 0
While (i < ctl.Controls.Count)
SetBackColor(ctl.Controls(i), c)
i += 1
End While
End If
End Sub
tested/works (skips the page it's self but if you start from the page it will get everything)
Re: Changing back color of ALL web controls on a form.
Just a tip too. Your not suposed to be able to use For Each to modify a value on an object just read. Thus why use it.....