Relativ schlicht, aber bequem und schnell einzusetzen ist die InputBox. Dafür haben Sie aber so gut wie keine Chance, ihr Aussehen verändern und in ihre Funktion eingreifen zu können. Unser Nachbau "InputForm" (eigentlich frmInput) auf der Basis eines Forms ist nahezu identisch zum Original. Verwenden Sie zum Aufruf die Funktion InputBox aus dem dazugehörenden Standard-Modul modInputForm.bas (die Funktion ist dort auskommentiert), ist der Aufruf sogar völlig code-kompatibel zum Original:
Public Function InputBox(Prompt As Variant, _
Optional Title As Variant, _
Optional Default As Variant, _
Optional ByVal XPos As Variant, _
Optional ByVal YPos As Variant, _
Optional HelpFile As Variant, _
Optional ByVal Context As Variant) _
As String
With New frmInput
InputBox = .ShowDlg(Prompt, Title, Default, XPos, YPos, _
HelpFile, Context)
End With
End Function
Selbst die Prüfung, ob der Anwender die Eingabe abgebrochen hat, funktioniert einwandfrei
(siehe: "Abbruch der InputBox"). Der einzige Unterschied ist, dass die Beschriftungen der Schaltflächen ("Ok", "Abbrechen" und gegebenenfalls "Hilfe") nicht mehr in der Sprache Ihrer VB-Version (bzw. der bei der Installation Ihrer Anwendung gewählten Sprache) erscheinen, sondern zunächst deutschsprachig vorgegeben sind. Aber damit hätten wir auch schon die erste Erweiterung der InputForm angesprochen: Sie können die Beschriftungen der Schaltflächen natürlich zur Entwicklungszeit beliebig ändern - und auch zur Laufzeit.
Das Grundprinzip der Erweiterungen ist einfach. Zum einen gibt es zusätzliche Parameter zum Aufruf, zum anderen können Sie auch alle gewohnten als auch hinzu gekommenen Werte als separate Eigenschaften des Forms setzen. Unser Modell sieht vor, dass dabei der Aufruf-Parameter Vorrang vor einer Voreinstellung über eine Eigenschaft hat.
Neben den schon angesprochenen Eigenschaften/Parametern CaptionOK, CaptionCancel und CaptionHelp für die Beschriftungen der Schaltflächen gibt es noch die Eigenschaften MaxLength und PasswordChar. Diese beiden Eigenschaften kennen Sie von einer TextBox her - sie haben die gleiche Bedeutung und Wirkung.
Die Angabe der Positionierung (XPos und YPos) über Parameter hat die gleiche Wirkung wie beim Original. Wird eine der Angaben weggelassen, und sind auch keine Werte über die Eigenschaften vorgegeben worden, wird das InputForm in der jeweiligen Achse auf dem Bildschirm zentriert. Wurden Werte über die Eigenschaften vorgegeben, werden nach dem Schließen des InputForms dessen tatsächlichen Positionswerte in die Eigenschaftenvariablen zurückgeschrieben. Sie können sie auslesen und gegebenenfalls beim nächsten Aufruf wieder verwenden bzw. angeben und somit dem Anwender den Komfort der Beibehaltung der Position bieten, selbst wenn dieser das InputForm verschoben haben sollte.
Die letzte Erweiterung besteht darin, dass Sie den Abbruch zusätzlich aus der Eigenschaft Cancelled auslesen können.
Der Code des InputForms (frmInput):
Private pCancelled As Boolean
Private pCaptionCancel As String
Private pCaptionHelp As String
Private pCaptionOK As String
Private pHelpContext As Long
Private pHelpFile As String
Private pMaxLength As Long
Private pPasswordChar As String
Private pPrompt As String
Private pTitle As String
Private pValue As String
Private pXPos As Variant
Private pYPos As Variant
Public Property Get Cancelled() As Boolean
Cancelled = pCancelled
End Property
Public Property Get CaptionCancel() As String
CaptionCancel = pCaptionCancel
End Property
Public Property Let CaptionCancel(New_CaptionCancel As String)
pCaptionCancel = New_CaptionCancel
End Property
Public Property Get CaptionHelp() As String
CaptionHelp = pCaptionHelp
End Property
Public Property Let CaptionHelp(New_CaptionHelp As String)
pCaptionHelp = New_CaptionHelp
End Property
Public Property Get CaptionOK() As String
CaptionOK = pCaptionOK
End Property
Public Property Let CaptionOK(New_CaptionOK As String)
pCaptionOK = New_CaptionOK
End Property
Public Property Get HelpContext() As Long
HelpContext = pHelpContext
End Property
Public Property Let HelpContext(New_HelpContext As Long)
pHelpContext = New_HelpContext
End Property
Public Property Get HelpFile() As String
HelpFile = pHelpFile
End Property
Public Property Let HelpFile(New_HelpFile As String)
pHelpFile = New_HelpFile
End Property
Public Property Get PasswordChar() As String
PasswordChar = pPasswordChar
End Property
Public Property Let PasswordChar(New_PasswordChar As String)
On Error Resume Next
pPasswordChar = Left$(New_PasswordChar, 1)
If Err.Number Then
On Error GoTo 0
Err.Raise 380
End If
End Property
Public Property Get Prompt() As String
Prompt = pPrompt
End Property
Public Property Let Prompt(New_Prompt As String)
pPrompt = New_Prompt
End Property
Public Property Get Title() As String
Title = pTitle
End Property
Public Property Let Title(New_Title As String)
pTitle = New_Title
End Property
Public Property Get Value() As String
Value = pValue
End Property
Public Property Let Value(New_Value As String)
pValue = New_Value
End Property
Public Property Get MaxLength() As Long
MaxLength = pMaxLength
End Property
Public Property Let MaxLength(New_MaxLength As Long)
Select Case New_MaxLength
Case Is < 0
Err.Raise 380
Case Else
pMaxLength = New_MaxLength
End Select
End Property
Public Property Get XPos() As Single
XPos = pXPos
End Property
Public Property Let XPos(New_XPos As Single)
pXPos = New_XPos
End Property
Public Property Get YPos() As Single
YPos = pYPos
End Property
Public Property Let YPos(New_YPos As Single)
pYPos = New_YPos
End Property
Public Function ShowDlg(Optional Prompt As Variant, _
Optional Title As Variant, _
Optional Default As Variant, _
Optional ByVal XPos As Variant, _
Optional ByVal YPos As Variant, _
Optional HelpFile As Variant, _
Optional ByVal Context As Variant, _
Optional ByVal MaxLength As Variant, _
Optional PasswordChar As Variant, _
Optional OwnerForm As Variant, _
Optional CaptionOK As Variant, _
Optional CaptionCancel As Variant, _
Optional CaptionHelp As Variant) As String
Dim nStoreXPos As Boolean
Dim nStoreYPos As Boolean
pCancelled = False
With cdg
If IsMissing(HelpFile) Then
.HelpFile = pHelpFile
Else
.HelpFile = HelpFile
End If
If IsMissing(Context) Then
.HelpContext = pHelpContext
Else
.HelpContext = Context
End If
cmdHelp.Visible = CBool(Len(Trim$(.HelpFile)))
End With
If IsMissing(Prompt) Then
lbl.Caption = pPrompt
Else
lbl.Caption = Prompt
End If
With txt
If IsMissing(PasswordChar) Then
.PasswordChar = pPasswordChar
Else
.PasswordChar = PasswordChar
End If
If IsMissing(Default) Then
.Text = pValue
Else
.Text = Default
End If
If IsMissing(MaxLength) Then
If pMaxLength Then
.Text = Left$(.Text, pMaxLength)
.MaxLength = pMaxLength
End If
ElseIf CLng(MaxLength) > 0 Then
.Text = Left$(.Text, MaxLength)
.MaxLength = MaxLength
End If
.SelStart = 0
.SelLength = Len(.Text)
End With
If IsMissing(CaptionCancel) Then
If Len(pCaptionCancel) Then
cmdCancel.Caption = pCaptionCancel
End If
Else
If Len(CaptionCancel) Then
cmdCancel.Caption = CaptionCancel
End If
End If
If IsMissing(CaptionHelp) Then
If Len(pCaptionHelp) Then
cmdHelp.Caption = pCaptionHelp
End If
Else
If Len(CaptionHelp) Then
cmdHelp.Caption = CaptionHelp
End If
End If
If IsMissing(CaptionOK) Then
If Len(pCaptionOK) Then
cmdOK.Caption = pCaptionOK
End If
Else
If Len(CaptionOK) Then
cmdOK.Caption = CaptionOK
End If
End If
With Me
If IsMissing(Title) Then
.Caption = pTitle
Else
.Caption = Title
End If
If IsMissing(XPos) Then
If Not IsEmpty(pXPos) Then
.Left = pXPos
Else
.Left = (Screen.Width - .Width) \ 2
End If
nStoreXPos = True
Else
.Left = XPos
End If
If IsMissing(YPos) Then
If Not IsEmpty(pYPos) Then
.Top = pYPos
Else
.Top = (Screen.Height - .Height) \ 2
End If
nStoreYPos = True
Else
.Top = YPos
End If
.Show vbModal, OwnerForm
If Not pCancelled Then
pValue = txt.Text
ShowDlg = pValue
End If
If nStoreXPos Then
pXPos = .Left
End If
If nStoreYPos Then
pYPos = .Top
End If
End With
Unload Me
End Function
Private Sub cmdCancel_Click()
pCancelled = True
Me.Hide
End Sub
Private Sub cmdHelp_Click()
cdg.ShowHelp
End Sub
Private Sub cmdOK_Click()
Me.Hide
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyF1
If cmdHelp.Visible Then
cdg.ShowHelp
End If
KeyCode = 0
End Select
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, _
UnloadMode As Integer)
Select Case UnloadMode
Case vbFormControlMenu
pCancelled = True
Me.Hide
Cancel = True
End Select
End Sub
Wenn Sie die Instanzierung sparen möchten, können Sie die Standard-Instanz des Forms direkt aufrufen:
Eingabe = frmInput.ShowDlg(...)
Oder Sie rufen analog zur InputBox-Funktion die Funktion InputForm aus dem Standard-Modul modInputForm auf. Darin wird eine eigene, frische Instanz des Forms angelegt und wie vorstehend aufgerufen:
Public Function InputForm(Optional Prompt As Variant, _
Optional Title As Variant, _
Optional Default As Variant, _
Optional ByVal XPos As Variant, _
Optional ByVal YPos As Variant, _
Optional HelpFile As Variant, _
Optional ByVal Context As Variant, _
Optional ByVal MaxLength As Variant, _
Optional PasswordChar As Variant, _
Optional OwnerForm As Variant, _
Optional CaptionOK As Variant, _
Optional CaptionCancel As Variant, _
Optional CaptionHelp As Variant) As String
With New frmInput
InputForm = .ShowDlg(Prompt, Title, Default, XPos, YPos, _
HelpFile, Context, MaxLength, PasswordChar, OwnerForm, _
CaptionOK, CaptionCancel, CaptionHelp)
End With
End Function
Der Aufruf der Hilfe, falls eine Hilfedatei angegeben worden ist und daher die Hilfeschaltfläche sichtbar und die F1-Taste aktiv sind, erfolgt der Einfachheit halber über das CommonDialog-Steuerelement, das auf dem Form platziert ist. Es muss daher im Projekt vorhanden sein. Sie können die Hilfe-Funktionalität natürlich auch entfernen oder durch eine direkte API-Version ersetzen. Letzteres ist aber ein Thema, das den Rahmen dieses Artikels sprengen würde - daher der einfache Einsatz des CommonDialog-Steuerelements.
|