Solved

Rasing events vb.net

Posted on 2014-11-11
2
124 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
2 Comments
 
LVL 39

Accepted Solution

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

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

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

Expert Comment

by:it_saige
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 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

10 Experts available now in Live!

Get 1:1 Help Now