首页 > 解决方案 > How do I open up a certain TabPage based on data in a ListView?

问题描述

I have created 2 forms: Form1 and Form2. Form1 contains my TabControl which has 5 TabPages. There are 5 TabPages: 'Special Incident Entry', 'Accident Incident Entry', 'General Incident Entry', 'Injury Incident Entry', and 'Workplace Violent Incident Entry'.

My Form2 contains a ListView. I created a column header named "Entry Type". Now there are 5 entry types: 'Special Incident Entry', 'Accident Incident Entry', 'General Incident Entry', 'Injury Incident Entry', and 'Workplace Violent Incident Entry'. What I'm trying to accomplish is this:

When the user double clicks on any item in the ListView, based on what is stated under the column "Entry Type" for that item, the corresponding TabPage opens containing form controls.

I've tried Form1.TabControl1.SelectedIndex = 1 and that opens up the TabPage with the index 1. How do I get the 'Entry Type' input from the ListView on Form2 then open the respective TabPage on Form1? Ive added test data into my ListView to try and figure it out.

标签: vb.netlistviewtabcontroltabpage

解决方案


使用双击事件并获取相应子项中的值。然后在 Select Case 中使用该值来选择正确的选项卡。

Private Sub ListView1_DoubleClick(sender As Object, e As EventArgs) Handles ListView1.DoubleClick
        'Use whatever SubItem index is the appropriate column
        Dim strTabName As String = ListView1.SelectedItems(0).SubItems(0).Text
        Select Case strTabName
            Case "Special Incident Entry"
                Form1.TabControl1.SelectedIndex = 0
            Case "Accident Incident Entry"
                Form1.TabControl1.SelectedIndex = 1
            Case "General Incident Entry"
                Form1.TabControl1.SelectedIndex = 2
            Case "Injury Incident Entry"
                Form1.TabControl1.SelectedIndex = 3
            Case "Workplace Violent Incident Entry"
                Form1.TabControl1.SelectedIndex = 4
            Case Else
                MessageBox.Show("Please try double clicking again.")
        End Select
    End Sub

推荐阅读