2011-06-21 58 views
1
我有使用下面的代碼高亮找到的ListView項問題

VB.net列表視圖中選擇

If lstMaster.View = View.Details AndAlso lstMaster.Items.Count > 0 Then 
    Dim lvi As ListViewItem = lstMaster.FindItemWithText(txtSearchSR.Text, True, 0) 

    If lvi IsNot Nothing Then 
     MsgBox("found") 
     lvi.ListView.Items(0).Selected = True 'Does not seem to work... 
    End If 

End If 

我如何highliht找到列?

大衛

+0

我看到你有一個「if語句」來決定是否應該突出顯示該項目。你是否檢查過lvi的值以查看FindItemWithText是否真的找到了一個項目。 –

+0

gosh darn!只是意識到你有一個MsgBox(「發現」)..是顯示爲發現? –

回答

1

明白了! :o)

lstMaster.Items(lvi.Index).Selected = True 
lstMaster.Select() 
lstMaster.SelectedItems.Item(0).EnsureVisible() 
1

你需要

lvi.Selected = True 

從你的片段,

lvi.ListView.Items(0)總是返回在ListView第一個ListViewItem。

+0

它突出顯示了最上面一排,無論它位於列表中的什麼位置...... – StealthRT

+0

@StealthRT查看我的編輯。 –

+0

不起作用,對不起:o( – StealthRT

1

嘗試設置子項

lvi.Items[0].UseItemStyleForSubItems = false 
lvi.Items[0].SubItems[0].BackColor = Color.Black  
lvi.Items[0].SubItems[0].ForeColor = Color.White 

如果不行嘗試

lvi.UseItemStyleForSubItems = false 
lvi.SubItems.Add(new ListViewItem.ListViewSubItem(lvi,"subitem", Color.Black, Color.White, lvi.Font)) 
+0

在這個例子中都不起作用。 – StealthRT