Results 1 to 5 of 5

Thread: Changing back color of ALL web controls on a form.

  1. #1

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    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

  2. #2
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    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:
    1. If CType(o,o.GetType()).BackColor Then
    2.     CType(o,o.GetType()).BackColor = System.Drawing.Color.White
    3. 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.
    Magiaus

    If I helped give me some points.

  3. #3

  4. #4
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    Re: Changing back color of ALL web controls on a form.

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim i As Int32 = 0
    3.         Dim ctl As System.Web.UI.Control
    4.  
    5.         While (i < Page.Controls.Count)
    6.             ctl = Page.Controls(i)
    7.             SetBackColor(ctl, System.Drawing.Color.Green)
    8.             i += 1
    9.         End While
    10.     End Sub
    11.  
    12.     Public Sub SetBackColor(ByRef ctl As System.Web.UI.Control, ByVal c As System.Drawing.Color)
    13.         Try
    14.             ctl.GetType().GetProperty("BackColor").SetValue(ctl, c, Nothing)
    15.         Catch
    16.             'beep
    17.         End Try
    18.         If ctl.Controls.Count > 0 Then
    19.             Dim i As Int32 = 0
    20.             While (i < ctl.Controls.Count)
    21.                 SetBackColor(ctl.Controls(i), c)
    22.                 i += 1
    23.             End While
    24.         End If
    25.     End Sub

    tested/works (skips the page it's self but if you start from the page it will get everything)
    Magiaus

    If I helped give me some points.

  5. #5
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    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.....
    Magiaus

    If I helped give me some points.

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