?
Solved

Rasing events vb.net

Posted on 2014-11-11
2
Medium Priority
?
134 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 2000 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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

762 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