Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

ActiveX in C# and ABAP Class

Former Member
0 Kudos

Hello,

I implemented a small C# control: only a Timer with an event tick (an a property period, and the methods activate/deactivate):


using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace winSAPTimerLib
{
	/// <summary>
	/// Summary description for UserControl1.
	/// </summary>
	/// 

	// [Guid("BB96CBFD-BA48-4489-8970-D3778DB39A6B")]
	// public interface SAPTimerInterface {}

	//[Guid("4DC8A5FE-CC8B-4a4d-8C33-D24A20DAA744")]
	//public interface SAPTimerEvents {}

	[Guid("F7CB8BF6-AFFB-43b5-9DE3-D43E7F1A8711")]
	public class SAPTimer : System.Windows.Forms.UserControl
	{
		private System.Windows.Forms.Timer theTimer;
		private System.Windows.Forms.Label label1;
		private System.ComponentModel.IContainer components;

		public delegate void TickHandler
			(
			object SAPTimer,
			EventArgs info
			);

		public event TickHandler OnTick;

		public SAPTimer()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

			// TODO: Add any initialization after the InitComponent call

		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if( components != null )
					components.Dispose();
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			this.theTimer = new System.Windows.Forms.Timer(this.components);
			this.label1 = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// theTimer
			// 
			this.theTimer.Interval = 10000;
			this.theTimer.Tick += new System.EventHandler(this.theTimer_Tick);
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 8);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(40, 16);
			this.label1.TabIndex = 0;
			this.label1.Text = "Timer";
			// 
			// SAPTimer
			// 
			this.Controls.Add(this.label1);
			this.Name = "SAPTimer";
			this.Size = new System.Drawing.Size(48, 32);
			this.ResumeLayout(false);

		}
		#endregion

		
		public int Interval
		{
			get
			{
				MessageBox.Show("GET "+theTimer.Interval.ToString());
				return theTimer.Interval;
			}
			set
			{  
				MessageBox.Show("SET "+value.ToString());
                theTimer.Interval = value;
			}
		}

		public void Enable()
		{
			 MessageBox.Show("ENABLE");
		     theTimer.Enabled = true;
		}

		public void Disable()
		{
			MessageBox.Show("DISABLE");
		    theTimer.Enabled = false;
		}

		private void theTimer_Tick(object sender, System.EventArgs e)
		{
			
			if (OnTick == null) { MessageBox.Show("TICK null"); }
			else {MessageBox.Show("TICK not null");};

            // EventArgs info = new EventArgs();
			if (OnTick != null)
			{
				OnTick(
					this, EventArgs.Empty);
			}
			
		}
	}
}

And a class in ABAP:

METHOD constructor .

  DATA prog_id(80).

  IF parent IS INITIAL.
    RAISE error_cntl_create.
  ENDIF.

  CLASS cl_gui_cfw DEFINITION LOAD.


* assign prog_id to get the frontend specific control
  IF NOT activex IS INITIAL.
*    prog_id = 'ZPanelXControl1.TZPanelX'.
    prog_id = '{F7CB8BF6-AFFB-43b5-9DE3-D43E7F1A8711}'.
  ELSEIF NOT javabean IS INITIAL.
    RAISE gui_type_not_supported.
  ENDIF.

  IF prog_id IS INITIAL.
    RAISE gui_type_not_supported.
  ENDIF.

* Set the window styles of the control when style parameter was not
* set with constructor call.
* For more information on the styles see WIN32 SDK
  IF style IS INITIAL.
* otherwise the control would be invisible and the mistake would be
* hard to find
    style = cl_gui_control=>ws_visible
            + cl_gui_control=>ws_child
            + cl_gui_control=>ws_clipsiblings.

  ENDIF.

* Create the control
  CALL METHOD super->constructor
    EXPORTING
      clsid      = prog_id
      shellstyle = style
      parent     = parent
      lifetime   = lifetime
      name       = name
    EXCEPTIONS
      OTHERS     = 1.

  CALL METHOD cl_gui_cfw=>flush
    EXCEPTIONS
      cntl_system_error = 1
      cntl_error        = 2
      OTHERS            = 3.

  IF sy-subrc <> 0.
