{sharing} Custom VB.Net Form in iLogic
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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
- - - - - - - - - - - - - - -
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