Solved

Rasing events vb.net

Posted on 2014-11-11
2
131 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 33

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# Linq - Join two objects into one 3 37
C# Service FileSystemwatcher isse 9 47
Selenium and Xpath 4 35
Visual Studio 2015 auto inserted code 12 71
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

730 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