Solved

InputBox

Posted on 2000-04-10
11
558 Views
Last Modified: 2012-06-27
Hello Experts,

I would like to ask a a question regarding InputBox use in VB.

How to differenciate the buttons between ok and cancel when the field is empty.
0
Comment
Question by:dekoay
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2699472
You can't.
You need to implement your own InputBox
0
 
LVL 5

Expert Comment

by:Voodooman
ID: 2699582
Hi

AngelIII is correct - the inputbox is very basic and not used by most VB Programmers.

We make our own!

Good Luck

Voodooman!
0
 
LVL 2

Expert Comment

by:corvanderlinden
ID: 2699709
If the user clicks Cancel, the function returns a zero-length string ("").
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2699731
corvanderlinden:
if the user deletes the text in the inputbox and clicks OK, the same zero-length string ("") is returned (wink)
0
 
LVL 2

Expert Comment

by:corvanderlinden
ID: 2699741
That has nothing to do with the question.
If a zero lenght string is returned you know you have to take some action in your program (why else use an input box?)
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 50 total points
ID: 2699755
corvanderlinden: please reread the question (i put the important words in captial letters)
"How to DIFFERENTIATE the buttons between ok and cancel WHEN the field IS empty.
0
 
LVL 2

Expert Comment

by:corvanderlinden
ID: 2699959
AngelIII you are wright, I am wrong and dekoay has to make his own inputbox
0
 
LVL 28

Expert Comment

by:Ark
ID: 2703035
Hi
AngelIII
Never say never :-). Nothing impossible with VB <smile>
Take a look:
'----- Bas module code---
Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Public Declare Function SetTimer& Lib "user32" (ByVal hwnd&, ByVal nIDEvent&, ByVal uElapse&, ByVal lpTimerFunc&)
Private Declare Function KillTimer& Lib "user32" (ByVal hwnd&, ByVal nIDEvent&)
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
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
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Public Const GWL_WNDPROC = (-4)
Public Const GW_CHILD = 5
Public Const GW_HWNDFIRST = 0
Public Const GW_HWNDNEXT = 2
Public Const NV_INPUTBOX As Long = &H5000&
Public Const WM_COMMAND = &H111
Const WM_NOTIFY = &H4E
Const WM_CLOSE = &H10


Public hMsg As Long
Public hOK As Long, hCancel As Long, hHelp As Long
Dim OldProc As Long
Public bOK As Boolean
Public bCancel As Boolean

Public Sub TimerProc(ByVal hwnd&, ByVal uMsg&, ByVal idEvent&, ByVal dwTime&)
  Dim h As Long, sClass As String, k As Long, sName As String
'If your InputBox has its own caption, change following string.
  hMsg = FindWindow("#32770", App.Title)
  KillTimer hwnd, idEvent
  h = GetWindow(hMsg, GW_CHILD)
  Do
    sClass = Space$(128)
    k = GetClassName(h, ByVal sClass, 128)
    sClass = Left$(sClass, k)
    If sClass = "Button" Then
       sName = Space$(128)
       k = GetWindowText(h, sName, 128)
       If k > 0 Then sName = Left$(sName, k)
       Select Case sName
              Case "OK"
                   hOK = h
              Case "Cancel"
                   hCancel = h
              Case "&Help"
                   hHelp = h
       End Select
    End If
    h = GetWindow(h, GW_HWNDNEXT)
  Loop While h <> 0
  OldProc = GetWindowLong(hMsg, GWL_WNDPROC)
  SetWindowLong hMsg, GWL_WNDPROC, AddressOf BtnProc
End Sub

Public Function BtnProc(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  Select Case wMsg
     Case WM_CLOSE
          BtnProc = CallWindowProc(OldProc, hwnd, wMsg, wParam, lParam)
          SetWindowLong hMsg, GWL_WNDPROC, OldProc
          Exit Function
     Case WM_COMMAND
          If lParam = hOK Then bOK = True
          If lParam = hCancel Then bCancel = True
  End Select
  BtnProc = CallWindowProc(OldProc, hwnd, wMsg, wParam, lParam)
End Function

'---Form code---
Private Sub Command1_Click()
 Dim sTemp As String
 SetTimer hwnd, NV_INPUTBOX, 10, AddressOf TimerProc
 bOK = False: bCancel = False
 sTemp = InputBox("Enter your value or press Cancel")
 If bOK Then MsgBox "You pressed OK"
 If bCancel Then MsgBox "You pressed Cancel"
 If Not bOK And Not bCancel Then MsgBox "You closed InputBox using [x]"
End Sub

' Of course, it's much more simply to make own InputBox instead of "double subclassing" - but take a look - it works :-))

Cheers
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2703132
Seeing all this code i like to run awayyyyyyyyyyyyyy.............
0
 
LVL 28

Expert Comment

by:Ark
ID: 2705624
Hi
I don't offer to use this code, I just want to show that it's possible <smile>.
Cheers
0
 

Author Comment

by:dekoay
ID: 2706170
Dear Ark,

Thanks for replying and show us some of this superb code with VB. I really appreciate it.

Thank You


Cheers
Dekoay
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB 6.0 printer how to align 6 67
Windows Script Host failed (Access is denied.) error 6 233
Help me. 3 60
transition to visual .net from vb6 5 41
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question