[RESOLVED] [2.0] C# Class -> Howto RaiseEvent VB-like
All,
Most of my work involves VB.*. I am familiar with defining a VB Class with a
VB Code:
Public Event thisEvent(args)
Now I am working with a C# Class, which is consumed by VB code. I need to make the C# Class "Raise an Event" which can be handled by the VB consumer code.
Here is a code snippet, of what I think I want to do:
Code:
namespace SNAPI_App
{
public partial class mainForm : Form
{
MotoSnapi.SnapiScanner scanner1;
MotoSnapi.SnapiScanner scanner2;
int status = 0; /* Return values of function calls */
static short[] paramsList /* Parameter information list */
= new short[SnapiDLL.MAX_PARAM_LEN];
int selectedScanner = 0;
static ArrayList scannerList = new ArrayList();
public mainForm()
{
Public Event thisEvent; // <- this doesn't work...
// ...
Re: [2.0] C# Class -> Howto RaiseEvent VB-like
OK I was able to find some sample code to help out, and I got it to work:
Code:
namespace SNAPI_App
{
public partial class mainForm : Form
{
//declaring the event handler delegate
public delegate void ButtonEventHandler(object source, int clickCount);
//declaring the event
public event ButtonEventHandler ButtonClick;
//Function to trigger the event
//This will be called when the user clicks the button
public void clicked(int count)
{
if (ButtonClick != null) ButtonClick(this, count);
}
//
public delegate void ScannerInput (object source, string strScanData);
public event ScannerInput ScanData;
public void scanned(string strInput)
{
if (ScanData != null) ScanData(this, strInput);
}
public mainForm()
{
InitializeComponent();
// ...
Now, from VB-land:
VB Code:
Imports SNAPI_App
Public Class Form1
Private WithEvents snapiApp As SNAPI_App.mainForm
' ...
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
snapiApp = New SNAPI_App.mainForm
snapiApp.Show()
snapiApp.Visible = False
End Sub
Private Sub snapiApp_ScanData(ByVal source As Object, ByVal strScanData As String) Handles snapiApp.ScanData
MsgBox(strScanData)
End Sub