Link to home
Create AccountLog in
Avatar of scprogs
scprogsFlag 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?
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of 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