2010年7月18日 星期日

[VB.NET] Listbox 和 Listview 自動向下滾動 程式碼

功能:
執行這段代碼後,Listbox 或 Listview 便會自動下拉到最底層,而且不會改變選中的項目

使用方法:
在新增項目後執行,或是使用Timer定時執行

For Listbox


        ListBox1.TopIndex = ListBox1.Items.Count - 1


For Listview

        ListView1.EnsureVisible(ListView1.Items.Count - 1)


如果覺得每次新增項目後都還要在嘉義航捲動的代碼看起來太冗長,可以作成一氣呵成的函數再使用,例如

Public Class Form1

    Public Function PutMsg(ByVal Msg As String)
        ListBox1.Items.Add(Msg)
        ListBox1.TopIndex = ListBox1.Items.Count - 1
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        PutMsg("你好")
    End Sub
End Class

1 則留言: