当改变列表框背景颜色时,listbox会自行取消select

我有一个列表框和代码,以确保它(和其他元素)被选中。 我在代码中添加了一个ELSE,这样如果ListBox有一个选定的项目,它会将背景颜色更改为白色。 如果先前尝试的条目是重复的(将BG更改为红色),则需要执行此操作,但只是将其设置为默认值就更容易了。

我的comboBox和textBox不这样做。 任何想法我可以做不同的只有Initialize函数清除它?

谢谢,

这是清除listBox选定项目的代码片段。 注释上面的行似乎是造成这一点。

编辑:它会取消select每当我改变背景颜色。 它也取消select何时造成重复,这不会更改背景颜色。 所以列表框在很多情况下是自行取消的。 如果我能find其中一个的原因(其中两个在这里列出),那么也许我可以解决第三个问题。

Function HighlightEmpty(ByVal nameSelect As Boolean, ByVal comboSelect As Boolean, ByVal listSelect As Boolean) As Boolean ' Highlight empty fields If Not nameSelect Then Enter_New_DTC_Form.SignalNameTxtBox.BackColor = RGB(255, 0, 0) Else Enter_New_DTC_Form.SignalNameTxtBox.BackColor = RGB(255, 255, 255) End If If Not comboSelect Then Enter_New_DTC_Form.ComboBox1.BackColor = RGB(255, 0, 0) Else Enter_New_DTC_Form.ComboBox1.BackColor = RGB(255, 255, 255) End If If Not listSelect Then Enter_New_DTC_Form.ListBox1.BackColor = RGB(255, 0, 0) Else **'This is where it breaks** Enter_New_DTC_Form.ListBox1.BackColor = RGB(255, 255, 255) End If ' Set focus to first empty field on form If Not nameSelect Then Enter_New_DTC_Form.SignalNameTxtBox.SetFocus ElseIf Not comboSelect Then Enter_New_DTC_Form.ComboBox1.SetFocus ElseIf Not listSelect Then Enter_New_DTC_Form.ListBox1.SetFocus End If ' Return boolean to trigger message HighlightEmpty = Not nameSelect Or Not comboSelect Or Not listSelect End Function 

我意识到原来的post已经超过2年了,但是它仍然是相关的,因为我使用Excel 2013和VBA 7.1来经历这个问题。 在编码方面,我只是一个业余爱好者,但是我提出了下面的代码作为解决方法。

数据validation完成后,将使用此代码,并准备移至下一步。

您最终find所选列表项目的索引,将其添加1,将所选列表项目设置为下一个/上一个项目,然后将其设置回实际项目。 我不知道为什么这是必要的,但它的作品。

 'Determine the currently selected list item and set j to the index of it For i = 0 To ListBox1.ListCount - 1 If ListBox1.Selected(i) Then j = i Next i 'Set j to 1 more than the index of the selected list item j = j + 1 'If the selected list item isn't the last one, select the next item, 'then re-select the original item If j < ListBox1.ListCount Then ListBox1.BackColor = RGB(255, 255, 255) 'Set the background color to white ListBox1.ForeColor = RGB(0, 0, 0) 'and the foreground color to black ListBox1.ListIndex = (j) 'Select the NEXT item in the list ListBox1.ListIndex = (j - 1) 'Re-select the ORIGINAL item 'If the selected list item is the last one, select the previous item, 'then re-select the original item ElseIf j = ListBox1.ListCount Then ListBox1.BackColor = RGB(255, 255, 255) 'Set the background color to white ListBox1.ForeColor = RGB(0, 0, 0) 'Set the foreground color to black ListBox1.ListIndex = (j - 2) 'Select the PREVIOUS item in the list ListBox1.ListIndex = (j - 1) 'Re-select the ORIGINAL item End If 

希望这可以帮助。 我花了很多时间研究,找不到根本原因的解决scheme,但是在我自己的电子表格中实现了这个代码。

一个可能的解决scheme是存储select,更改BackColor然后重新应用select;

 Public Sub UpdateBackgroundColor() Dim sel() As Boolean: sel = GetSelectedIndexs() list_box.BackColor = &H80000005 SetSelectedIndexs sel End Sub Private Function GetSelectedIndexs() As Boolean() If list_box.ListCount > 0 Then ReDim sel(0 To list_box.ListCount - 1) As Boolean Dim i As Integer For i = 0 To list_box.ListCount - 1 sel(i) = list_box.Selected(i) Next GetSelectedIndexs = sel Else ReDim GetSelectedIndexs(0) End If End Function Private Function SetSelectedIndexs(sel() As Boolean) Dim i As Integer For i = LBound(sel) To UBound(sel) list_box.Selected(i) = sel(i) Next End Function