Solved

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

Posted on 2014-10-20
6
550 Views
Last Modified: 2014-10-21
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.
0
Comment
Question by:Mike Eghtebas
  • 3
  • 2
6 Comments
 
LVL 32

Expert Comment

by:it_saige
ID: 40393581
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
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40393600
Could you please teach me about what "New EventArgs()" is and what it transfers?
0
 
LVL 32

Expert Comment

by:it_saige
ID: 40393611
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 11

Expert Comment

by:louisfr
ID: 40393713
Instead of creating a New EventArgs(), I'd pass EventArgs.Empty to the event.
0
 
LVL 32

Accepted Solution

by:
it_saige earned 500 total points
ID: 40394419
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
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40394680
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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
.net VBA word safemode 1 24
Data Saving 5 36
C# DataGridView_RowsAdded event not firing 1 39
Visual Studio editor? 1 16
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now