Avatar of scprogs
scprogs
Flag for United States of America asked on

Delegate Class

I'm trying to build a delegate class that will avoid cross threading operations.  This class should be able to take most common controls and make changes to them that would result in cross threading errors.  This is the code I have so far.
Public Class Removed_Items

	Private Delegate Sub delChangeControl(ByVal ctrl As Control, ByVal prop As String, ByVal Value As String)

	Dim chk As CheckBox
	Dim txt As TextBox
	Dim rad As RadioButton

	Private Sub ChangeControl(ByVal ctrl As Control, ByVal prop As String, ByVal Value As String)
		If Me.InvokeRequired Then
			Me.Invoke(New delChangeControl(AddressOf ChangeControl), ctrl, prop, Value)
			Exit Sub
		End If

		If TypeOf (ctrl) Is CheckBox Then
			chk = ctrl
			chkBox(prop, Value)
		ElseIf TypeOf (ctrl) Is TextBox Then
			txt = ctrl
			txtBox(prop, Value)
		ElseIf TypeOf (ctrl) Is RadioButton Then
			rad = ctrl
			radButton(prop, Value)
		End If
	End Sub

	Private Sub chkBox(ByVal prop As String, ByVal value As String)
		Select Case prop
			Case "Text"
				chk.Text = value
			Case "Checked"
				chk.Checked = value
			Case "Tag"
				chk.Tag = value
		End Select
	End Sub

	Private Sub txtBox(ByVal prop As String, ByVal value As String)
		Select Case prop
			Case "Text"
				txt.Text = value
			Case "Left"
				txt.Left = value
			Case "Tag"
				txt.Tag = value
		End Select
	End Sub

	Private Sub radButton(ByVal prop As String, ByVal value As String)
		Select Case prop
			Case "Text"
				rad.Text = value
			Case "Checked"
				rad.Checked = value
			Case "Tag"
				rad.Tag = value
		End Select
	End Sub
End Class

Open in new window

I would like to avoid writing a case for each property in a control if possible, would there be an easier way to do this?
Visual Basic.NET

Avatar of undefined
Last Comment
scprogs

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Nasir Razzaq

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
scprogs

ASKER
While it led me to the right direction I still had to do some additional research to get it to work.  here is the final code that worked
Public Class Form1

	Public Delegate Sub thrCallByName(ByVal objectRef As Object, ByVal ProcName As String, ByVal UseCallType As Microsoft.VisualBasic.CallType, <[ParamArray]()> ByVal Args() As Object)

	Public Sub callName(ByVal objectRef As Object, ByVal ProcName As String, ByVal UseCallType As Microsoft.VisualBasic.CallType, <[ParamArray]()> ByVal Args() As Object)
		Me.Invoke(New thrCallByName(AddressOf CallByName), objectRef, ProcName, UseCallType, Args)
	End Sub

	'Private Sub callingName(ByVal objectRef As Object, ByVal ProcName As String, ByVal UseCallType As Microsoft.VisualBasic.CallType, <[ParamArray]()> ByVal Args() As Object)
	'	CallByName(objectRef, ProcName, UseCallType, Args)
	'End Sub

	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		Dim thr As New Threading.Thread(AddressOf dowork)
		thr.Start()
	End Sub

	Private Sub dowork()
		'Me.Invoke(CallByName(TextBox1, "Text", CallType.Set, "Test"))
		callName(TextBox1, "Text", CallType.Set, New Object() {"Test"})
		callName(Label1, "Text", CallType.Set, New Object() {"Tested"})
		callName(CheckBox1, "Checked", CallType.Set, New Object() {True})
		callName(ComboBox1.Items, "Add", CallType.Method, New Object() {"Test"})
	End Sub
End Class

Open in new window

Your help has saved me hundreds of hours of internet surfing.
fblack61