william york
william york

Reputation: 1

newline in a list box

OK I am having problems with writing a newline in a listbox. Its gotta be something simple and I cannot figure it out.

dim decInput1 as decimal = 40.65
dim decInput2 as decimal = 500.90
dim decInput3 as decimal = 450.89
dim strOutput as string

strOutput = " Here is decInput1 " & decInput1.tostring("c")
strOutput &= " Here is decInput2 " & decInput2.tostring("c")
strOutput &= " here is decInput3 " & decInput3.tostring("c")

lstBox.items.clear()
lstBox.items.add(strOutput)

the output i want should be
40.65
500.90
450.89 but i keep getting it all in one line. this isnt code out of the actual program I am working on but similar. thank you for you help


This is my actual program. Im trying to display in 2 columns for the title and the amounts in 4 diferent listbox for 4 diferent employees. and i am at this point so lost. as u can see its not to unifom when it comes to tthe dif strEmployee statements where ive tried several diferent techniques. I dont need an answer. Just maybe an exapmle statement in as simplest form u can for me please. thank you

Private Sub btnEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnter.Click
    Dim intCount1 As Integer = 1                          ' count increment
    Dim intCount2 As Integer = 1
    Dim intColumn As Integer = 1
    Dim decEmployee4Hours As Decimal = 0
    Dim decPayRate As Decimal = 0                        ' stores pay rate
    Dim decEmployee1Pay As Decimal = 0                   ' employee 1 pay rate
    Dim decEmployee2Pay As Decimal = 0
    Dim decEmployee3Pay As Decimal = 0
    Dim decEmployee4Pay As Decimal = 0
    Dim decHours As Decimal = 0                          ' stores hours
    Dim decEmployee1Hours As Decimal = 0                 ' stores the hours for each employee
    Dim decEmployee2Hours As Decimal = 0
    Dim decEmployee3Hours As Decimal = 0
    Dim strInput As String                               ' stores string
    Dim dblStateTax As Double = CDbl(txtStateTax.Text)
    Dim dblFedTax As Double = CDbl(txtFedTax.Text)
    Dim dblFICA As Double = CDbl(txtFICA.Text)
    Dim decEmployee1Gross As Decimal = 0
    Dim decEmployee2Gross As Decimal = 0
    Dim decEmployee3Gross As Decimal = 0
    Dim decEmployee4Gross As Decimal = 0
    Dim decEmployee1StateTax As Decimal = 0
    Dim decEmployee2StateTax As Decimal = 0
    Dim decEmployee3StateTax As Decimal = 0
    Dim decEmployee4StateTax As Decimal = 0
    Dim decEmployee1FedTax As Decimal = 0
    Dim decEmployee2FedTax As Decimal = 0
    Dim decEmployee3FedTax As Decimal = 0
    Dim decEmployee4FedTax As Decimal = 0
    Dim decEmployee1FICA As Decimal = 0
    Dim decEmployee2FICA As Decimal = 0
    Dim decEmployee3FICA As Decimal = 0
    Dim decEmployee4FICA As Decimal = 0
    Dim decEmployee1NetPay As Decimal = 0
    Dim decEmployee2NetPay As Decimal = 0
    Dim decEmployee3NetPay As Decimal = 0
    Dim decEmployee4NetPay As Decimal = 0
    Dim strEmployee1 As String
    Dim strEmployee2 As String
    Dim strEmployee3 As String
    Dim strEmployee4 As String


    ' get pay rate for each employee
    For intCount1 = 1 To 4 Step 1

        strInput = InputBox("Enter Pay Rate for employee " & intCount1.ToString())
        If intCount1 = 1 Then
            decEmployee1Pay = CDec(strInput)
            lblEmployee1A.Text = decEmployee1Pay.ToString("c")
        ElseIf intCount1 = 2 Then
            decEmployee2Pay = CDec(strInput)
            lblEmployee2A.Text = decEmployee2Pay.ToString("c")
        ElseIf intCount1 = 3 Then
            decEmployee3Pay = CDec(strInput)
            lblEmployee3A.Text = decEmployee3Pay.ToString("c")
        ElseIf intCount1 = 4 Then
            decEmployee4Pay = CDec(strInput)
            lblEmployee4A.Text = decEmployee4Pay.ToString("c")
        End If

    Next

    For intCount2 = 1 To 4 Step 1
        strInput = InputBox("Enter the hours for employee " & intCount2.ToString())
        If intCount2 = 1 Then
            decEmployee1Hours = CDec(strInput)
            lblEmployee1B.Text = decEmployee1Hours.ToString()
        ElseIf intCount2 = 2 Then
            decEmployee2Hours = CDec(strInput)
            lblEmployee2B.Text = decEmployee2Hours.ToString()
        ElseIf intCount2 = 3 Then
            decEmployee3Hours = CDec(strInput)
            lblEmployee3B.Text = decEmployee3Hours.ToString()
        ElseIf intCount2 = 4 Then
            decEmployee4Hours = CDec(strInput)
            lblEmployee4B.Text = decEmployee4Hours.ToString()
        End If

    Next

    decEmployee1Gross = decEmployee1Pay * decEmployee1Hours
    decEmployee1StateTax = decEmployee1Gross * dblStateTax
    decEmployee1FedTax = decEmployee1Gross * dblFedTax
    decEmployee1FICA = decEmployee1Gross * dblFICA
    decEmployee1NetPay = decEmployee1Gross - decEmployee1FedTax - decEmployee1StateTax -decEmployee1FICA

    lstBox1.Items.Clear()

    For intColumn = 1 To 2 Step 1
        If intColumn = 1 Then
            strEmployee1 = " Gross pay: "
            lstBox1.Items.Add(strEmployee1)
            strEmployee1 = " State Income tax: "
            lstBox1.Items.Add(strEmployee1)
            strEmployee1 = " Federal Income Tax: "
            lstBox1.Items.Add(strEmployee1)
            strEmployee1 = " FICA: "
            lstBox1.Items.Add(strEmployee1)
            strEmployee1 = " Net Pay: "
            lstBox1.Items.Add(strEmployee1)
        ElseIf intColumn = 2 Then
            strEmployee1 = decEmployee1Gross.ToString("c")
            lstBox1.Items.Add(strEmployee1)
            strEmployee1 = decEmployee1StateTax.ToString("c")
            lstBox1.Items.Add(strEmployee1)
            strEmployee1 = decEmployee1FedTax.ToString("c")
            lstBox1.Items.Add(strEmployee1)
            strEmployee1 = decEmployee1FICA.ToString("c")
            lstBox1.Items.Add(strEmployee1)

        End If
    Next

    decEmployee2Gross = decEmployee2Pay * decEmployee2Hours
    decEmployee2StateTax = decEmployee2Gross * dblStateTax
    decEmployee2FedTax = decEmployee2Gross * dblFedTax
    decEmployee2FICA = decEmployee2Gross * dblFICA
    decEmployee2NetPay = decEmployee2Gross - decEmployee2FedTax - decEmployee2StateTax - decEmployee2FICA

    strEmployee2 = " Employee Gross pay " & vbCrLf & "    " & decEmployee2Gross.ToString("c") & vbCrLf & "aaaaa"
    strEmployee2 &= " Employee State Income tax " & vbCrLf & "     " & decEmployee2StateTax.ToString("c") & vbCrLf
    strEmployee2 &= " Employee Federal Income Tax " & vbCrLf & "     " & decEmployee2FedTax.ToString("c") & vbCrLf
    strEmployee2 &= " Employee FICA " & vbCrLf & "     " & decEmployee2FICA.ToString("c")
    strEmployee2 &= " Net Pay " & vbCrLf & "     " & decEmployee2NetPay.ToString("c")

    lstBox2.Items.Clear()


    lstBox2.Items.Add(strEmployee2)

    decEmployee3Gross = decEmployee3Pay * decEmployee3Hours
    decEmployee3StateTax = decEmployee3Gross * dblStateTax
    decEmployee3FedTax = decEmployee3Gross * dblFedTax
    decEmployee3FICA = decEmployee3Gross * dblFICA
    decEmployee3NetPay = decEmployee3Gross - decEmployee3FedTax - decEmployee3StateTax - decEmployee3FICA

    strEmployee3 = " Employee Gross pay " & vbCrLf & "    " & decEmployee3Gross.ToString("c") & vbCrLf
    strEmployee3 &= " Employee State Income tax " & vbCrLf & "     " & decEmployee3StateTax.ToString("c") & vbCrLf
    strEmployee3 &= " Employee Federal Income Tax " & vbCrLf & "     " & decEmployee3FedTax.ToString("c") & vbCrLf
    strEmployee3 &= " Employee FICA " & vbCrLf & "     " & decEmployee3FICA.ToString("c")
    strEmployee3 &= " Net Pay " & vbCrLf & "     " & decEmployee3NetPay.ToString("c")

    lstBox3.Items.Clear()

    lstBox3.Items.Add(strEmployee3)

    decEmployee4Gross = decEmployee4Pay * decEmployee4Hours
    decEmployee4StateTax = decEmployee4Gross * dblStateTax
    decEmployee4FedTax = decEmployee4Gross * dblFedTax
    decEmployee4FICA = decEmployee4Gross * dblFICA
    decEmployee4NetPay = decEmployee4Gross - decEmployee4FedTax - decEmployee4StateTax - decEmployee4FICA

    strEmployee4 = " Employee Gross pay " & vbNewLine & " " & decEmployee4Gross.ToString("c") & vbNewLine
    strEmployee4 += " Employee State Income tax " & vbNewLine & " " & decEmployee4StateTax.ToString("c") & vbNewLine
    strEmployee4 += " Employee Federal Income Tax " & vbNewLine & " " & decEmployee4FedTax.ToString("c") & vbNewLine
    strEmployee4 += " Employee FICA " & vbNewLine & " " & decEmployee4FICA.ToString("c") & vbNewLine
    strEmployee4 += " Net Pay " & vbNewLine & " " & decEmployee4NetPay.ToString("c")

    lstBox4.Items.Clear()

    lstBox4.Items.Add(strEmployee4)
