{sharing} Custom VB.Net Form in iLogic

{sharing} Custom VB.Net Form in iLogic

Owner2229
Advisor Advisor
2,316 Views
5 Replies
Message 1 of 6

{sharing} Custom VB.Net Form in iLogic

Owner2229
Advisor
Advisor

Hey guys, I'd like to share some lines of my code as I believe it might be useful to someone.

All it does is it creates a custom VB.Net form (on each run) and adds some dynamic elements to it (combobox, textbox, picture, button, ...).

To "see" what it does just paste it into a new iLogic rule and run it.

 

Highlighted parts:

Blue - code adding form elements

Red - defines the size of the form

Green - shows a textbox (after the form closes) with the values entered in the comboboxes

Orange - sub-function executed on the "Do stuff" button-click

 

Imported elements:
AddLB - adds label text
AddCB - adds combobox
AddTB - adds textbox
AddPB - adds picturebox

AddBT - adds button

 

To find the parameters passed to each of the elements just scroll to it and read it's header.

 

Imports System
AddReference "System.Drawing.dll"
Imports System.Windows.Forms

Sub Main()
	CreateForm("MyForm")
	MF.ShowDialog()
	MsgBox("ComboBox one value: " & CB(0).Text & vbNewLine & "ComboBox two value: " & CB(1).Text & _
	vbNewLine & "TextBox one value: " & TB(0).Text)
End Sub

Private MF As System.Windows.Forms.Form
Private BT(0) As System.Windows.Forms.Button
Private CB(0) As System.Windows.Forms.ComboBox
Private LB(0) As System.Windows.Forms.Label
Private PB(0) As System.Windows.Forms.PictureBox
Private TB(0) As System.Windows.Forms.TextBox

Private Sub CreateForm(Optional Name As String = vbNullString)
	MF = New System.Windows.Forms.Form
	MF.Text = Name
	MF.AutoScaleMode = AutoScaleMode.None
	MF.Size = New Drawing.Size(300, 300) 'Width, Heigth
	MF.MinimumSize = MF.Size
	MF.MaximumSize = MF.Size
	MF.Font = New Drawing.Font(MF.Font.FontFamily, 10)
	MF.MaximizeBox = False
	MF.MinimizeBox = False
	MF.ShowIcon = False
	MF.SizeGripStyle = SizeGripStyle.Hide
	MF.StartPosition = FormStartPosition.CenterScreen
	
	AddLB(5, 5, "Parameter One")
	AddLB(5, 40, "Parameter Two")
	AddCB(120, 5, {"Option one", "Option two"}, 150)
	AddCB(120, 40, {"Option three", "Option four"}, 150)
	AddTB(120, 75, "sometext", 150)
	AddPB(20, 120, "C:\Path\01.jpg")
	
	Dim oBT As Button = AddBT(100, 231, "Do stuff")
	AddHandler oBT.Click, AddressOf Me.DoStuff
	
	AddExitButton()
End Sub

Private Sub DoStuff()
	Dim oDoc As Document = ThisApplication.ActiveDocument
	Dim FN As String = oDoc.DisplayName
	MsgBox("Doing stuff in:" & vblf & FN)
End Sub

Private Function AddLB(PosX As Integer, PosY As Integer, Optional Caption As String = vbNullString, Optional Width As Integer = 100) As System.Windows.Forms.Label
	Dim LC As Integer = LB.Length - 1
	If Not LB(LC) Is Nothing Then
		LC = LC + 1
		ReDim Preserve LB(LC)
	End If
	LB(LC) = New System.Windows.Forms.Label
	LB(LC).Name = "L" & LC
	LB(LC).Location = New Drawing.Point(PosX, PosY)
	LB(LC).Text = Caption
	LB(LC).Width = Width
	MF.Controls.Add(LB(LC))
	Return LB(LC)
End Function

Private Function AddTB(PosX As Integer, PosY As Integer, Optional Caption As String = vbNullString, Optional Width As Integer = 100) As System.Windows.Forms.TextBox
	Dim LC As Integer = TB.Length - 1
	If Not TB(LC) Is Nothing Then
		LC = LC + 1
		ReDim Preserve TB(LC)
	End If
	TB(LC) = New System.Windows.Forms.TextBox
	TB(LC).Location = New Drawing.Point(PosX, PosY)
	TB(LC).Name = "TB" & LC
	TB(LC).Text = Caption
	TB(LC).Width = Width
	MF.Controls.Add(TB(LC))
	Return TB(LC)
