Solved

Rasing events vb.net

Posted on 2014-11-11
2
133 Views
Last Modified: 2014-11-11
Converting a project from C# to VB.NET and having trouble coding the event handler.
Here is sample from C# and VB.NET   Need to know how to enable the event handler in VB

   public class RowControl
    {
        public delegate void AddNewRowEventHandler(object sender, EventArgs e);
        public event AddNewRowEventHandler AddNewRowEvent;
...

on form: collectionslist

      private void AddNewRow(int rowIDX)
        {
            RowControl control = new RowControl(ramCustomersDict, rowControlsList, rowIDX);  //row 0 is column headers
            control.stopLabel.Text = rowIDX.ToString();
            rowControlsList.Add(control);
            tableLayoutPanel1.Add(control);
            control.AddNewRowEvent += new RowControl.AddNewRowEventHandler(this.AddNewRowEvent);
        }

        //***************************************************************************
        private void AddNewRowEvent(object sender, EventArgs e)
        {
            RowControl rc = rowControlsList.Last();
            if (rc.customerIdTextBox.Text.Length > 0 && rc.addressTextbox.Text.Length > 0)
            {
                int rowIDX = rowControlsList.Count + 1;
                AddNewRow(rowIDX);
            }
        }

Now in VB, I have this:

Public Class RowControl
    Public Delegate Sub AddNewRowEventHandler(sender As Object, e As EventArgs)
    Public Event AddNewRowEvent As AddNewRowEventHandler
...

on the form:
   Private Sub AddNewRow(rowIDX As Integer)
        dim control as New RowControl(ramCustomersDict, rowControlsList, rowIDX)
        'row 0 is column headers
        control.stopLabel.Text = rowIDX.ToString()
        rowControlsList.Add(control)
        TableLayoutPanel1.Add(control)

        control.AddNewRowEvent() += New RowControl.AddNewRowEventHandler(AddressOf Me.AddNewRowEvent)
    End Sub

    '***************************************************************************
    Private Sub AddNewRowEvent(sender As Object, e As EventArgs)
        Dim rc As RowControl = rowControlsList.Last
        If rc.customerIdTextBox.Text.Length > 0 AndAlso rc.addressTextbox.Text.Length > 0 Then
            Dim rowIDX As Integer = rowControlsList.Count + 1
            AddNewRow(rowIDX)
        End If
    End Sub


SO my question is....How do I get my event raised?

I am getting the error saying i Need to call raiseevent, but when I try to enter the code for raiseevent, nothing appears.

Thanks
0
Comment
Question by:lburnsinmagnolia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 500 total points
ID: 40435538
You should be using addhandler in VB.net:

 control.AddNewRowEvent() += New RowControl.AddNewRowEventHandler(AddressOf Me.AddNewRowEvent)

becomes
AddHandler(control.AddNewRowEvent, AddressOf AddNewrowEvent)
0
 
LVL 34

Expert Comment

by:it_saige
ID: 40435602
Using your above example, here is one way you could accomplish your task:

RowControl.vb -
Public Class RowControl
	Public Delegate Sub AddNewRowEventHandler(sender As Object, e As EventArgs)
	Private ReadOnly _addNewRowEventHandlers As List(Of AddNewRowEventHandler)
	Public Custom Event AddNewRowEvent As AddNewRowEventHandler
		AddHandler(ByVal value As PropertyChangedEventHandler)
			_addNewRowEventHandlers.Add(value)
		End AddHandler

		RemoveHandler(ByVal value As PropertyChangedEventHandler)
			_addNewRowEventHandlers.Remove(value)
		End RemoveHandler

		RaiseEvent(ByVal sender As Object, ByVal e As EventArgs)
			For Each handler As PropertyChangedEventHandler In _propertyChangedHandlers
				Try
					handler.Invoke(sender, e)
				Catch ex As Exception
					Debug.WriteLine(String.Format("Exception while invoking event handler: {0}", ex))
				End Try
			Next
		End RaiseEvent
	End Event

	Protected Overridable Sub OnNewRowAdded(ByVal sender As Object, ByVal e As EventArgs)
		RaiseEvent AddNewRowEvent(sender, e)
	End Sub 

	Private Sub SomeMethodUsedToAddARow(DataRow row)
		' Perform methods used to add a row
		' Ok now we fire our event.
		OnNewRowAdded(Me, New EventArgs())
	End Sub
End Class

Open in new window


Form methods -
Private Sub AddNewRow(rowIDX As Integer)
	Dim control as New RowControl(ramCustomersDict, rowControlsList, rowIDX)
	'row 0 is column headers
	control.stopLabel.Text = rowIDX.ToString()
	rowControlsList.Add(control)
	TableLayoutPanel1.Add(control)

	AddHandler control.AddNewRowEvent, AddressOf Me.AddNewRowEvent
End Sub

'***************************************************************************
Private Sub AddNewRowEvent(sender As Object, e As EventArgs)
	Dim rc As RowControl = rowControlsList.Last
	If rc.customerIdTextBox.Text.Length > 0 AndAlso rc.addressTextbox.Text.Length > 0 Then
		Dim rowIDX As Integer = rowControlsList.Count + 1
		AddNewRow(rowIDX)
	End If
End Sub

Open in new window


-saige-
0

Featured Post

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

717 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