Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Rasing events vb.net

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

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 35

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

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

Question has a verified solution.

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

Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Loops Section Overview
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Suggested Courses
Course of the Month14 days, 9 hours left to enroll

577 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