Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

label forecolor change doesn't work always

Status
Not open for further replies.

smeyer56

IS-IT--Management
Oct 16, 2002
206
US
I have an array of labels in VB2003. They are all black. When a user clicks on the label it should reset all labels to black and change the clicked one to red.

The code
Private Sub Label_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
For index = 0 To IPs
label(index).ForeColor = Color.Black
Next
label(GetIndex(sender)).ForeColor = Color.Red
End Sub

I use this on 2 seperate forms. On one, this sub seems to work half a dozen times and then stops working. On the other, it works once then stops.

Any ideas?
Thanks
 
When it stops I assume it doesn't come up with any error message just the colors are not changing? When I do it it works. You might show your array and how you add to your array. When I do it just like yours above I have no problems. Also I suggest you make a few changes. Don't call your array label. Label is a type and it could be causing some kind of conflict. Then also make at least one other change.

Code:
Private Sub Label_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        For index = 0 To IPs
            LabelList(index).ForeColor = Color.Black
        Next
        [RED]CType(sender, Label).ForeColor = Color.Red[/RED]
End Sub
Personally, I would really check the type just to make sure that it is a label before I did anything. That is one of those better safe now than sorry later kind of things.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Okay, maybe not so great. If I run this and do nothing else it works great. If I do anything with the form(resize, move, work on a different program) it will still change the CType to red but the labelList will not turn black so all the labels will eventually be red.

Private Sub Label_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
For index = 0 To IPs
labelList(index).ForeColor = Color.Black
Next
CType(sender, Label).ForeColor = Color.Red
End Sub


here is how I add the array.
For index = 0 To IPs
labelList(index) = New Label
labelList(index).Text = locations(index).Name
labelList(index).Name = "label(" & index & ")"
labelList(index).Location = New Point(x, y)
AddHandler labelList(index).Click, AddressOf Label_Click
Me.Controls.Add(labelList(index))
Next index


Thanks for your help.

 
The last one works no matter what because it works with the Label you clicked on rather than looking up which one in your list it should use. You must be doing something somewhere else with your lableList. It all works fine for me. How/where are you declaring your lableList?

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Here's a large chunk of the code

Imports System.Management
Imports System.drawing

Public Class frmNetworkStatus
Inherits System.Windows.Forms.Form
Dim running As Boolean
Const IPs As Integer = 7
Dim index As Integer
Dim locations(IPs) As cNetInfo
Dim circle(IPs) As Graphics
Dim labelList(IPs) As label
Const NAS As String = "192.168.100.10"
Const TIMEOUT As Integer = 5
#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Timer1 As System.Windows.Forms.Timer
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container
Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
'
'Timer1
'
Me.Timer1.Interval = 310000
'
'frmNetworkStatus
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(216, 293)
Me.Name = "frmNetworkStatus"
Me.Text = "Network Status"

End Sub

#End Region
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Height = IPs * 35 + 20
Timer1.Enabled = True
Call assignLocation()
running = False
Timer1.Start()

End Sub
Private Sub Form2_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Dim pencolor As New SolidBrush(Color.Red)
Dim pen As New Pen(Color.Black)
Dim x, y As Integer
x = 25
y = 25
If Not running Then
For index = 0 To IPs
pencolor.Color = Color.Yellow
circle(index) = Me.CreateGraphics
circle(index).FillEllipse(pencolor, x + 150, y, 13, 13)
circle(index).DrawEllipse(pen, x + 150, y, 13, 13)
labelList(index) = New Label
labelList(index).Text = locations(index).Name
labelList(index).Name = "labelList(" & index & ")"
labelList(index).Location = New Point(x, y)
AddHandler labelList(index).Click, AddressOf Label_Click

Me.Controls.Add(labelList(index))
y = y + 25
Next index
Else
y = 25
For index = 0 To IPs
If PingSilent(locations(index).IP) = 1 Then
pencolor.Color = Color.Red
circle(index).FillEllipse(pencolor, x + 150, y, 13, 13)
Else
pencolor.Color = Color.Green
circle(index).FillEllipse(pencolor, x + 150, y, 13, 13)
End If
circle(index).DrawEllipse(pen, x + 150, y, 13, 13)
y = y + 25
Next index
End If

