2014-05-23 19 views
0

我有一個列表框和代碼來確保它(和其他元素)被選中。我在代碼中添加了ELSE,這樣如果ListBox有一個選定的項目,它會將背景顏色更改爲白色。如果先前嘗試的條目是重複的(將BG更改爲紅色),則需要執行此操作,但只是將其設置爲默認值就會更容易。當改變列表框背景色時,listbox會自動取消選擇

我的comboBox和textBox不這樣做。任何想法我可以做不同的做法,只有Initialize函數清除它?

感謝,

這裏的代碼段將清除列表框中選擇的項目。註釋上面的行似乎是造成這種情況。

編輯:它會取消選擇每當我更改背景顏色。它也取消選擇何時導致重複,這不會更改背景顏色。因此在許多情況下,列表框自行取消選擇。如果我能找到其中一個原因(其中兩個列在這裏),那麼也許我可以解決第三個問題。

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 
+0

你如何運行此功能?在什麼事件? – stenci

+0

打開Click_Save按鈕(在窗體上)。每次保存命中時,在填充行並檢查它是否重複(發送消息並刪除行)之前,它會檢查以確保選擇了必填字段。如果不是,則調用該函數來突出顯示空白字段,然後將焦點放在第一個字段上。然後它將退出保存子,以便他們可以再次嘗試。 – JSM

+0

爲什麼你不用Sub來代替。上面的代碼更適合它。你都可以傳遞值。儘管函數不是100%不適用於執行對象方法和屬性(基本上用於返回值),但Subs更好。 – L42

回答

2

我知道原帖是2歲以上,但它仍然相關,因爲我經歷過使用Excel 2013和VBA 7.1這個確切的問題。在編碼方面,我只是一個業餘愛好者,但我提出了以下代碼作爲解決方法。

在驗證數據並且您準備移動到下一步之後,將使用此代碼。

您最終找到所選列表項目的索引,並將其添加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 

希望這會有所幫助。我花了很多時間研究,找不到根本原因的解決方案,但是在我自己的電子表格中實現了這些代碼。

+0

好的電話Sometowngeek。我提出了你的建議改變。謝謝。 – Josh

+0

在這種情況下可能不需要。我提出了這種改變,因爲如果它依賴於單個變量或屬性,並且該操作取決於它所具有的功能,那麼在單個塊中遵循「If ... ElseIf ... Else ...」是一種很好的做法。例如,如果顏色是紅色,請停止;否則,如果顏色是黃色的,做一個筋斗;否則,去買一袋葡萄。 – Sometowngeek

0

一個可能的解決方案是存儲選擇,更改BackColor然後重新應用選擇;

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 
相關問題