home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 25 / nopv25.iso / 040A / INPUTB21.ZIP / INPUTBOX.BAS < prev    next >
Encoding:
BASIC Source File  |  1996-12-31  |  9.3 KB  |  245 lines

  1. Attribute VB_Name = "InputBoxFunctions"
  2. ' InputBox 2.11 for Visual Basic 4.0
  3. '
  4. ' SHAREWARE, registration is $10
  5. ' See readme.txt for more information on registration
  6. '
  7. ' Functions to replace and enhance VB's built-in InputBox function
  8. ' ⌐1994-1997 Tuomas Salste (vbshop@netgate.net)
  9. '
  10. ' You may use, modify and distribute this source code in your programs as you wish,
  11. ' provided that
  12. ' 1. You have registered
  13. ' 2. You keep this copyright text intact
  14. '
  15. ' **************************************************************************************************************************
  16. ' InputBox(Prompt, Title, Default, xpos, ypos, HelpFile, Context, MaxLength)
  17. ' InputLcase(Prompt, Title, Default, xpos, ypos, HelpFile, Context, MaxLength)
  18. ' InputUcase(Prompt, Title, Default, xpos, ypos, HelpFile, Context, MaxLength)
  19. ' InputPassword(Prompt, Title, Default, xpos, ypos, HelpFile, Context, MaxLength, PasswordChar)
  20. ' **************************************************************************************************************************
  21. '
  22. ' These routines replace VB's built-in InputBox() function
  23. '
  24. ' Load InputBox.Bas and InputBox.Frm into your project, and you are ready to
  25. ' replace the built-in InputBox with an enhanced one, automatically
  26. ' YOU DON'T HAVE TO DO ANY CODING!
  27. '
  28. ' **************************************************************************************************************************
  29. '
  30. ' VB's built-in InputBox function is declared like this:
  31. ' > InputBox(Prompt[, Title][, Default][, xpos][, ypos][, HelpFile, Context]) As String
  32. ' Returns a string or "" if the user pressed Cancel
  33. '
  34. ' The new InputBox function is declared like this:
  35. ' > InputBox(Prompt[, Title][, Default][, xpos][, ypos][, HelpFile][, Context][, MaxLength]) As Variant
  36. '
  37. ' DIFFERENCES FROM THE BUILT-IN INPUTBOX FUNCTION:
  38. ' New optional parameter MaxLength to set the maximum length of accepted input
  39. ' Returns Null if the user pressed Cancel (depends on Private Const IBValueOnCancel below)
  40. '
  41. ' **************************************************************************************************************************
  42. '
  43. ' NEW, ENHANCED FUNCTIONS:
  44. ' > InputUcase(Prompt[, Title][, Default][, xpos][, ypos][, HelpFile][, Context][, MaxLength]) As Variant
  45. ' Like InputBox, but turns all input to UPPER CASE
  46. '
  47. ' > InputLcase(Prompt[, Title][, Default][, xpos][, ypos][, HelpFile][, Context][, MaxLength]) As Variant
  48. ' Like InputBox, but turns all input to lower case
  49. '
  50. ' > InputPassword(Prompt[, Title][, Default][, xpos][, ypos][, HelpFile][, Context][, MaxLength][, PasswordChar]) As Variant
  51. ' Like InputBox, but the input is masked with * or character specified by parameter PasswordChar
  52. '               (default mask char * depends on Private Const IBDefaultPasswordChar below)
  53. '
  54. '
  55. ' DEFAULT VALUES IF PARAMETERS NOT SET:
  56. ' PARAMETER       DEFAULT VALUE
  57. ' Title           App.Title
  58. ' Default         ""
  59. ' xpos            Center of parent window or screen
  60. ' ypos            Center of parent window or screen
  61. ' HelpFile        App.HelpFile
  62. ' Context         0. If set, displays a Help button
  63. ' MaxLength       None
  64. ' PasswordChar    IPDefaultPasswordChar (originally "*")
  65.  
  66. Option Explicit
  67.  
  68. ' *******************************************************************************
  69. ' User defined constants
  70. '
  71. Private Const IBValueOnCancel = Null      ' Return value when user pressed Cancel
  72. Private Const IBDefaultPasswordChar = "*" ' Default password mask character
  73. #Const UseBuiltInInputBox = False         ' Set to True to disable function InputBox
  74. '
  75. ' End of user defined constants
  76. ' *******************************************************************************
  77.  
  78.  
  79. ' Symbolic constants for internal use
  80.  
  81. Public Const IBUcase = &H10000
  82. Public Const IBLcase = &H20000
  83. Public Const IBPassword = &H40000
  84.  
  85. Public Const InputBoxVersion = 2.11
  86. Public Const InputBoxVersionName = "InputBox 2.11"
  87.  
  88. Private Sub CenterForm(Parent As Form, Child As Form)
  89. ' Centers Child in relation to Parent
  90.  
  91. Dim x As Integer, y As Integer
  92.  
  93. x = (Parent.Left + Parent.Width / 2) - Child.Width / 2
  94. y = (Parent.Top + Parent.Height / 2) - Child.Height / 2
  95.  
  96. Child.Move x, y
  97.  
  98. End Sub
  99.  
  100. Private Sub CenterToParent(Child As Form)
  101. ' Centers a form to its parent form
  102.  
  103. If Screen.ActiveForm Is Child Then
  104.     ' No parent form
  105.     CenterToScreen Child
  106. Else
  107.     CenterForm Forms(ParentForm(Child)), Child
  108. End If
  109.  
  110. End Sub
  111.  
  112.  
  113. Private Sub CenterToScreen(F As Form)
  114. ' Centers form F to the screen
  115.  
  116. With F
  117.     .Move Screen.Width / 2 - .Width / 2, Screen.Height / 2 - .Height / 2
  118. End With
  119.  
  120. End Sub
  121.  
  122.  
  123. Private Function DoInputBox(ByVal Prompt As String, Optional ByVal Title As Variant, Optional ByVal Default As Variant, Optional ByVal xpos As Variant, Optional ByVal ypos As Variant, Optional ByVal HelpFile As Variant, Optional ByVal Context As Variant, Optional ByVal MaxLength As Variant, Optional ByVal Flags As Variant, Optional ByVal PasswordChar As Variant) As Variant
  124. ' Slave function to implement InputBox, InputLCase, InputUCase and InputPassword
  125.  
  126. Dim AppHelpFile As String
  127. AppHelpFile = App.HelpFile
  128.  
  129. Load InputForm
  130.  
  131. With InputForm
  132.     ' Set coordinates
  133.     CenterToParent InputForm
  134.     If IsNumeric(xpos) Then .Left = CLng(xpos)
  135.     If IsNumeric(ypos) Then .Top = CLng(ypos)
  136.     
  137.     ' Set helpfile and context id
  138.     If Not IsMissing(HelpFile) Then App.HelpFile = Format(HelpFile)
  139.     If IsNumeric(Context) Then
  140.         .HelpContextID = CLng(Context)
  141.         .Help.Visible = True
  142.     Else
  143.         .HelpContextID = 0
  144.         .Help.Visible = False
  145.     End If
  146.  
  147.     ' Set Flags
  148.     If Not IsMissing(Flags) Then
  149.         ' Password
  150.         If Flags And IBPassword Then
  151.             If Not IsMissing(PasswordChar) And Not IsNull(PasswordChar) Then
  152.                 .Answer.PasswordChar = CStr(PasswordChar)
  153.             Else
  154.                 .Answer.PasswordChar = IBDefaultPasswordChar
  155.             End If
  156.         End If
  157.         If Flags And IBUcase Then
  158.             .CharCase = IBUcase
  159.         ElseIf Flags And IBLcase Then
  160.             .CharCase = IBLcase
  161.         Else
  162.             .CharCase = 0
  163.         End If
  164.     End If
  165.     
  166.     ' Set prompt, title
  167.     .Question = Prompt
  168.     If Not IsMissing(Title) And Not IsNull(Title) Then
  169.         .Caption = CStr(Title)
  170.     Else
  171.         .Caption = App.Title
  172.     End If
  173.     
  174.     ' Set default string and maximum length
  175.     If IsNumeric(MaxLength) Then
  176.         .Answer.MaxLength = CLng(MaxLength)
  177.         If Not IsMissing(Default) And Not IsNull(Default) Then .Answer = Left(CStr(Default), CLng(MaxLength))
  178.     Else
  179.         If Not IsMissing(Default) And Not IsNull(Default) Then .Answer = CStr(Default)
  180.     End If
  181.  
  182.     ' Show the form
  183.     .Show vbModal
  184.     
  185.     If .Tag = "OK" Then
  186.         ' If the user pressed OK, return the Answer
  187.         DoInputBox = .Answer
  188.     Else
  189.         ' If the user pressed Cancel, return Null
  190.         DoInputBox = IBValueOnCancel
  191.     End If
  192. End With
  193.  
  194. Unload InputForm
  195. App.HelpFile = AppHelpFile
  196.  
  197. End Function
  198.  
  199.  
  200. #If UseBuiltInInputBox = False Then
  201.  
  202. Public Function InputBox(ByVal Prompt As String, Optional ByVal Title As Variant, Optional ByVal Default As Variant, Optional ByVal xpos As Variant, Optional ByVal ypos As Variant, Optional ByVal HelpFile As Variant, Optional ByVal Context As Variant, Optional ByVal MaxLength As Variant) As Variant
  203. ' This corresponds to VB's InputBox(prompt[, title][, default][, xpos][, ypos][, helpfile, context])
  204.  
  205. InputBox = DoInputBox(Prompt, Title, Default, xpos, ypos, HelpFile, Context, MaxLength)
  206.  
  207. End Function
  208.  
  209. #End If
  210.  
  211. Public Function InputLCase(ByVal Prompt As String, Optional ByVal Title As Variant, Optional ByVal Default As Variant, Optional ByVal xpos As Variant, Optional ByVal ypos As Variant, Optional ByVal HelpFile As Variant, Optional ByVal Context As Variant, Optional ByVal MaxLength As Variant) As Variant
  212. ' Like InputBox but turns all input to lower case
  213.  
  214. InputLCase = DoInputBox(Prompt, Title, Default, xpos, ypos, HelpFile, Context, MaxLength, IBLcase)
  215.  
  216. End Function
  217.  
  218. Public Function InputPassword(ByVal Prompt As String, Optional ByVal Title As Variant, Optional ByVal Default As Variant, Optional ByVal xpos As Variant, Optional ByVal ypos As Variant, Optional ByVal HelpFile As Variant, Optional ByVal Context As Variant, Optional ByVal MaxLength As Variant, Optional ByVal PasswordChar As Variant) As Variant
  219. ' Like InputBox but masks all input with PasswordChar (IBDefaultPasswordChar if PasswordChar is not set)
  220.  
  221. InputPassword = DoInputBox(Prompt, Title, Default, xpos, ypos, HelpFile, Context, MaxLength, IBPassword, PasswordChar)
  222.  
  223. End Function
  224.  
  225. Public Function InputUCase(ByVal Prompt As String, Optional ByVal Title As Variant, Optional ByVal Default As Variant, Optional ByVal xpos As Variant, Optional ByVal ypos As Variant, Optional ByVal HelpFile As Variant, Optional ByVal Context As Variant, Optional ByVal MaxLength As Variant) As Variant
  226. ' Like InputBox but turns all input to UPPER CASE
  227.  
  228. InputUCase = DoInputBox(Prompt, Title, Default, xpos, ypos, HelpFile, Context, MaxLength, IBUcase)
  229.  
  230. End Function
  231. Private Function ParentForm(F As Form) As Integer
  232. ' Returns the index of the parent "forms(_i_)"
  233.  
  234. Dim i As Integer
  235. For i = 0 To Forms.Count - 1
  236.     If Forms(i) Is Screen.ActiveForm Then
  237.         ParentForm = i
  238.         Exit Function
  239.     End If
  240. Next
  241.  
  242. End Function
  243.  
  244.  
  245.