To call a function from button handler

To call a function from button handler

dialunau
Advocate Advocate
500 Views
2 Replies
Message 1 of 3

To call a function from button handler

dialunau
Advocate
Advocate

Hello,

I'm trying to call a function from a button I added into a form, the problem is that the handler doesn't allow me to call that function because it requires external parameters, which the handler can't read due to input restrictions.

 

How can I fix this?

Some code sample:

Public Sub Button
Dim
oButton1 As New Button() oForm.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) Call MyClickFunction(oParam) End Sub 

 

0 Likes
Accepted solutions (1)
501 Views
2 Replies
Replies (2)
Message 2 of 3

JelteDeJong
Mentor
Mentor
Accepted solution

I guess this is a follow up to your other question. I don't understand your exact problem. but I figured I could show off some solutions for passing parameters around.

Notice

  • I defined the variable _occName in the class (not in a function) therefore it's available in all functions. it is set and in the sub New and was used in the function MyClickFunction 
  • I defined the public property MyProperty.  this is set in the MyClickFunction but used out site the winform class in the rule.

Hopefully you answer is solved like this.

 

Public Sub Main()
	'//////// GET THE OCCURRENCE NAME ////////
	Dim occ As ComponentOccurrence = ThisApplication.CommandManager.Pick(
			SelectionFilterEnum.kAssemblyOccurrenceFilter, 
			"Select occurrence")
	Dim occName As String = occ.Name
	
	'//////// RUN THE FORM ////////
	Dim oForm As New WinForm(occName)
	oForm.ShowDialog()
	
	MsgBox(oForm.MyProperty)
	
End Sub

Public Class WinForm
	Inherits System.Windows.Forms.Form
	
	Public oLargerFont As System.Drawing.Font = New Font("Arial", 10)
	Private _occName As String
	Public Property MyProperty As String
		
	'//////// CREATE NEW INSTANCE ////////
	Public Sub New(occName As String)
		oForm = Me
		_occName = occName
		
		'//////// PARAMETERS FOR THE FORM ////////
		With oForm
			.FormBorderStyle = FormBorderStyle.FixedToolWindow
			.StartPosition = FormStartPosition.CenterScreen
			.Width = 300
			.Height = 700
			.TopMost = True
			.Font = oLargerFont
			.Text = "Component Select"
			.Name = "Components"
			.ShowInTaskbar = False
		End With
		
		Dim oButton1 As New Button()
		With oButton1
			.Text = "MODIFY"
			.Top = 25
			.Left = 25
			.Enabled = True
			.AutoSize = True
		End With
		oForm.Controls.Add(oButton1)
		AddHandler oButton1.Click, AddressOf oButton1_Click
		
		Dim oOptions As New List(Of String)
		oOptions.AddRange({"Option 1","Option 2","Option 3"})

		Dim oGroupBox As GroupBox = New GroupBox()
		oGroupBox.Left = 25
		oGroupBox.Width = 150
		oGroupBox.Top = oButton1.Bottom + 20
		oGroupBox.Height = 100
		oGroupBox.Text = "Choose"
		oForm.Controls.Add(oGroupBox)
		
		'//////// RADIOBUTTON PARAMETERS ////////
		Dim oRadioButton1 As New RadioButton()
		oGroupBox.Controls.Add(oRadioButton1)
		oRadioButton1.Text = occName
		oRadioButton1.Top = 20
		oRadioButton1.Left = 35
		oRadioButton1.AutoSize = True
	End Sub
  
	Private Sub oButton1_Click(ByVal oSender As System.Object, ByVal oEventArgs As System.EventArgs)
		MyClickFunction("My param")
	End Sub
	
	Private Sub MyClickFunction(oParam As String)
		MsgBox(_occName + " : " + oParam)
		MyProperty = "I set my property here but it is used in the rule class"
	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 3 of 3

dialunau
Advocate
Advocate

That was really helpful, now I can access my data between classes and functions. Thank you.

0 Likes