Hi all,

Does anyone know how to change the color (or highlight) a part of a listbox)??

I need to have the negative amount (if any) highlight automatically during an if statement.

I have looked through my textbook for VB, and the instructor does not think I can because the Outpu string is a variable and not an object

I have a bit of the code at the bottom

anyone have any suggestions??

Thanks in advance

Angela

.........................

strHoursWorked = InputBox("Enter the # of Hours Worked by Employee " & intCount, "Hours Worked")
            strHourlyPay = FormatCurrency(InputBox("Enter the Hourly Amount paid  to Employee " & intCount, "Hours Pay"))
            GrossPay = FormatCurrency(strHoursWorked * strHourlyPay)
            strPercentState = InputBox("Enter the Percent State Tax to take out for Employee" & intCount, "State Percent Withheld")
            StateWH = FormatCurrency(CDec((strPercentState * GrossPay) * 0.01))
            strPercentFederal = InputBox("Enter the Percent Federal Tax to take out for Employee" & intCount, "Federal Percent Withheld")
            FederalWH = FormatCurrency(CDec((strPercentFederal * GrossPay) * 0.01))
            strPercentFICA = InputBox("Enter the Percent FICA Tax to take out for Employee" & intCount, "Fica Percent Withheld")
            FICAwh = FormatCurrency(CDec((strPercentFICA * GrossPay) * 0.01))
            NetPay = FormatCurrency((GrossPay - StateWH - FederalWH - FICAwh))
            TotalTaxes = FormatCurrency(CDec(FederalWH) + CDec(StateWH) + CDec(FICAwh))

            strOut += ("Employee " & intCount)
            strOut += ("  " & strHoursWorked & "=")
            strOut += ("Hours Worked" & ",")
            strOut += ("  " & strHourlyPay & "=")
            strOut += (" Hourly Pay " & ",")
            strOut += ("  " & strPercentState & "%=")
            strOut += (" State Tax" & ",")
            strOut += ("  " & strPercentFederal & "%=")
            strOut += (" Federal Tax" & ",")
            strOut += ("   " & strPercentFICA & "%=")
            strOut += (" FICA Tax")

            strOutput += ("Employee " & intCount)
            strOutput += ("  " & GrossPay & "=")
            strOutput += ("Gross Pay" & ",")
            strOutput += ("  " & StateWH & "=")
            strOutput += ("State Taxes" & ",")
            strOutput += ("  " & FederalWH & "=")
            strOutput += ("Federal Taxes" & ",")
            strOutput += ("  " & FICAwh & "=")
            strOutput += ("FICA Taxes" & ",")
            strOutput += ("  " & NetPay & "=")
            strOutput += ("NetPay")

            If NetPay <= 0 Then
                MessageBox.Show("Withholdings are higher than Gross Pay")
                lstPayroll.Items.Add(strOut)
                lstPayroll.Items.Add("")
                lstPayroll.Items.Add("ERROR...." + strOutput + "....ERROR")
                lstPayroll.Items.Add("")
                strOutput = Color.Red
            Else
                lstPayroll.Items.Add(strOut)
                lstPayroll.Items.Add("")
                lstPayroll.Items.Add(strOutput)
                lstPayroll.Items.Add("")
            End If
        Next
    End Sub
End Class

*** I am wanting the the netpay to be flashing red or text to be red when the taxes are larger than the grosspay........I can have the entire listbox highlight, but I only want the employee who this applies to to be highlighted??

anyone have any ideas????


Thanks ;)
ANG

Hi,

As far as i'm aware you can't have multiple font / colour declerations for a list box. Best option is to use the data grid (.NET) or msflexgrid (VB6)

Green2Go :P

Set the DrawMode of the listbox to OwnerDrawFixed or OwnerDrawVariable , then add this code to the DrawItem event:

Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
        e.DrawBackground()
        Dim textBrush As Brush = SystemBrushes.ControlText
        Dim drawFont As Font = e.Font
        If (ListBox1.Items.Item(e.Index) < 0) Then
            textBrush = Brushes.Red
            drawFont = New Font(drawFont.FontFamily, drawFont.Size, FontStyle.Bold)
        ElseIf (e.State And DrawItemState.Selected) > 0 Then
            textBrush = SystemBrushes.HighlightText
        End If
        e.Graphics.DrawString(ListBox1.Items.Item(e.Index).ToString(), drawFont, textBrush, e.Bounds.X, e.Bounds.Y)
    End Sub

I've tested this code and it works just fine. This is a solution to your question.

This article has been dead for over six months. Start a new discussion instead.