⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 customizecolumnsform.vb

📁 一个管理系统
💻 VB
📖 第 1 页 / 共 3 页
字号:
            Get
                Return m_RefDataGridColumnStyle
            End Get
        End Property

        Public Overrides Function ToString() As String
            'work around for priority column
            If m_RefDataGridColumnStyle.HeaderText = "!" Then Return m_ResourceManager.GetString("Priority")

            Return m_RefDataGridColumnStyle.HeaderText
        End Function
    End Class
#End Region

#Region " Functions "
    Private Sub ResetListBoxes()
        ' Populate the list boxes from the current collection.
        lbAvailable.Items.Clear()
        lbDisplayed.Items.Clear()

        Dim cs As DataGridColumnStyle
        For Each cs In m_dgTableStyle.GridColumnStyles
            If cs.Width = 0 Then
                lbAvailable.Items.Add(New DataGridColumnStyleReference(cs))
            Else
                lbDisplayed.Items.Add(New DataGridColumnStyleReference(cs))
            End If
        Next
    End Sub

    Private Sub ShiftColumns(ByVal direction As ColumnMoveDirection)
        With lbDisplayed
            Dim i As Integer = .SelectedIndex

            ' Switch the elements in the list box.
            Dim tmp As DataGridColumnStyleReference = CType(.Items(i), DataGridColumnStyleReference)
            If direction = ColumnMoveDirection.Right Then
                .Items(i) = .Items(i + 1)
                .Items(i + 1) = tmp
            Else
                .Items(i) = .Items(i - 1)
                .Items(i - 1) = tmp
            End If

            'reselect the column after refreshing
            If direction = ColumnMoveDirection.Right Then
                .SelectedIndex = i + 1
            Else
                .SelectedIndex = i - 1
            End If
        End With
    End Sub

    Private Sub DisableColumnEditing()
        btnMoveDown.Enabled = False
        btnMoveUp.Enabled = False
        txtColumnWidth.Enabled = False
        txtColumnWidth.Text = "0" ' This prevents a spurious validation error.
    End Sub

#End Region

#Region " Event Handlers "
    Private Sub CustomizeColumnsForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ResetListBoxes()
    End Sub

    Private Sub lbAvailable_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbAvailable.SelectedIndexChanged
        DisableColumnEditing()
    End Sub

    Private Sub lbDisplayed_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbDisplayed.SelectedIndexChanged
        With lbDisplayed
            If .SelectedIndex >= 0 Then
                'display the proper move buttons
                btnMoveUp.Enabled = (.SelectedIndex > 0)
                btnMoveDown.Enabled = (.SelectedIndex < .Items.Count - 1)

                'display the corresponding width
                txtColumnWidth.Text = CType(.SelectedItem, DataGridColumnStyleReference).Width.ToString()
                txtColumnWidth.Enabled = True
            Else
                DisableColumnEditing()
            End If
        End With
    End Sub

    Private Sub txtColumnWidth_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtColumnWidth.Validating
        Try
            If lbDisplayed.SelectedIndex >= 0 Then
                CType(lbDisplayed.SelectedItem, DataGridColumnStyleReference).Width = Integer.Parse(txtColumnWidth.Text)
            End If
        Catch
            MessageBox.Show("That is not a valid column width.")
            e.Cancel = True
        End Try
    End Sub

    Private Sub btnOK_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOK.Click
        m_dgTableStyle.GridColumnStyles.Clear()

        'set the reference width to the tentative width and add the columns
        Dim csr As DataGridColumnStyleReference
        For Each csr In lbDisplayed.Items
            'save the new width
            csr.RefDataGridColumnStyle.Width = csr.Width

            'add to the datagrid
            m_dgTableStyle.GridColumnStyles.Add(csr.RefDataGridColumnStyle)
        Next

        For Each csr In lbAvailable.Items
            Debug.Assert(csr.Width = 0)
            csr.RefDataGridColumnStyle.Width = 0
            m_dgTableStyle.GridColumnStyles.Add(csr.RefDataGridColumnStyle)
        Next
    End Sub

    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        txtColumnWidth.CausesValidation = False
        Me.Close()
    End Sub

    Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        DisableColumnEditing()

        'set the tentative width to original value, or create a width value
        'so it's added on the refresh
        If lbAvailable.SelectedIndex > -1 Then
            Dim csr As DataGridColumnStyleReference = CType(lbAvailable.SelectedItem, DataGridColumnStyleReference)
            If csr.RefDataGridColumnStyle.Width > 0 Then
                csr.Width = csr.RefDataGridColumnStyle.Width
            Else
                ' The width was never set so use a suitable default.
                Dim newWidth As Integer = csr.RefDataGridColumnStyle.HeaderText.Length * 7
                If newWidth > 65 Then
                    csr.Width = newWidth
                Else
                    csr.Width = 65
                End If
            End If

            lbDisplayed.Items.Add(csr)
            lbAvailable.Items.RemoveAt(lbAvailable.SelectedIndex)
        End If
    End Sub

    Private Sub btnRemove_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRemove.Click
        DisableColumnEditing()

        'remove the column from displayed and add it to available
        If lbDisplayed.SelectedIndex > -1 Then
            Dim csr As DataGridColumnStyleReference = CType(lbDisplayed.SelectedItem, DataGridColumnStyleReference)
            csr.Width = 0
            lbAvailable.Items.Add(csr)
            lbDisplayed.Items.RemoveAt(lbDisplayed.SelectedIndex)
        End If
    End Sub

    Private Sub btnReset_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReset.Click
        DisableColumnEditing()
        ResetListBoxes()
    End Sub

    Private Sub btnMoveUp_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMoveUp.Click
        If lbDisplayed.SelectedIndex > -1 Then
            ShiftColumns(ColumnMoveDirection.Left)
        End If
    End Sub

    Private Sub btnMoveDown_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMoveDown.Click
        If lbDisplayed.SelectedIndex > -1 Then
            ShiftColumns(ColumnMoveDirection.Right)
        End If
    End Sub
#End Region

#Region " Validation work-around "

    ' See http://www.dotnet247.com/247reference/msgs/10/50719.aspx or
    ' http://www.aspfriends.com/search/msg.asp?msgid=601196 for more
    ' information on this problem.  According to Scott Berry at Microsoft,
    ' this is how to implement the work-around.

    Private Const WM_CLOSE As Integer = &H10
    Private Const WM_QUERYENDSESSION As Integer = &H11
    Private Const WM_ENDSESSION As Integer = &H16

    Protected Overrides Sub WndProc(ByRef m As Message)
        If m.Msg = WM_CLOSE OrElse m.Msg = WM_QUERYENDSESSION OrElse m.Msg = WM_ENDSESSION Then
            txtColumnWidth.CausesValidation = False
        End If

        MyBase.WndProc(m)
    End Sub

    ' This is an extension to the workaround above which doesn't address how to
    ' handle the Escape key.  In this implementation, the form does not have
    ' its CancelButton property set since it won't provide the Escape key here
    ' if it is set.
    Private Sub txtColumnWidth_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtColumnWidth.KeyDown
        If e.KeyCode = Keys.Escape Then
            txtColumnWidth.CausesValidation = False
            btnCancel.PerformClick()
        End If
    End Sub

#End Region

End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -