【问题标题】:Listbox Items backcolor列表框项目背景色
【发布时间】:2016-03-08 08:57:48
【问题描述】:

首先,我需要一个答案 (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

【问题讨论】:

  • 设置值的时候为什么不直接画每个列表框的背景呢?那么那些未上漆的(白色)意味着当时发生了错误?
  • 但我只想画有错误的那个。这样我就可以看到缺少的产品是什么
  • 你能去掉blockquote引起的banner效果吗?谢谢

标签: vb.net listbox listboxitems


【解决方案1】:

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

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

【讨论】:

  • 解决不了之前的问题。绘制了 Form3 的所有ListBox1。我想绘制 Form2 中的ListBoxes。
【解决方案2】:

它会按照锡上所说的去做。您要求绘制整个 ListBox,它会绘制所有内容。

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

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

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

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

因此绘制单独的行没有任何麻烦。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-14
    • 1970-01-01
    • 2011-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多