End Function

Private Function AddCB(PosX As Integer, PosY As Integer, Values() As String, Optional Width As Integer = 100) As System.Windows.Forms.ComboBox
	Dim LC As Integer = CB.Length - 1
	If Not CB(LC) Is Nothing Then
		LC = LC + 1
		ReDim Preserve CB(LC)
	End If
	CB(LC) = New System.Windows.Forms.ComboBox
	CB(LC).Location = New Drawing.Point(PosX, PosY)
	CB(LC).Name = "CB" & LC
	CB(LC).Width = Width
	For Each Value As String In Values
		CB(LC).Items.Add(Value)
	Next
	MF.Controls.Add(CB(LC))
	Return CB(LC)
End Function

Private Function AddPB(PosX As Integer, PosY As Integer, PicPath As String, Optional Width As Integer = 0, Optional Height As Integer = 0) As System.Windows.Forms.PictureBox
	Dim Pic As Drawing.Image = Nothing
	If PicPath <> vbNullString And System.IO.File.Exists(PicPath) Then
		Try
			Pic = Drawing.Image.FromFile(PicPath)
		Catch
		End Try
	End If
	If Pic IsNot Nothing Then
		If Width = 0 Then Width = Pic.Width
		If Height = 0 Then Height = Pic.Height
	End If
	Dim LC As Integer = PB.Length - 1
	If Not PB(LC) Is Nothing Then
		LC = LC + 1
		ReDim Preserve PB(LC)
	End If
	PB(LC) = New System.Windows.Forms.PictureBox
	PB(LC).Location = New Drawing.Point(PosX, PosY)
	PB(LC).Name = "PB" & LC
	PB(LC).Width = Width
	PB(LC).Height = Height
	PB(LC).Image = Pic
	MF.Controls.Add(PB(LC))
	Return PB(LC)
End Function

Private Function AddBT(PosX As Integer, PosY As Integer, Caption As String, Optional Width As Integer = 80) As System.Windows.Forms.Button
	Dim LC As Integer = BT.Length - 1
	If Not BT(LC) Is Nothing Then
		LC = LC + 1
		ReDim Preserve BT(LC)
	End If
	BT(LC) = New System.Windows.Forms.Button
	BT(LC).Name = "BT" & LC
	BT(LC).Location = New Drawing.Point(PosX, PosY)
	BT(LC).Text = Caption
	BT(LC).Width = Width
	MF.Controls.Add(BT(LC))
	Return BT(LC)
End Function

Private Sub AddExitButton()
	Dim oBT As Button = AddBT(0, 0, "OK")
	Dim PosX As Integer = MF.Width - (oBT.Width * 1.3)
	Dim PosY As Integer = MF.Height - (oBT.Height * 3)
	oBT.Location = New Drawing.Point(PosX, PosY)
	AddHandler oBT.Click, AddressOf Me.ExitButtonClick
End Sub

Private Sub ExitButtonClick()
	MF.Close()
End Sub

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
2,317 Views
5 Replies
Replies (5)
Message 2 of 6

MechMachineMan
Advisor
Advisor

Thanks!

 

I had seen this earlier!.Seems like it could definitely be a useful chunk of code! Planning on using something like this for a dockable window iProp editor.

 

Did you originally create it by pulling code from the Form.Designer.vb in a visual studio project?


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 3 of 6

Owner2229
Advisor
Advisor

