Results 1 to 2 of 2

Thread: [RESOLVED] [2.0] C# Class -> Howto RaiseEvent VB-like

  1. #1

    Thread Starter
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961

    Resolved [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:
    1. 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...
                // ...
    Nobody knows what software they want until after you've delivered what they originally asked for.

    Don't solve problems which don't exist.

    "If I had eight hours to cut down a tree, I'd spend six hours sharpening my axe." --- Abraham Lincoln (1809-1865)

    2 idiots don't make a genius.

  2. #2

    Thread Starter
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961

    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:
    1. Imports SNAPI_App
    2.  
    3. Public Class Form1
    4.  
    5.     Private WithEvents snapiApp As SNAPI_App.mainForm
    6.  
    7.     ' ...
    8.  
    9.     Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    10.         snapiApp = New SNAPI_App.mainForm
    11.         snapiApp.Show()
    12.         snapiApp.Visible = False
    13.     End Sub
    14.  
    15.     Private Sub snapiApp_ScanData(ByVal source As Object, ByVal strScanData As String) Handles snapiApp.ScanData
    16.         MsgBox(strScanData)
    17.     End Sub
    Last edited by Dave Sell; May 1st, 2008 at 06:26 PM.
    Nobody knows what software they want until after you've delivered what they originally asked for.

    Don't solve problems which don't exist.

    "If I had eight hours to cut down a tree, I'd spend six hours sharpening my axe." --- Abraham Lincoln (1809-1865)

    2 idiots don't make a genius.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width