Results 1 to 11 of 11

Thread: VB - InputBoxEx (Extended InputBox)

  1. #1

    Thread Starter
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    VB - InputBoxEx (Extended InputBox)

    The following code creates a wrapper for the standard VB InputBox which subclasses the dialog to allow a number of customizations to be made.

    Extended (optional) Functionality includes:
    [list=1][*]Changing Background/Foreground Colors[*]Centering the InputBox Dialog[*]Changing Font Name/Size[*]Implement a Customizable Password Character[*]Generating a Cancel Error like the CommonDialog[/list=1]

    Simply drop the code into a standard module and away you go.
    VB Code:
    1. '*******************************************************
    2. '* InputBoxEx() - Written by Aaron Young, Jan/Feb 2000
    3. '*
    4. '* Usage:
    5. '*
    6. '* Result = InputboxEx( _
    7. '* Message,[Title],[Default],[Default],[XPos],[YPos], _
    8. '* [HelpFile],[Context],[ForeColor],[BackColor], _
    9. '* [FontName],[FontSize],[PasswordChar],[CancelError])
    10. '*
    11. '* This code is Freeware, but if you use it in whole
    12. '* or part, I would appreciate some credit for my work.
    13. '*
    14. '*******************************************************
    15.  
    16. Option Explicit
    17.  
    18. Private Type LOGBRUSH
    19.         lbStyle As Long
    20.         lbColor As Long
    21.         lbHatch As Long
    22. End Type
    23.  
    24. Private Type CWPSTRUCT
    25.         lParam As Long
    26.         wParam As Long
    27.         message As Long
    28.         hwnd As Long
    29. End Type
    30.  
    31. Private Type RECT
    32.         Left As Long
    33.         Top As Long
    34.         Right As Long
    35.         Bottom As Long
    36. End Type
    37.  
    38. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    39. Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
    40. Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal nCode As Long, ByVal wParam As Long, lParam As Any) As Long
    41. Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
    42. Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    43. Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    44. Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
    45. Private Declare Function SetBkColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long
    46. Private Declare Function SetTextColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long
    47. Private Declare Function CreateBrushIndirect Lib "gdi32" (lpLogBrush As LOGBRUSH) As Long
    48. Private Declare Function CreateFont Lib "gdi32" Alias "CreateFontA" (ByVal H As Long, ByVal W As Long, ByVal E As Long, ByVal O As Long, ByVal W As Long, ByVal I As Long, ByVal u As Long, ByVal S As Long, ByVal C As Long, ByVal OP As Long, ByVal CP As Long, ByVal Q As Long, ByVal PAF As Long, ByVal F As String) As Long
    49. Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
    50. Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
    51. Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
    52. Private Declare Function GetSysColor Lib "user32" (ByVal nIndex As Long) As Long
    53. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    54.  
    55. ' System Color Constants
    56. Private Const COLOR_BTNFACE = 15
    57. Private Const COLOR_BTNTEXT = 18
    58.  
    59. ' SetWindowPos Constants
    60. Private Const SWP_FRAMECHANGED = &H20
    61. Private Const SWP_NOSIZE = &H1
    62. Private Const SWP_NOZORDER = &H4
    63.  
    64. Private Const WH_CALLWNDPROC = 4
    65.  
    66. Private Const GWL_WNDPROC = (-4)
    67.  
    68. ' Windows Messages
    69. Private Const WM_GETFONT = &H31
    70. Private Const WM_CREATE = &H1
    71. Private Const WM_CTLCOLORBTN = &H135
    72. Private Const WM_CTLCOLORDLG = &H136
    73. Private Const WM_CTLCOLORSTATIC = &H138
    74. Private Const WM_CTLCOLOREDIT = &H133
    75. Private Const WM_DESTROY = &H2
    76. Private Const WM_SHOWWINDOW = &H18
    77. Private Const WM_COMMAND = &H111
    78.  
    79. Private Const BN_CLICKED = 0
    80. Private Const IDOK = 1
    81.  
    82. Private Const EM_SETPASSWORDCHAR = &HCC
    83.  
    84. ' InputboxEx Variables
    85. Private INPUTBOX_HOOK As Long
    86. Private INPUTBOX_HWND As Long
    87. Private INPUTBOX_PASSCHAR As String
    88. Private INPUTBOX_BACKCOLOR As Long
    89. Private INPUTBOX_FORECOLOR As Long
    90. Private INPUTBOX_FONT As String
    91. Private INPUTBOX_FONTSIZE As Integer
    92. Private INPUTBOX_SHOWING As Boolean
    93. Private INPUTBOX_CENTERV As Boolean
    94. Private INPUTBOX_CENTERH As Boolean
    95. Private INPUTBOX_OK As Boolean
    96.  
    97. Private Function InputBoxProc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    98.     Dim tLB As LOGBRUSH
    99.     Dim lFont As Long
    100.     Dim tRECT As RECT
    101.     Dim lNotify As Long
    102.     Dim lID As Long
    103.    
    104.     Select Case Msg
    105.     Case WM_COMMAND
    106.         'Check to see if the OK Button was Pressed..
    107.         lNotify = Val("&H" & Left$(Right$("00000000" & Hex$(wParam), 8), 4))
    108.         lID = Val("&H" & Right$(Right$("00000000" & Hex$(wParam), 8), 4))
    109.         If lNotify = BN_CLICKED Then
    110.             INPUTBOX_OK = (lID = IDOK)
    111.         End If
    112.        
    113.     Case WM_SHOWWINDOW
    114.         'Reposition Inputbox if Neccessary
    115.         Call GetWindowRect(hwnd, tRECT)
    116.         If INPUTBOX_CENTERH Then tRECT.Left = ((Screen.Width / Screen.TwipsPerPixelX) - (tRECT.Right - tRECT.Left)) / 2
    117.         If INPUTBOX_CENTERV Then tRECT.Top = ((Screen.Height / Screen.TwipsPerPixelY) - (tRECT.Bottom - tRECT.Top)) / 2
    118.         Call SetWindowPos(hwnd, 0, tRECT.Left, tRECT.Top, 0, 0, SWP_NOSIZE Or SWP_NOZORDER Or SWP_FRAMECHANGED)
    119.        
    120.     Case WM_CTLCOLORDLG, WM_CTLCOLORSTATIC, WM_CTLCOLORBTN, WM_CTLCOLOREDIT
    121.         'Set the Colors
    122.         If Msg = WM_CTLCOLOREDIT Then
    123.             If Len(INPUTBOX_PASSCHAR) Then
    124.                 Call SendMessage(lParam, EM_SETPASSWORDCHAR, Asc(INPUTBOX_PASSCHAR), ByVal 0&)
    125.             End If
    126.         Else
    127.             Call SetTextColor(wParam, INPUTBOX_FORECOLOR)
    128.             Call SetBkColor(wParam, INPUTBOX_BACKCOLOR)
    129.             If Msg = WM_CTLCOLORSTATIC Then
    130.                 'Set the Font
    131.                 lFont = CreateFont(-((INPUTBOX_FONTSIZE / 72) * 96), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, INPUTBOX_FONT)
    132.                 Call SelectObject(wParam, lFont)
    133.             End If
    134.             'Create a Solid Brush using that Color
    135.             tLB.lbColor = INPUTBOX_BACKCOLOR
    136.             'Return the Handle to the Brush to Paint the Inputbox
    137.             InputBoxProc = CreateBrushIndirect(tLB)
    138.             Exit Function
    139.         End If
    140.        
    141.     Case WM_DESTROY
    142.         'Remove the Inputbox Subclassing
    143.         Call SetWindowLong(hwnd, GWL_WNDPROC, INPUTBOX_HWND)
    144.        
    145.     End Select
    146.     InputBoxProc = CallWindowProc(INPUTBOX_HWND, hwnd, Msg, wParam, ByVal lParam)
    147. End Function
    148.  
    149. Private Function HookWindow(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    150.     Dim tCWP As CWPSTRUCT
    151.     Dim sClass As String
    152.     'This is where you need to Hook the Inputbox
    153.     CopyMemory tCWP, ByVal lParam, Len(tCWP)
    154.     If tCWP.message = WM_CREATE Then
    155.         sClass = Space(255)
    156.         sClass = Left(sClass, GetClassName(tCWP.hwnd, ByVal sClass, 255))
    157.         If sClass = "#32770" Then
    158.             If INPUTBOX_SHOWING Then
    159.                 'Subclass the Inputbox as it's created
    160.                 INPUTBOX_HWND = SetWindowLong(tCWP.hwnd, GWL_WNDPROC, AddressOf InputBoxProc)
    161.             End If
    162.         End If
    163.     End If
    164.     HookWindow = CallNextHookEx(INPUTBOX_HOOK, nCode, wParam, ByVal lParam)
    165. End Function
    166.  
    167. Public Function InputBoxEx(ByVal Prompt As String, Optional ByVal Title As String, Optional ByVal Default As String, Optional ByVal XPos As Single = -1, Optional ByVal YPos As Single = -1, Optional ByVal HelpFile As String, Optional ByVal Context As Long, Optional ByVal ForeColor As ColorConstants, Optional ByVal BackColor As ColorConstants, Optional ByVal FontName As String, Optional ByVal FontSize As Long, Optional ByVal PasswordChar As String, Optional ByVal CancelError As Boolean = False) As String
    168.     'Set the Defaults
    169.     If Len(Title) = 0 Then Title = App.Title
    170.     INPUTBOX_FONT = "MS Sans Serif"
    171.     INPUTBOX_FONTSIZE = 8
    172.     INPUTBOX_FORECOLOR = GetSysColor(COLOR_BTNTEXT)
    173.     INPUTBOX_BACKCOLOR = GetSysColor(COLOR_BTNFACE)
    174.     INPUTBOX_CENTERH = (XPos = -1)
    175.     INPUTBOX_CENTERV = (YPos = -1)
    176.     INPUTBOX_PASSCHAR = PasswordChar
    177.     'Set the Font and Colors
    178.     If Len(FontName) Then INPUTBOX_FONT = FontName
    179.     If FontSize > 0 Then INPUTBOX_FONTSIZE = FontSize
    180.     If ForeColor > 0 Then INPUTBOX_FORECOLOR = ForeColor
    181.     If BackColor > 0 Then INPUTBOX_BACKCOLOR = BackColor
    182.     'Show the Modified Inputbox
    183.     INPUTBOX_SHOWING = True
    184.     'Monitor All Messages to this Thread.
    185.     INPUTBOX_HOOK = SetWindowsHookEx(WH_CALLWNDPROC, AddressOf HookWindow, App.hInstance, App.ThreadID)
    186.     InputBoxEx = InputBox(Prompt, Title, Default, XPos, YPos, HelpFile, Context)
    187.     INPUTBOX_SHOWING = False
    188.     'Remove the Hook
    189.     Call UnhookWindowsHookEx(INPUTBOX_HOOK)
    190.     If Not INPUTBOX_OK And CancelError Then Err.Raise vbObjectError + 1, , "User Pressed " & Chr(34) & "Cancel" & Chr(34)
    191. End Function
    Example Usage:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.   Dim sReturnVal As String
    5.  
    6.   sReturnVal = _
    7.     InputBoxEx("Enter Password", "InputBoxEx", "password", , , , , vbRed, vbYellow, "Courier New", 10, "*")
    8. End Sub

  2. #2
    New Member
    Join Date
    Feb 2004
    Posts
    5

    Question Applying this....

    I'd like to incorporate your code into an inputbox script that I have. I am particularly intersted in making the input field in the input box configurable. However, I am not sure on what to delete or add to incorporate with the coding that I have. I have made an E-Signature inputbox, but I need for the user inputted text to be astericked or of a different font. Can you look at what I am using and advise on where to add or del. your code?

    Thanks in advance.

    VB Code:
    1. Private Sub Ctl10_1_1_Enter()
    2. Dim strInputPin As String
    3. If Ctl10_1_1 <> "" Then
    4.     strInputPin = InputBox("Unlock with original signer's pin:")
    5.     Dim rsCheck As ADODB.Recordset
    6.     Set rsCheck = New ADODB.Recordset
    7.     rsCheck.ActiveConnection = CurrentProject.Connection
    8.     rsCheck.CursorType = adOpenKeyset
    9.     rsCheck.LockType = adLockOptimistic
    10.     rsCheck.Open "Select * From tblTrainerSignOffs"
    11.     rsCheck.Find "[strName] = '" & Ctl10_1_1 & "'"
    12.     If rsCheck!strPin = strInputPin Then
    13.         MsgBox ("Sign-off unlocked.")
    14.     Else
    15.         MsgBox ("Pin did not match. Stop cheating.")
    16.         GoTo 998
    17.     End If
    18.     rsCheck.Close
    19.     Set rsCheck = Nothing
    20. Else
    21. End If
    22.  
    23. strInputPin = InputBox("Enter Sign-Off Pin:")
    24.  
    25. Dim rsUsers As ADODB.Recordset
    26. Set rsUsers = New ADODB.Recordset
    27.     rsUsers.ActiveConnection = CurrentProject.Connection
    28.     rsUsers.CursorType = adOpenKeyset
    29.     rsUsers.LockType = adLockOptimistic
    30.     rsUsers.Open "Select * From tblTrainerSignOffs"
    31.     rsUsers.MoveFirst
    32.     Do Until rsUsers.EOF
    33.         If rsUsers!strPin = strInputPin Then
    34.             Ctl10_1_1 = rsUsers!strName
    35.             GoTo 999
    36.         Else
    37.         rsUsers.MoveNext
    38.         End If
    39.     Loop
    40. 999 rsUsers.Close
    41. Set rsUsers = Nothing
    42.  
    43. 998 [Command34].SetFocus
    44.  
    45. DoCmd.RunCommand (acCmdRefresh)
    46. End Sub

  3. #3
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427
    I added [vbcode][/vbcode] tags to your code so that it is easier to read. You can and should add them yourself.

    BTW, your use of GoTo to line numbers is error prone and it can also lead to spaghetti code. In your case you could use Exit Do rather than GoTo 999 and DoCmd.RunCommand (acCmdRefresh) followed by Exit Sub instead of GoTo 998.

  4. #4
    Lively Member
    Join Date
    Aug 2003
    Location
    everett washington
    Posts
    64

    shuts down VB

    I tried this code just to see output and it shutdown VB project?
    Am I missing something here?
    Tarakwar

  5. #5
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Re: shuts down VB

    Originally posted by tarakwar
    I tried this code just to see output and it shutdown VB project?
    Am I missing something here?
    Aaron 's code uses subclassing and whenever that is done you must exit the code gracefully. In other words you can't use the IDE's End button.

  6. #6
    Lively Member
    Join Date
    Mar 2005
    Location
    Scotland
    Posts
    123

    Unhappy Re: VB - InputBoxEx (Extended InputBox)

    thanks for everyone replying

    i am new to visual basic and i cant work out how to edit the vb form code posted by Aaron Young

    I am trying to dim a pass as a string variable.

    when i type in:

    VB Code:
    1. dim pass as string
    2. pass = sreturnval      'which is the name of the pass stored in mem
    3. if pass = "go" then
    4. msgbox ("HII")
    5. else
    6. msgbox ("LOO")
    7. end if
    8. end sub

    it justs comes up saying tht sreturnval is an arguement which cannot be complete or some guff like tht

    Hopefully one of you will know the cure to my problems

    Regards

    Aaron

  7. #7
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Re: VB - InputBoxEx (Extended InputBox)

    The code you show obviously isn't your actual code. Why not just cut and paste your real code.

    Notice that in Aaron's code he has

    VB Code:
    1. Private Sub Command1_Click()
    2.   Dim sReturnVal As String
    3.  
    4.   [HL="#FFFF80"]sReturnVal = _
    5.     InputBoxEx("Enter Password", "InputBoxEx", "password", , , , , vbRed, vbYellow, "Courier New", 10, "*")[/HL]End Sub

    That code asks the user for a password. I don't mean to be critical but your code seems to be just trying to grab the password out of thin air since it wouldn't be in memory unless you put it there.

  8. #8
    Lively Member
    Join Date
    Mar 2005
    Location
    Scotland
    Posts
    123

    Re: VB - InputBoxEx (Extended InputBox)

    I am happy that an administrator has replied to my post and martin i aint trying to grab it out of think air

    i am going to have to store a password in memory and then i will want as soon as the form runs for the modified inputbox with pass char on it to open up.

    Then if the user enters the correct password he will forwarded to a form called

    frmghy1

    dont ask why if you knew what program i was building it would work out

    Hopefully someone out there in the world wide web (VB FORUMS) will be able to help me


    Regards

    Aaron Smith

  9. #9
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Re: VB - InputBoxEx (Extended InputBox)

    I'm not sure I understand, but you can't store anything in memory directly using VB. If you want to store it temorarily in your program you assign it to a variable which has an address in memory which is temporary until you close the program and then it's gone. If you want to store it permanently you need to use the Registry or an ini file or a text file or a database.

  10. #10
    Lively Member
    Join Date
    Mar 2005
    Location
    Scotland
    Posts
    123

    Re: VB - InputBoxEx (Extended InputBox)

    Thanks for replying Martin

    As i have said in my previous posts i have no clue about visual basic and dont know wot an ini file is or how to save to the registry

    but i will try and expand on my prggy

    When the program is run the inputbox will appear but it wont be an ordinary input box it will be an input box with a password character engroseed. So anyway there will be a password stored 'somewhere' and if the user enters a correct password then a form will be displayed and if the user enters an incorrect password then the proggy will terminate,
    ]

    Regards

    Aaron

  11. #11

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width