End Sub
Private Sub Label_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
For index = 0 To IPs
labelList(index).ForeColor = Color.Black
Next
CType(sender, Label).ForeColor = Color.Red
End Sub


This code is supposed to be a simple form that pings some external and internal ips every 5 minutes or so and shows me graphicly if we have a problem. I have this working in VB6 but wanted to update it and add more funtionality to the program.
 
It has something to do with labelList being created in paint. Which is really bad any way because you don't want to keep making duplicate controls and adding them to the form each time you paint the form. Do at least this:

Code:
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Height = IPs * 35 + 20
        Timer1.Enabled = True
        Call assignLocation()
        running = False
        Timer1.Start()
[GREEN]        'Move to here[/GREEN]
[RED]        Dim x, y As Integer
        x = 25
        y = 25
        If Not running Then
            For index = 0 To IPs
                labelList(index) = New Label
                labelList(index).Text = locations(index).Name
                labelList(index).Name = "labelList(" & index & ")"
                labelList(index).Location = New Point(x, y)
                AddHandler labelList(index).Click, AddressOf Label_Click

                Me.Controls.Add(labelList(index))
                y = y + 25
            Next
[/RED]
    End Sub
    Private Sub Form2_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        Dim pencolor As New SolidBrush(Color.Red)
        Dim pen As New Pen(Color.Black)
        Dim x, y As Integer
        x = 25
        y = 25
        If Not running Then
            For index = 0 To IPs
                pencolor.Color = Color.Yellow
                circle(index) = Me.CreateGraphics
                circle(index).FillEllipse(pencolor, x + 150, y, 13, 13)
                circle(index).DrawEllipse(pen, x + 150, y, 13, 13)
[GREEN]                'From here[/GREEN]
                y = y + 25
            Next index
        Else
            y = 25
            For index = 0 To IPs
                If PingSilent(locations(index).IP) = 1 Then
                    pencolor.Color = Color.Red
                    circle(index).FillEllipse(pencolor, x + 150, y, 13, 13)
                Else
                    pencolor.Color = Color.Green
                    circle(index).FillEllipse(pencolor, x + 150, y, 13, 13)
                End If
                circle(index).DrawEllipse(pen, x + 150, y, 13, 13)
                y = y + 25
            Next index
        End If

    End Sub
    Private Sub Label_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        For index = 0 To IPs
            labelList(index).ForeColor = Color.Black
        Next
        CType(sender, Label).ForeColor = Color.Red
    End Sub
That is the only change you have to make. There may be a few other things that you really might want to do differently, but that is all I can check right now. Well one other thing I would suggest is that I really wouldn't declare index for the whole class. You could end up causing yourself errors later. Declare it each time you need it as just a safe practice.


-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
oops

this:
Code:
 Dim x, y As Integer
        x = 25
        y = 25
        If Not running Then
            For index = 0 To IPs
                labelList(index) = New Label
                labelList(index).Text = locations(index).Name
                labelList(index).Name = "labelList(" & index & ")"
                labelList(index).Location = New Point(x, y)
                AddHandler labelList(index).Click, AddressOf Label_Click

                Me.Controls.Add(labelList(index))
                y = y + 25
            Next

should be this:
Code:
 Dim x, y As Integer
        x = 25
        y = 25
            For index = 0 To IPs
                labelList(index) = New Label
                labelList(index).Text = locations(index).Name
                labelList(index).Name = "labelList(" & index & ")"
                labelList(index).Location = New Point(x, y)
                AddHandler labelList(index).Click, AddressOf Label_Click

                Me.Controls.Add(labelList(index))
                y = y + 25
            Next

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Are you sure? running is a boolean I have set to tell if the timer has started. Maybe I should be using a different variable name there? But that works fine while debugging.
 
Nevermind, I see what you were doing here now. I'll try. Thanks for your help.
 
np. glad to help.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top