AutoClosing inputbox ( timeout )

AutoClosing inputbox ( timeout )

ILU
Enthusiast Enthusiast
1,748 Views
13 Replies
Message 1 of 14

AutoClosing inputbox ( timeout )

ILU
Enthusiast
Enthusiast

hi there,

 

I am looking for an iLogic code to auto close an input box ( to chose "Cancel" if in 5 seconds "OK" button it's not clicked )

 

Thanks

 

@Anonymous Waguespack

0 Likes
Accepted solutions (2)
1,749 Views
13 Replies
Replies (13)
Message 2 of 14

Tony_Yates
Advocate
Advocate

Hi Curtis,

 

We have one for closing a message box, maybe you can modify it to suit.

Imports System.Threading.Tasks
Dim timeout = 2 ' seconds

Dim form As New Form() With { .Enabled = False }
Task.Delay(TimeSpan.FromSeconds(timeout)).ContinueWith(Sub(t) 
		form.Close() 
	End Sub ,
TaskScheduler.FromCurrentSynchronizationContext())

MessageBox.Show(form, String.Format("Screenshot - " & CurrentFile & " - Saved) ", "Screenshot Saved.", timeout), "Save Location")
0 Likes
Message 3 of 14

ILU
Enthusiast
Enthusiast

Thanks @Tony_Yates for answering, but i don't know any way how to adapt that snip-code to an input box , I tried but I am to new to iLogic 🙂 

any other ideas of how to do it?

 

0 Likes
Message 4 of 14

JhoelForshav
Mentor
Mentor

Hi @ILU 

Try this iLogic Rule 🙂

 

Class ThisRule
	Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA"(ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
	Private Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA"(ByVal hWndParent As Integer, ByVal hWndChildAfter As Integer, ByVal lpszClass As String, ByVal lpszWindow As String) As Integer
	Private Declare Function SendMessage Lib "user32" Alias "SendMessageA"(ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
	Private Const BM_CLICK As Integer = &HF5&

	Public WithEvents oTimer As New System.Windows.Forms.Timer
	Dim second As Integer = 0
	Dim oInputBoxName As String = "ExampleBox" 'This is the Title of the inputbox, you must use this variable for the title.
	
	Sub Main
		oTimer.Interval = 1000
		oTimer.Start
		
		Dim oReturnString As String = InputBox("Wait 5 seconds for me to close...", oInputBoxName)
		
		oTimer.Stop()
	End Sub

	Sub oTimer_Tick(ByVal sender As System.Object, ByVal E As System.EventArgs) Handles oTimer.Tick

		second = second + 1
		If second >= 5 Then
			oTimer.Stop()
			Try
				Dim h1 = FindWindow(Nothing, oInputBoxName)
				h2 = FindWindowEx(h1, h2, Nothing, "Cancel")
				SendMessage(h2, BM_CLICK, 0, 0)
				second = 0
			Catch
				'Cant find inputbox
			End Try
		End If

	End Sub
End Class
Message 5 of 14

JhoelForshav
Mentor
Mentor
Message 6 of 14

ILU
Enthusiast
Enthusiast

Thanks @JhoelForshav 
you are awesome 😄

 

I didn't reply anything yet because I was still trying to adapt it according to our needs. BUT I am really new with iLogic and it seems impossible for me.

Rule that I use looks like this:

strClient = "M:\CLIENTS\"
oFilename = ThisDoc.FileName(False)
oProjectfol = (Left(oFilename, 6))
oProjectSubfol = "\3.Production\2.Drawings\1.Production designs\"

Dim dir1() As String = System.IO.Directory.GetDirectories(strClient , oProjectfol & "*")
Dim dir2 As String = String.Concat(dir1) & oProjectsubfol

modelName = IO.Path.GetFileName(ThisDrawing.ModelDocument.FullFileName)
oTitle =iProperties.Value(modelName,"Summary", "Title")

mbox = InputBox(dir2, "Save PDF to folder", oProjectfol & " - " & oTitle)

If mbox = "" Then
Return

Else
strNamePDF = dir2 & mbox
ThisDoc.Document.SaveAs(strNamePDF & (".pdf"), True)

End If

0 Likes
Message 7 of 14

JhoelForshav
Mentor
Mentor
Accepted solution

Hi @ILU 

I see. I didn't mean to stress you. I just found this to be a fun function and decided to make a screencast of it. Then I felt like i should include it in this thread 🙂

 

An implementation of the autoclosing inputbox in your code would look something like this:

Class ThisRule
	Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA"(ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
	Private Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA"(ByVal hWndParent As Integer, ByVal hWndChildAfter As Integer, ByVal lpszClass As String, ByVal lpszWindow As String) As Integer
	Private Declare Function SendMessage Lib "user32" Alias "SendMessageA"(ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
	Private Const BM_CLICK As Integer = &HF5&

	Public WithEvents oTimer As New System.Windows.Forms.Timer
	Dim second As Integer = 0
	Dim oInputBoxName As String

	Sub Main
		oTimer.Interval = 1000


		strClient = "M:\CLIENTS\"
		oFilename = ThisDoc.FileName(False)
		oProjectfol = (Left(oFilename, 6))
		oProjectSubfol = "\3.Production\2.Drawings\1.Production designs\"

		Dim dir1() As String = System.IO.Directory.GetDirectories(strClient, oProjectfol & "*")
		Dim dir2 As String = String.Concat(dir1) & oProjectSubfol

		modelName = IO.Path.GetFileName(ThisDrawing.ModelDocument.FullFileName)
		oTitle = iProperties.Value(modelName, "Summary", "Title")

		oInputBoxName = "Save PDF to folder"
		oTimer.Start
		mbox = InputBox(dir2, oInputBoxName, oProjectfol & " - " & oTitle)
		oTimer.Stop()
		If mbox = "" Then
			Return

		Else
			strNamePDF = dir2 & mbox
			ThisDoc.Document.SaveAs(strNamePDF & (".pdf"), True)

		End If
	End Sub

	Sub oTimer_Tick(ByVal sender As System.Object, ByVal E As System.EventArgs) Handles oTimer.Tick

		second = second + 1
		If second >= 5 Then
			oTimer.Stop()
			Try
				Dim h1 = FindWindow(Nothing, oInputBoxName)
				h2 = FindWindowEx(h1, h2, Nothing, "Cancel")
				SendMessage(h2, BM_CLICK, 0, 0)
				second = 0
			Catch
				'Cant find inputbox
			End Try
		End If

	End Sub
End Class

 

Message 8 of 14

ILU
Enthusiast
Enthusiast

that's wonderful
Hard for me to understand the "Logic" from this iLogic rule , but it works and Im going to use it 😄

Thanks @JhoelForshav 

0 Likes
Message 9 of 14

JhoelForshav
Mentor
Mentor
Accepted solution

@ILU 

The rule is a little "beyond iLogic", since i'm implementing functions from user32.dll and a timer from system.windows.forms.

I try to explain it better with comments in the code under the video here:

https://knowledge.autodesk.com/support/inventor/learn-explore/caas/screencast/Main/Details/5f902d27-...

 

Basically I start a timer with 1000 milliseconds between each "tick", just before the inputbox is shown. Each tick triggers the sub oTimer_Tick.

That sub then adds 1 to the integer value of the variable "second". Then it checks if second is greater than or equal to 5. That means five seconds have passed. If that's the case the function finds the window with the title of the inputbox (meaning it finds the inputbox). Then it searches for the button "cancel" in that inputbox and finally it clicks that button.

 

If the "OK" button is clicked the main sub continues and immediatly stops the timer, meaning it'll stop ticking and therefore never reaches 5 seconds 🙂

Message 10 of 14

engilic
Advocate
Advocate

Hi,

 

Any chance for VBA code for timed-out messages (for macros)?

 

Thank you

 

Have a lovely day

0 Likes
Message 11 of 14

rogmitch
Advocate
Advocate

Try the following 

 

RM

 

Private Declare PtrSafe Function MsgBoxTimeout _
        Lib "user32" _
        Alias "MessageBoxTimeoutA" ( _
            ByVal hwnd As LongPtr, _
            ByVal lpText As String, _
            ByVal lpCaption As String, _
            ByVal wType As VbMsgBoxStyle, _
            ByVal wlange As Long, _
            ByVal dwTimeout As Long) _
    As Long

Public Sub MessageBoxTimeout()
    Call MsgBoxTimeout(0, "This message box will be closed after 3 seconds ", "Timeout", vbInformation, 0, 3000)
End Sub
Message 12 of 14

engilic
Advocate
Advocate

That's great @rogmitch ,

 

Is there any way to remove buttons, so just a popup message disappears?

 

ooooo, this is gooooood.

0 Likes
Message 13 of 14

rogmitch
Advocate
Advocate

I am not aware of being able to create a messagebox without any buttons.  An alternative would be to

  • Create a Userform
  • Show it modally
  • Wait using the vba Timer function
  • Hide the form

 

RM

Message 14 of 14

engilic
Advocate
Advocate

Thank you @rogmitch 

Very helpful.

0 Likes