Run Code in the background without exiting a form

Run Code in the background without exiting a form

dialunau
Advocate Advocate
643 Views
8 Replies
Message 1 of 9

Run Code in the background without exiting a form

dialunau
Advocate
Advocate

Hello
I have a form and I want to run the code for (MsgBox) whenever I click the button in my form (so I will see 2 messages whenever I click the button).
The problem is that it will show only after I close the form. How can I do this?

My code looks like this:

 

 

 

 

Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms

Public Sub Main()
	Dim oForm As New WinForm(iLogicVb.Automation)
	oForm.ShowDialog()
	
	MsgBox("TASK 2")
End Sub

Public Class WinForm
	Inherits System.Windows.Forms.Form
	
	Public Sub New(ByVal Iinf As IiLogicAutomation) 
		oForm = Me
		
		With oForm
			.FormBorderStyle = FormBorderStyle.FixedToolWindow
			.StartPosition = FormStartPosition.CenterScreen
			.Width = 300
			.Height = 400
			.Text = "MYFORM"
			.ShowInTaskbar = False
		End With
		
		Dim oButton1 As New Button()
		With oButton1
			.Text = "CLICK ME!"
			.Top = oForm.Bottom - 80
			.Left = (oForm.Width/2) - oButton1.Width
			.Enabled = True
			.AutoSize = True
		End With
		Me.Controls.Add(oButton1)
	
		AddHandler oButton1.Click, AddressOf oButton1_Click
	End Sub
  
	Public Sub oButton1_Click(ByVal oSender As System.Object, ByVal oEventArgs As System.EventArgs)
		MsgBox("TASK 1")
	End Sub
End Class

 

 

 

Thank you.

0 Likes
Accepted solutions (1)
644 Views
8 Replies
Replies (8)
Message 2 of 9

JelteDeJong
Mentor
Mentor

use oForm.Show() in stead of oForm.ShowDialog()

Public Sub Main()
	Dim oForm As New WinForm(iLogicVb.Automation)
	oForm.Show()
	
	MsgBox("TASK 2")
End Sub

 

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

Message 3 of 9

bradeneuropeArthur
Mentor
Mentor
Modal vs Modeless forms!

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 4 of 9

dlunaEVTVX
Participant
Participant

,

0 Likes
Message 5 of 9

dialunau
Advocate
Advocate

Hello all,

I'm sorry I didn't explain properly. I want my code to show the 2 MsgBoxes whenever I click the button, so the form calls the main function to execute that code when the button is clicked.

 

I tried with oForm.Show, but it causes the code to run before starting the form. On the other hand, oForm.Show(vbModeless) causes the second message to show after closing the form, so none of these attempts gave me the desired result.

 

How can I call a portion of the main function from the "oButton_click" event inside the class?

0 Likes
Message 6 of 9

Michael.Navara
Advisor
Advisor

The easiest way for execution of background task is BackgroundWorker. There are two samples how to use them. 

0 Likes
Message 7 of 9

dialunau
Advocate
Advocate

I tried the backgroungworker but it won't work in this scenario. I'll explain more detail.
I want my code to change an iProperty when I click a button, I cannot change the iProperties inside the form (I tried using IiProperties but it gives errors), that is why I must change them from the main function.

 

The problem is that I can only run code in the main function before or after running the form, but I want that iProperty to change whenever I click the button, not after the form is closed.

 

Imports System.Windows.Forms
Public Sub Main()
	Dim oForm As New WinForm()
	oForm.ShowDialog
'	oForm.Show(vbModeless)
	
	' I want to change this iProperty when I click the button
	iProperties.Value("Y.ipt", "Project", "Part Number") = "2"
	ThisDoc.Document.Update
End Sub

Public Class WinForm
	Inherits System.Windows.Forms.Form
	
	Public Sub New() 
		oForm = Me
		
		With oForm
			.FormBorderStyle = FormBorderStyle.FixedToolWindow
			.StartPosition = FormStartPosition.CenterScreen
			.Width = 300
			.Height = 400
			.Text = "MYFORM"
			.ShowInTaskbar = False
		End With
		
		Dim oButton1 As New Button()
		With oButton1
			.Text = "Change iProperty"
			.Top = oForm.Bottom - 80
			.Left = (oForm.Width/2) - oButton1.Width
			.Enabled = True
			.AutoSize = True
		End With
		Me.Controls.Add(oButton1)
	
		AddHandler oButton1.Click, AddressOf oButton1_Click
	End Sub
  
	Public Sub oButton1_Click(ByVal oSender As System.Object, ByVal oEventArgs As System.EventArgs)
		'When clicking the button, I want the program to run the iProperties change in the main function
	End Sub
End Class

 


 I've been trying various methods to access the main function without exiting the form, but no luck so far.

0 Likes
Message 8 of 9

JelteDeJong
Mentor
Mentor
Accepted solution

you can change iProperies from the form. The only thing is that you cant use the iLogic API. You need to use the Inventor API. Try this:

Imports System.Windows.Forms
Public Sub Main()

	Dim partDocument As PartDocument = ThisDoc.Document

	Dim oForm As New WinForm(partDocument)
	oForm.ShowDialog()



End Sub

Public Class WinForm
	Inherits System.Windows.Forms.Form

	Private _doc As PartDocument

	Public Sub New(doc As PartDocument)

		_doc = doc

		Dim oForm = Me
		With oForm
			.FormBorderStyle = FormBorderStyle.FixedToolWindow
			.StartPosition = FormStartPosition.CenterScreen
			.Width = 300
			.Height = 400
			.Text = "MYFORM"
			.ShowInTaskbar = False
		End With

		Dim oButton1 As New Button()
		With oButton1
			.Text = "CLICK ME!"
			.Top = oForm.Bottom - 80
			.Left = (oForm.Width / 2) - oButton1.Width
			.Enabled = True
			.AutoSize = True
		End With
		Me.Controls.Add(oButton1)

		AddHandler oButton1.Click, AddressOf oButton1_Click
	End Sub

	Public Sub oButton1_Click(ByVal oSender As System.Object, ByVal oEventArgs As System.EventArgs)
		' for more info check https://modthemachine.typepad.com/my_weblog/2010/02/accessing-iproperties.html
		Dim propertySet As PropertySet = _doc.PropertySets.Item("Design Tracking Properties")
		Dim myProperty As [Property] = propertySet.Item("Description")
		myProperty.Value = "I have been updated from the form!"
		
		msgbox("Description property has been updated!")
	End Sub
End Class

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

Message 9 of 9

dialunau
Advocate
Advocate

That worked perfectly.
modifying the property sets instead of using the iproperties.value method helped me modify them without exiting the class.

Thank you.

0 Likes