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

📄 customizecolumnsform.vb

📁 使用Access数据库演示的任务分配管理程序 一个使用ADO.NET基于Microsoft Access数据库演示的任务分配管理的程序
💻 VB
字号:
'---------------------------------------------------------------------
'  This file is part of the Microsoft .NET Framework SDK Code Samples.
' 
'  Copyright (C) Microsoft Corporation.  All rights reserved.
' 
' This source code is intended only as a supplement to Microsoft
' Development Tools and/or on-line documentation.  See these other
' materials for detailed information regarding Microsoft code samples.
' 
' THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
' KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
' IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
' PARTICULAR PURPOSE.
'---------------------------------------------------------------------

Public Class CustomizeColumnsForm
    Inherits System.Windows.Forms.Form

    Public Sub New(ByVal dgvColumns As DataGridViewColumnCollection)
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call
        m_dgvColumns = dgvColumns

    End Sub

    Private m_dgvColumns As DataGridViewColumnCollection


#Region " Private Classes "
    Private Enum ColumnMoveDirection
        Left = 0
        Right = 1
    End Enum

    Private Class DataGridViewColumnReference

        Private m_RefDataGridViewColumn As DataGridViewColumn
        Public Width As Integer
        Private m_ResourceManager As New Resources.ResourceManager("TeamVision.Localize", System.Reflection.Assembly.GetExecutingAssembly())
        'this class was created to track desired changes to a referenced columnstyle
        'without setting the values to the columnstyle

        Public Sub New(ByVal refDataGridViewColumn As DataGridViewColumn)
            m_RefDataGridViewColumn = refDataGridViewColumn
            Width = refDataGridViewColumn.Width
        End Sub

        Public ReadOnly Property RefDataGridViewColumn() As DataGridViewColumn
            Get
                Return m_RefDataGridViewColumn
            End Get
        End Property

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

            Return m_RefDataGridViewColumn.HeaderText
        End Function
    End Class
#End Region

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


        Dim col As DataGridViewColumn
        col = m_dgvColumns.GetFirstColumn(DataGridViewElementStates.Visible, DataGridViewElementStates.None)
        While Not col Is Nothing
            lbDisplayed.Items.Add(New DataGridViewColumnReference(col))
            col = m_dgvColumns.GetNextColumn(col, DataGridViewElementStates.Visible, DataGridViewElementStates.None)
        End While

        col = m_dgvColumns.GetFirstColumn(DataGridViewElementStates.None, DataGridViewElementStates.Visible)
        While Not col Is Nothing
            lbAvailable.Items.Add(New DataGridViewColumnReference(col))
            col = m_dgvColumns.GetNextColumn(col, DataGridViewElementStates.None, DataGridViewElementStates.Visible)
        End While


        'Dim cs As DataGridColumnStyle
        'For Each cs In m_dgTableStyle.GridColumnStyles
        '    If cs.Width = 0 Then
        '        lbAvailable.Items.Add(New DataGridViewColumnReference(cs))
        '    Else
        '        lbDisplayed.Items.Add(New DataGridViewColumnReference(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 DataGridViewColumnReference = CType(.Items(i), DataGridViewColumnReference)
            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, DataGridViewColumnReference).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, DataGridViewColumnReference).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()
        Dim allColIndex As Integer = 0
        Dim col As DataGridViewColumnReference

        'set the reference width to the tentative width and add the columns
        ' Assign the column to its new position
        For Each col In lbDisplayed.Items
            'save the new width
            col.RefDataGridViewColumn.Width = col.Width
            col.RefDataGridViewColumn.Visible = True
            col.RefDataGridViewColumn.DisplayIndex = allColIndex
            allColIndex = allColIndex + 1

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

        For Each col In lbAvailable.Items
            'Debug.Assert(csr.Width = 0)
            'col.RefDataGridColumnStyle.Width = col.Width
            'm_dgTableStyle.GridColumnStyles.Add(csr.RefDataGridColumnStyle)

            col.RefDataGridViewColumn.Width = col.Width
            col.RefDataGridViewColumn.Visible = False
            col.RefDataGridViewColumn.DisplayIndex = allColIndex
            allColIndex = allColIndex + 1
        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 col As DataGridViewColumnReference = CType(lbAvailable.SelectedItem, DataGridViewColumnReference)
        '    If col.RefDataGridViewColumn.Width > 0 Then
        '        col.Width = col.RefDataGridViewColumn.Width
        '    Else
        '        ' The width was never set so use a suitable default.
        '        Dim newWidth As Integer = col.RefDataGridViewColumn.HeaderText.Length * 7
        '        If newWidth > 65 Then
        '            csr.Width = newWidth
        '        Else
        '            csr.Width = 65
        '        End If
        '    End If

        If lbAvailable.SelectedIndex > -1 Then
            Dim col As DataGridViewColumnReference = CType(lbAvailable.SelectedItem, DataGridViewColumnReference)
            lbDisplayed.Items.Add(col)
            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 col As DataGridViewColumnReference = CType(lbDisplayed.SelectedItem, DataGridViewColumnReference)
            lbAvailable.Items.Add(col)
            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 + -