在VB 2010中填充选项卡控件内的ListView

时间:2011-05-14 17:43:12

标签: vb.net

我是Visual Basic中的新手。我在表单中有一个选项卡控件,在里面我放了一个listview。每次点击每个标签时,我想要做的就是填充列表视图。但我不知道该怎么做。

有人可以帮助我吗?

以下是我的代码

Public Class Form2

    Private Sub PopulateRecords()
        Dim TSR As New ADODB.Recordset
        Dim STRSQL As String = ""
        STRSQL &= "SELECT * from Records " & vbCrLf
        STRSQL &= "WHERE (Customer_name like '" & TextBox1.Text & "%' or Item_name like '%" & TextBox1.Text & "%' or Item_code like '%" & TextBox1.Text & "%' or Price like '%" & TextBox1.Text & "%' or Qty like '%" & TextBox1.Text & "%' or Total_price like '%" & TextBox1.Text & "%' or Date_started like '%" & TextBox1.Text & "%' or Date_finish like '%" & TextBox1.Text & "%')" & vbCrLf
        STRSQL &= "order by Customer_name, Item_name" & vbCrLf
        TSR.Open(STRSQL, CNN, 1, 3)
        ListView1.Items.Clear()
        While Not TSR.EOF
            ListView1.Items.Add(TSR.Fields("Customer_name").Value)
            ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(TSR.Fields("Item_name").Value)
            ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(TSR.Fields("Item_code").Value)
            ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(TSR.Fields("Price").Value)
            ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(TSR.Fields("Qty").Value)
            ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(TSR.Fields("Total_price").Value)
            ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(TSR.Fields("Date_started").Value)
            ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(TSR.Fields("Date_finish").Value)
            ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(TSR.Fields("ID").Value)
            TSR.MoveNext()
        End While
        TSR.Close()
    End Sub

    Private Sub TabPage1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabPage1.Click
        Call PopulateRecords()
    End Sub


End Class

2 个答案:

答案 0 :(得分:0)

您应该尝试响应父级的SelectedIndexChanged event(TabControl)。当用户在选项卡之间移动时会触发此操作。单击页面本身时会触发TabPage.Click,而不是在选项卡更改为。

时触发

答案 1 :(得分:0)

使用标签控件的 SelectedIndexChanged 事件。

    private sub tabControl_SelectedIndexChanged(Byval sender as System.Object, _
            Byval e as System.EventArgs) Handles tabControl.SelectedIndexChanged
    {
        PopulateRecords()
    }

您还可以识别您正在查看的标签页:

    byval page as TabPage = tabControl.TabPages(tabControl.SelectedIndex)

希望这有助于= D