列表框项目背景色

时间:2016-03-08 08:57:48

标签: vb.net listbox listboxitems

首先,我需要一个答案(VB.net Try Catch. Keep the Loop after the Catch)来执行下一个问题......

阅读上面的链接。我在那里解释一切。

我想为获得Catch的项目的背景着色,以便我知道其他类型中的产品不存在。

正在做的是绘制所有ListBox

  

我在Form3中:

ConnectionString.Open()

        Dim ds As New DataSet
        Dim dt As New DataTable
        ds.Tables.Add(dt)
        Dim da As OleDbDataAdapter

        For i = 0 To Form2.ListBox1.Items.Count - 1

            Dim str As String = Form2.ListBox1.Items(i).ToString
            Dim col As String = str.Substring(2, 3)

            If Form1.MESA_btn.Enabled = False Then
                da = New OleDbDataAdapter("SELECT * FROM [Mesa$] WHERE Decor = '" & CB_col.Text & "' and Format([Ref], ""000000000"") like '__" & col & "%'", ConnectionString)
            Else
                da = New OleDbDataAdapter("SELECT * FROM [Gift$] WHERE Decor = '" & CB_col.Text & "' and Format([Ref], ""000000000"") like '__" & col & "%'", ConnectionString)
            End If

            da.Fill(dt)
            Try
                ListBox1.Items.Add(dt.Rows(i).Item(0))
                ListBox2.Items.Add(dt.Rows(i).Item(1))
                ListBox3.Items.Add(dt.Rows(i).Item(3))
                ListBox4.Items.Add(dt.Rows(i).Item(5))
                ListBox5.Items.Add(Form2.ListBox5.Items.Item(i))
                ListBox6.Items.Add(ListBox4.Items.Item(i) * ListBox5.Items.Item(i))
            Catch ex As Exception
                Form2.ListBox1.BackColor = Color.Yellow
                Form2.ListBox2.BackColor = Color.Yellow
                Form2.ListBox3.BackColor = Color.Yellow
                Form2.ListBox4.BackColor = Color.Yellow
                Form2.ListBox5.BackColor = Color.Yellow
                Form2.ListBox6.BackColor = Color.Yellow
            End Try
        Next

ConnectionString.Close()

这些是程序运行的图像(它们被编辑):

Form1

Form2

Form3

2 个答案:

答案 0 :(得分:0)

我不认为这是最好的做事方法。虽然这可以解决您的问题。

Dim currentList as Object
            Try
                currentList = Form2.ListBox1
                ListBox1.Items.Add(dt.Rows(i).Item(0))
                currentList = Form2.ListBox2
                ListBox2.Items.Add(dt.Rows(i).Item(1))
                currentList = Form2.ListBox3
                ListBox3.Items.Add(dt.Rows(i).Item(3))
                currentList = Form2.ListBox4
                ListBox4.Items.Add(dt.Rows(i).Item(5))
                currentList = Form2.ListBox5
                ListBox5.Items.Add(Form2.ListBox5.Items.Item(i))
                currentList = Form2.ListBox6
                ListBox6.Items.Add(ListBox4.Items.Item(i) * ListBox5.Items.Item(i))
            Catch ex As Exception
                currentList.BackColor = Color.Yellow
            End Try

答案 1 :(得分:0)

它在锡上做了它所说的。您要求绘制整个ListBox并将其全部绘制。

我建议使用DataGridView而不是ListBox。如果设置:

Me.DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
Me.DataGridView1.MultiSelect = False

它的外观和行为与ListBox非常相似。然后,您可以设置:

Me.DataGridView1.row(ir).DefaultCellStyle.BackColor = Color.LightSalmon

因此可以毫无困难地绘制各行。