How call and to pass parameters to ... TestRevised(sender As Object, e As EventArgs) in vb.net

There is a sub like:

Private Sub TestRevised(sender As Object, e As EventArgs) Handles FirstNameTextBox.TextChanged, LastNa...
.
.
.
End Sub

Question: How can I call this from another event?

Something like:  
Dim myTextBox As New TextBox("FirstName")  
myTextBox =CType( myTextBox , Object)

Then use  TestRevised(myTextBox ,???)

Question: How can I do something like this?

I know that I could move my code to a new sub without any parameters and call it from both of these subs. But I want to learn how I could do this with parameters as stated above.
LVL 34
Mike EghtebasDatabase and Application DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

it_saigeDeveloperCommented:
You would use:
Dim myTextBox As New TextBox("FirstName")
TestRevised(myTextBox, New EventArgs())

Open in new window


You don't have to cast myTextBox as an Object since myTextBox *is* an object.

-saige-
0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Could you please teach me about what "New EventArgs()" is and what it transfers?
0
it_saigeDeveloperCommented:
It depends on the Event Arguments themselves.  Many event arguments carry information relevent to the event itself, e.g. 1.  Mouse related event arguments can carry information concerning the mouse; button state, pointer state, x and y axis coordinates.
2.  Grid related event arguments can carry information concerning the grid; the selected row or column.

This happens because ultimately all event arguments inherit from the base class EventArgs.

In the case of a button click event, no additional information is relevant because we know what is happening, a button has been clicked.  To determine which button it is, we pass the button as an object (because button's ultimately inherit from the base class object).  Since we know that we assigned this event to a button and we know which button it is we merely have to provide the action that happens when the button is pressed.

http://msdn.microsoft.com/en-us/library/system.eventargs(v=vs.110).aspx

-saige-
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

louisfrCommented:
Instead of creating a New EventArgs(), I'd pass EventArgs.Empty to the event.
0
it_saigeDeveloperCommented:
Here is an example where I created my own Event Arguments to signal that a thread has completed it's work:
Imports System.Collections.Generic
Imports System.Threading

Public Class Worker
#Region "ThreadWorderCompleted Memebers"
	Public Event ThreadWorkerCompleted As ThreadWorkerCompletedEventHandler

	Protected Overridable Sub OnThreadWorkerCompleted(ByVal sender As Object, ByVal e As ThreadWorkerCompletedEventArgs)
		RaiseEvent ThreadWorkerCompleted(sender, e)
	End Sub
#End Region

	Private shouldStop As Boolean = False
	Private m_id As Integer = -1
	Public Property ID() As Integer
		Get
			Return m_id
		End Get
		Private Set(ByVal value As Integer)
			If Not value.Equals(m_id) Then
				m_id = value
			End If
		End Set
	End Property

	Public Sub DoWork()
		Dim count As Integer = 0
		While Not shouldStop
			Console.WriteLine(String.Format("Worker Thread {0}: Working ({1} passes)...", ID, count + 1))
			' Simulating long running process.
			Thread.Sleep(200)
			count += 1
			If count = 5 Then
				shouldStop = True
			End If
		End While
		Console.WriteLine(String.Format("Worker Thread {0}: Terminating gracefully", ID))
		OnThreadWorkerCompleted(Me, New ThreadWorkerCompletedEventArgs(ID))
	End Sub

	Public Sub RequestStop()
		shouldStop = True
	End Sub

	Private Sub New()
	End Sub

	Public Sub New(ByVal ID As Integer)
		m_id = ID
	End Sub
End Class

Class Module1
	Shared reset As New AutoResetEvent(False)
	Shared workers As New List(Of Worker)()
	Shared creatingThreads As Boolean = False

	Shared Sub Main(ByVal args As String())
		creatingThreads = True
		For i As Integer = 0 To 4
			CreateAndStartThreads(i + 1)
		Next
		Console.WriteLine("Settings the reset event to block the main thread")
		reset.WaitOne()
		Console.WriteLine("Finished executing all threads.")
		Console.ReadLine()
	End Sub

	Public Shared Sub CreateAndStartThreads(ByVal ID As Integer)
		Dim worker As New Worker(ID)
		AddHandler worker.ThreadWorkerCompleted, AddressOf OnThreadWorkerCompleted
		Dim thread As New Thread(AddressOf worker.DoWork) With { _
		 .Priority = ThreadPriority.Normal, _
		 .IsBackground = True _
		}
		thread.Start()
		Console.WriteLine(String.Format("Adding worker with ID - {0}", ID))
		workers.Add(worker)

		If ID = 5 Then
			creatingThreads = False
		End If
	End Sub

	Public Shared Sub OnThreadWorkerCompleted(ByVal sender As Object, ByVal e As ThreadWorkerCompletedEventArgs)
		If TypeOf sender Is Worker Then
			Console.WriteLine(String.Format("Removing worker with ID - {0}", e.ID))
			workers.Remove(TryCast(sender, Worker))
			If Not creatingThreads AndAlso workers.Count = 0 Then
				reset.[Set]()
			End If
		End If
	End Sub
End Class

Public Delegate Sub ThreadWorkerCompletedEventHandler(ByVal sender As Object, ByVal e As ThreadWorkerCompletedEventArgs)

Public Class ThreadWorkerCompletedEventArgs
	Inherits EventArgs
	' Ideally you really want to use items that can identify the thread.
	Public Property ID() As Integer
		Get
			Return m_ID
		End Get
		Private Set(ByVal value As Integer)
			m_ID = value
		End Set
	End Property
	Private m_ID As Integer

	Private Sub New()
		ID = 0
	End Sub

	Public Sub New(ByVal ID As Integer)
		Me.ID = ID
	End Sub
End Class

Open in new window

Notice how the ThreadWorkerCompletedArgs class inherits from EventArgs -
Public Class ThreadWorkerCompletedEventArgs
	Inherits EventArgs
...
End Class

Open in new window


And then in the OnThreadWorkerCompleted event method, I use the ID of the Worker that I passed in in order to display which worker is affected and then cast the sender as a Worker in order to remove it from the list of workers.

-saige-
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
I appreciate for the sample. I have to read it couple of times to get comfortable with it and be able to use it later. Later, I may have new followup questions.

Thanks,

Mike
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.