Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

i have lots of this probubly 40 or more, is there an easier way to code this?

f6space.Show()
       With f6space
           .ForeColor = Color.Green
           .BackColor = Color.Black
       End With
       f7space.Show()
       With f7space
           .ForeColor = Color.Green
           .BackColor = Color.Black
       End With
       f8space.Show()
       With f8space
           .ForeColor = Color.Green
           .BackColor = Color.Black
       End With
       g1space.Show()
       With g1space
           .ForeColor = Color.Green
           .BackColor = Color.Black
       End With

ps its vb not c sorry

  • *Experts*
Posted

You could do this:

Dim lbl As Label
For Each lbl In Me.Controls
        lbl.ForeColor = Color.Green
        lbl.BackColor = Color.Black
Next

This will change all your labels on your form.

Posted (edited)

o cool thanks, i probubly should have said this before but is there a way to alternate the back colors without doing to manually? i'm making a checkerboard affect. Sorry that worked wonderfully by the way thanks again

 

wait while i have your attention i got a question. Is Dim like telling it to 'set'?

Edited by starcraft
  • *Experts*
Posted

Or you can do something like this to draw a checker board (im saying something like this becuase I dont know the size of checker board but i know the size of a chess board :) )

(this is assuming the size of each square is 45)

   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       SetStyle(ControlStyles.DoubleBuffer, True) 'to reduce flicker if there will be any
   End Sub

   Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
       For x As Integer = 0 To 360 Step 90
           For y As Integer = 0 To 360 Step 90
               e.Graphics.FillRectangle(New SolidBrush(Color.Black), x, y, 45, 45)
           Next
       Next
       For x As Integer = 45 To 315 Step 90
           For y As Integer = 0 To 360 Step 90
               e.Graphics.FillRectangle(New SolidBrush(Color.White), x, y, 45, 45)
           Next
       Next
       For x As Integer = 45 To 315 Step 90
           For y As Integer = 45 To 315 Step 90
               e.Graphics.FillRectangle(New SolidBrush(Color.Black), x, y, 45, 45)
           Next
       Next
       For x As Integer = 0 To 360 Step 90
           For y As Integer = 45 To 315 Step 90
               e.Graphics.FillRectangle(New SolidBrush(Color.White), x, y, 45, 45)
           Next
       Next
   End Sub

Each of the loops draws rows of squeres that alternate.

  • *Experts*
Posted

You could eliminate two of those loops by simply filling the whole form with white and then drawing black squares with the loop.

 

It might not affect performance, I don't know, but since it's happening every time the form is drawn, it might.

  • *Experts*
Posted

Each of those loops is drawing only 4 times.

If you set the background to white or black they board will get confusing becuse you will be able to see only one color coming out as some kind of a shape and the rest will not seem like part of the board :)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...