How to break from loop by the cancel button in inputbox
The following VB script check the valid IP by Test function
if we get OK from the test function then We break from loop ValidIP=OK If not the inputBox ask the question until ValidIP=OK
But sometimes I want to exit from loop by cancel button
How to break loop by the cancel button (in order to set the ValidIP as OK)
Do Until ValidIP=OK
IPrange=InputBox("Enter IP address range",,"172开发者_JAVA百科.17.202.1-10,192.9.200.1-100")
Test IPrange, strPattern
Loop
Put something like the code below before the Test... row (code written from old memories of how vbscript works):
If Len(IPrange) = 0 Then
Exit Do
End
When the user clicks Cancel
the InputBox
returns a 0 length string.
I do not know VB script but I would solve this problem as follows.
Your IP by Test function would have run in a separate thread, the thread in which the GUI runs must be able to communicate with your other thread and set a variable.
For readability I would introduce a new variable lets say AbortedByUser this variable can be set by your other thread (event handler or something like that to not make it public..)
Then your code would be looking something like this:
Do Until ValidIP=OK AND !AbortedByUser
IPrange=InputBox("Enter IP address range",,"172.17.202.1-10,192.9.200.1-100")
Test IPrange, strPattern
Loop
Note that the AbortedByUser will has to be set to false when your function gets started.
As I don't know VB script I don't now how to write this AND NOT thing properly.. ;-)
Got an answer here. Though It is searchable via google, it is on a not-so-common-site. Hence I am posting the answer here, so that it will be available for others.
For this particular question, the answer currently accepted will work just fine.
There is a problem with the common approach which looks similar to if IPrange = "" then ...
If you press OK
keeping the inputbox empty, or if you press cancel
, or you click the close button, all 3 are treated in same way by above code.
This code however takes care of this situation:
IPrange=InputBox("Enter IP address range",,"172.17.202.1-10,192.9.200.1-100")
If TypeName(IPrange) = "Empty" Then
MsgBox "User has cancelled"
Exit Do
End If
精彩评论