1. 程式人生 > >機房收費系統優化篇

機房收費系統優化篇

一、給主窗體新增快捷鍵:

1、將cmdok的default的屬性設為true
2、在主窗體中新增如下程式碼:

Private Sub form_KeyPress(KeyAscii As Integer)
         If KeyAscii = 13 Then
        Me.Hide
        frmMain.Show
    End If
End Sub

二、字元的限制

文字框的限制:

Private Sub txtUserName_keypress(KeyAscii As Integer)
 Dim ctemp As String
    ctemp = "#$%^&*"   '禁止輸入的字元
    If InStr(1, ctemp, Chr(KeyAscii)) <> 0 Then
        KeyAscii = 0
    End If
Select Case KeyAscii
    Case 48 To 57 '只能輸入數字
    Case 65 To 90 '只能輸入大小寫字母
    Case 97 To 122 ' 只能輸入退格
    Case 8
    Case Else
    KeyAscii = 0
End Select

End Sub

三、如何讓combo框不能輸入字元

在combo框的keypress事件中輸入keyascii=0

四、如何調整mshflexgrid控制元件的單元格的大小,使得讓他們可以根據字串的大小來進行調整

第一步:在模組中新增以下程式碼:

    '宣告函式自動調整mshflexgrid控制元件的單元格的大小
    Public Sub AdjustColWidth(frmCur As Form, gridCur As Object, Optional bNullRow As Boolean = True, Optional dblIncWidth As Double = 0)
    '--------------------------------------------------------------------
    '功能:
    '       自動調整Grid各列列寬為最合適的寬度
    '引數:
    '       [frmCur].........................................當前工作窗體
    '       [gridCur]........................................當前要調整的Grid
    '--------------------------------------------------------------------
    Dim i, j As Integer
    Dim dblWidth As Double
        With gridCur
            For i = 0 To .Cols - 1
                dblWidth = 0
                If .ColWidth(i) <> 0 Then
                    For j = 0 To .Rows - 1
                        If frmCur.TextWidth(.TextMatrix(j, i)) > dblWidth Then
                            dblWidth = frmCur.TextWidth(.TextMatrix(j, i))
                        End If
                    Next
                    .ColWidth(i) = dblWidth + dblIncWidth + 800
                End If
            Next
        End With

End Sub

第二步:在各個子窗體中呼叫:

Call AdjustColWidth(窗體名字,控制元件名字)

小技巧篇:

如何一鍵刪除所有控制元件中所有的內容
程式碼如下:

Private Sub cmdclear_Click()
    Dim ctrl As Control
 
    For Each ctrl In Me.Controls
            Rem:如果控制元件的型別為combo
            If TypeOf ctrl Is ComboBox Then
                    ctrl.Text = ""
            End If
 
            Rem:如果控制元件的型別是text
            If TypeOf ctrl Is TextBox Then
                    ctrl.Text = ""
            End If
 
            Rem:如果控制元件的型別是msflexgrid
            If TypeOf ctrl Is msflexgrid Then
                    ctrl.Clear
            End If
    Next
    
End Sub