*    CALL METHOD me->InitializeControl.
    CALL METHOD me->init_control
      EXPORTING
        clsid             = prog_id
      EXCEPTIONS
        cntl_error        = 1
        cntl_system_error = 2
        create_error      = 3
        lifetime_error    = 4
        OTHERS            = 5.

    MESSAGE ID 'ZES_GENERAL' TYPE 'I' NUMBER 077.
    RAISE error_cntl_create.
  ENDIF.

* register instance at framework
  CALL METHOD cl_gui_cfw=>subscribe
    EXPORTING
      shellid = h_control-shellid
      ref     = me
    EXCEPTIONS
      OTHERS  = 1.

  IF sy-subrc <> 0.
    RAISE error_cntl_create.
  ENDIF.
 CALL METHOD cl_gui_cfw=>flush.
  IF sy-subrc NE 0.
    RAISE error_cntl_create.

  ENDIF.


data: events type cntl_simple_events,
wa_events type cntl_simple_event.

wa_events-eventid = 0.  " EVENT_LEFT_CLICK_RUN.
append wa_events to events.
wa_events-eventid = 1.  " EVENT_LEFT_CLICK_RUN.
append wa_events to events.
wa_events-eventid = 2.  " EVENT_LEFT_CLICK_RUN.
append wa_events to events.
wa_events-eventid = 3.  " EVENT_LEFT_CLICK_RUN.
append wa_events to events.
wa_events-eventid = 4.  " EVENT_LEFT_CLICK_RUN.
append wa_events to events.
wa_events-eventid = 5.  " EVENT_LEFT_CLICK_RUN.
append wa_events to events.

call method set_registered_events
exporting
  events = events.

ENDMETHOD.

METHOD dispatch .

break tc.
  IF me->h_control IS INITIAL.
    RAISE cntl_error.
  ENDIF.
  CASE eventid.

    WHEN 0 or 1 or 2 or 3 or 4 or 5.
      RAISE EVENT tick.

    WHEN OTHERS.
  ENDCASE.
ENDMETHOD.

METHOD enabletimer .
  CALL METHOD call_method
    EXPORTING
      method            = 'Enable'
      p_count           = 0
    EXCEPTIONS
      cntl_system_error = 1
      cntl_error        = 2
      OTHERS            = 3.
ENDMETHOD.                    "ENABLETIMER

And so on...

The properties and the methods work fine.

The problem is with the event.

Inthe Test Program I define:

*       CLASS handlerTick DEFINITION
*----------------------------------------------------------------------*
CLASS handlertick DEFINITION.
  PUBLIC SECTION.
    METHODS handle_tick
    FOR EVENT tick OF zcl_timer.
ENDCLASS.                    "handlerTick DEFINITION


*----------------------------------------------------------------------*
*       CLASS handlerTick IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS handlertick IMPLEMENTATION.
  METHOD handle_tick.
  break tc.
    atime = sy-uzeit.
  ENDMETHOD.                    "handle_Tick
ENDCLASS.                    "handlerTick IMPLEMENTATION

DATA:
  c_handlertick TYPE REF TO handlertick.

And after creating the object c_zcl_timer:

    CALL METHOD c_zcl_timer->set_interval
      EXPORTING
        interval = 5600.
    CALL METHOD c_zcl_timer->enabletimer
      .

    CREATE OBJECT: c_handlertick.
    SET HANDLER c_handlertick->handle_tick FOR ALL INSTANCES.

In C# the event is not thrown, because OnTick == null. Why?

Thank you for your help!

Is there a complete and working example with a C# activex (with properties, methods and events) and a interface class for abap to be free download?

Regards,

Tommaso

2 REPLIES 2

Former Member
0 Kudos

Hi,

Two years has passed by since Tommaso's message, and guess what?

I am stucked with the exactly same problem!

Ideas?

Former Member
0 Kudos

I tried out the solution (in ABAP) that you have posted on the site. However, i keep getting an error which prevents me from moving further. I am a beginner to ABAP, hence it would be kind of you to have a look at the error and guide me further. The error is as follows--

"The type ZCL_TIMER is unknown"