End Sub

Upvotes: 0

Views: 13445

Answers (2)

LarsTech
LarsTech

Reputation: 81620

You can, but it's a little bit of work.

You have to set the DrawMode = OwnerDrawVariable, and then subscribe to the DrawItem and MeasureItem events.

In it's simplest form:

Protected Overrides Sub OnLoad(e As EventArgs)
  ListBox1.DrawMode = DrawMode.OwnerDrawVariable
  ListBox1.Items.Add("This is line one." + Environment.NewLine + "This is line two.")
  ListBox1.Items.Add("This is another line.")
  MyBase.OnLoad(e)
End Sub

Private Sub ListBox1_MeasureItem(sender As Object, e As MeasureItemEventArgs) Handles ListBox1.MeasureItem
  e.ItemHeight = e.Graphics.MeasureString(ListBox1.Items(e.Index).ToString, ListBox1.Font).Height
End Sub

Private Sub ListBox1_DrawItem(sender As Object, e As DrawItemEventArgs) Handles ListBox1.DrawItem
  e.DrawBackground()
  e.Graphics.DrawString(ListBox1.Items(e.Index).ToString, ListBox1.Font, Brushes.Black, e.Bounds)
End Sub

Upvotes: 1

Joel Coehoorn
Joel Coehoorn

Reputation: 415810

The only way to have multiple lines in a listbox is to add each line as a separate item. The only work around is to build your own control that only looks like a listbox, and that's not at all trivial or simple.

Upvotes: 0

Related Questions