Well, kind of. I've created (and edited) a lot (dozens) of forms using the designer and I've learned and remembered most of it's functions. Then I've designed my own (this) on-the-go form builder. It's good for iLogic, even thought I'm not using it much lately (since I've moved to AddIns).

It might be useful for those who need more than the in-build iLogic form, but don't want to move to AddIns (yet).

Also, I've tuned it a bit since the last version I've posted (to someone as an answer).

 

I can write in some more element builders if someone would have any use for them, e.g.:

ProgressBar

CheckBox

RadioButton

ListBox

MenuStrip (on right-click)

Open/Save file/folder dialog

 

Or even some custom elements like: circular progressbar, camera/cube control, drawing's title/iProperties population ... you name it.

 

Let me know if you'll want my help with your form.

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 4 of 6

Ben-Cornelius
Collaborator
Collaborator

Hey could you provide the element builder for input listbox the restriction on the input listbox width with ilogic is driving us nutty. Thanks!

0 Likes
Message 5 of 6

Owner2229
Advisor
Advisor

Hey, something like this?

 

Imports System
AddReference "System.Drawing.dll"
Imports System.Windows.Forms

Sub Main()
	CreateForm("MyForm")
	MF.ShowDialog()
	MsgBox("TextBox one value: " & TB(0).Text)
	MF.Dispose()
	MF = Nothing
End Sub

Private MF As System.Windows.Forms.Form
Private BT(0) As System.Windows.Forms.Button
Private LB(0) As System.Windows.Forms.Label
Private TB(0) As System.Windows.Forms.TextBox

Private Sub CreateForm(Optional Name As String = vbNullString)
	MF = New System.Windows.Forms.Form
	MF.Text = Name
	MF.AutoScaleMode = AutoScaleMode.None
	MF.Size = New Drawing.Size(300, 150) 'Width, Heigth
	MF.MinimumSize = MF.Size
	MF.Font = New Drawing.Font(MF.Font.FontFamily, 10)
	MF.MaximizeBox = False
	MF.MinimizeBox = False
	MF.ShowIcon = False
	MF.SizeGripStyle = SizeGripStyle.Hide
	MF.StartPosition = FormStartPosition.CenterScreen
	
	AddLB(5, 5, "Gimme input:")
	Dim oTB As System.Windows.Forms.TextBox = AddTB(5, 30, "sometext", 270)
	oTB.Anchor = AnchorStyles.Top Or AnchorStyles.Left Or AnchorStyles.Bottom Or AnchorStyles.Right
	oTB.MultiLine = True
	oTB.Height = 42
	
	AddExitButton()
End Sub

Private Function AddLB(PosX As Integer, PosY As Integer, Optional Caption As String = vbNullString, Optional Width As Integer = 100) As System.Windows.Forms.Label
	Dim LC As Integer = LB.Length - 1
	If Not LB(LC) Is Nothing Then
		LC = LC + 1
		ReDim Preserve LB(LC)
	End If
	LB(LC) = New System.Windows.Forms.Label
	LB(LC).Name = "L" & LC
	LB(LC).Location = New Drawing.Point(PosX, PosY)
	LB(LC).Text = Caption
	LB(LC).Width = Width
	MF.Controls.Add(LB(LC))
	Return LB(LC)
End Function

Private Function AddTB(PosX As Integer, PosY As Integer, Optional Caption As String = vbNullString, Optional Width As Integer = 100) As System.Windows.Forms.TextBox
	Dim LC As Integer = TB.Length - 1
	If Not TB(LC) Is Nothing Then
		LC = LC + 1
		ReDim Preserve TB(LC)
	End If
	TB(LC) = New System.Windows.Forms.TextBox
	TB(LC).Location = New Drawing.Point(PosX, PosY)
	TB(LC).Name = "TB" & LC
	TB(LC).Text = Caption
	TB(LC).Width = Width
	MF.Controls.Add(TB(LC))
	Return TB(LC)
End Function

Private Function AddBT(PosX As Integer, PosY As Integer, Caption As String, Optional Width As Integer = 80) As System.Windows.Forms.Button
	Dim LC As Integer = BT.Length - 1
	If Not BT(LC) Is Nothing Then
		LC = LC + 1
		ReDim Preserve BT(LC)
	End If
	BT(LC) = New System.Windows.Forms.Button
	BT(LC).Name = "BT" & LC
	BT(LC).Location = New Drawing.Point(PosX, PosY)
	BT(LC).Text = Caption
	BT(LC).Width = Width
	MF.Controls.Add(BT(LC))
	Return BT(LC)
End Function

Private Sub AddExitButton()
	Dim oBT As Button = AddBT(0, 0, "OK")
	Dim PosX As Integer = MF.Width - (oBT.Width * 1.3)
	Dim PosY As Integer = MF.Height - (oBT.Height * 3)
	oBT.Location = New Drawing.Point(PosX, PosY)
	oBT.Anchor = AnchorStyles.Bottom Or AnchorStyles.Right
	AddHandler oBT.Click, AddressOf Me.ExitButtonClick
End Sub

Private Sub ExitButtonClick()
	MF.Close()
End Sub
Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 6 of 6

Ben-Cornelius
Collaborator
Collaborator

Hey thanks for the speedy reply. Sorry no so I want to emulate the input list box from ilogic as below so the list would be a read only array list and the input would be the user picking one of the options. Thanks again!

 

listbox.JPG

 

0 Likes