How to Create ActiveX Control


In this article I describe how to create an ActiveX control and how to display it in another screen.

For COM application read

http://www.c-sharpcorner.com/Blogs/7439/creating-com-components-in-net.aspx

Step 1: First create a Class Library project and add a window named CustomerDisplay.
 
Your form's design .cs will look like:

using System.Drawing;

namespace testCustomerDisplay
{
    partial class CustomerDisplay
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
 
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
 
        #region Windows Form 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.Displaypanel = new System.Windows.Forms.Panel();
            this.panel1 = new System.Windows.Forms.Panel();
            this.txtwelcome = new System.Windows.Forms.TextBox();
            this.txtline2Text = new System.Windows.Forms.TextBox();
            this.txtline1Text = new System.Windows.Forms.TextBox();
            this.txtline2Amount = new System.Windows.Forms.TextBox();
            this.txtline1Amount = new System.Windows.Forms.TextBox();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.Displaypanel.SuspendLayout();
            this.panel1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // Displaypanel
            // 
            this.Displaypanel.BackColor = System.Drawing.Color.Black;
            this.Displaypanel.Controls.Add(this.panel1);
            this.Displaypanel.Controls.Add(this.pictureBox1);
            this.Displaypanel.Location = new System.Drawing.Point(66, 12);
            this.Displaypanel.Name = "Displaypanel";
            this.Displaypanel.Size = new System.Drawing.Size(581, 435);
            this.Displaypanel.TabIndex = 0;
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.txtwelcome);
            this.panel1.Controls.Add(this.txtline2Text);
            this.panel1.Controls.Add(this.txtline1Text);
            this.panel1.Controls.Add(this.txtline2Amount);
            this.panel1.Controls.Add(this.txtline1Amount);
            this.panel1.Location = new System.Drawing.Point(23, 12);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(539, 154);
            this.panel1.TabIndex = 5;
            // 
            // txtwelcome
            // 
            this.txtwelcome.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.txtwelcome.Enabled = false;
            this.txtwelcome.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.txtwelcome.Location = new System.Drawing.Point(3, 69);
            this.txtwelcome.Multiline = true;
            this.txtwelcome.Name = "txtwelcome";
            this.txtwelcome.Size = new System.Drawing.Size(520, 61);
            this.txtwelcome.TabIndex = 4;
            this.txtwelcome.Text = "\r\n\r\nWELCOME ";
            this.txtwelcome.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            // 
            // txtline2Text
            // 
            this.txtline2Text.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.txtline2Text.Enabled = false;
            this.txtline2Text.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.txtline2Text.Location = new System.Drawing.Point(3, 39);
            this.txtline2Text.Multiline = true;
            this.txtline2Text.Name = "txtline2Text";
            this.txtline2Text.Size = new System.Drawing.Size(260, 30);
            this.txtline2Text.TabIndex = 2;
            // 
            // txtline1Text
            // 
            this.txtline1Text.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.txtline1Text.Enabled = false;
            this.txtline1Text.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.txtline1Text.Location = new System.Drawing.Point(3, 9);
            this.txtline1Text.Multiline = true;
            this.txtline1Text.Name = "txtline1Text";
            this.txtline1Text.Size = new System.Drawing.Size(260, 30);
            this.txtline1Text.TabIndex = 0;
            // 
            // txtline2Amount
            // 
            this.txtline2Amount.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.txtline2Amount.Enabled = false;
            this.txtline2Amount.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.txtline2Amount.Location = new System.Drawing.Point(263, 39);
            this.txtline2Amount.Multiline = true;
            this.txtline2Amount.Name = "txtline2Amount";
            this.txtline2Amount.Size = new System.Drawing.Size(260, 30);
            this.txtline2Amount.TabIndex = 3;
            this.txtline2Amount.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // txtline1Amount
            // 
            this.txtline1Amount.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.txtline1Amount.Enabled = false;
            this.txtline1Amount.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.txtline1Amount.Location = new System.Drawing.Point(263, 9);
            this.txtline1Amount.Multiline = true;
            this.txtline1Amount.Name = "txtline1Amount";
            this.txtline1Amount.Size = new System.Drawing.Size(260, 30);
            this.txtline1Amount.TabIndex = 1;
            this.txtline1Amount.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // pictureBox1
            // 
            this.pictureBox1.Location = new System.Drawing.Point(0, 0);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(100, 50);
            this.pictureBox1.TabIndex = 6;
            this.pictureBox1.TabStop = false;
            // 
            // CustomerDisplay
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(728, 459);
            this.ControlBox = false;
            this.Controls.Add(this.Displaypanel);
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "CustomerDisplay";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Displaypanel.ResumeLayout(false);
            this.panel1.ResumeLayout(false);
            this.panel1.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.ResumeLayout(false);
 
        }
        #endregion
        private System.Windows.Forms.Panel Displaypanel;
        private System.Windows.Forms.TextBox txtline2Amount;
        private System.Windows.Forms.TextBox txtline2Text;
        private System.Windows.Forms.TextBox txtline1Amount;
        private System.Windows.Forms.TextBox txtline1Text;
        private System.Windows.Forms.PictureBox pictureBox1;
        private System.Windows.Forms.TextBox txtwelcome;
        private System.Windows.Forms.Panel panel1;
    }
}



Step 2: Add one class Connecter and create one interface INAVDisplay.



For Creating COM Visible ActiveX Control I create Interface INAVDisplay, I create this ActiveX Control for MS Dynamic NAV POS System which is Running in 
Dual screen monitor. So MS Dynamic NAV Developer just call this control and pass the value to the Interface. 

In Step 3 you must select the register for COM Interop.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
 
namespace testCustomerDisplay
{
    [Guid("FDE2C8D0-E950-4874-9AE9-516221AFAC31")]
    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    [ComVisible(true)]
    public interface INAVDisplay
    {
        [DispId(1)]
        int ShowOnMonitor { getset; }
        [DispId(2)]
        string Line1_Text { getset; }
        [DispId(3)]
        string Line2_Text { getset; }
        [DispId(4)]
        string Line1_Amount { getset; }
        [DispId(5)]
        string Line2_Amount { getset; }
         [DispId(6)]
        bool Displaywellcome { getset; }
        
        [DispId(7)]
        void showtext();
    }
 
    [Guid("CBAEA8CE-0FA0-4524-97BA-5E204DD791F9")]
    [ClassInterface(ClassInterfaceType.None)]
    [ComVisible(true)]
    public class Connecter : INAVDisplay
    {
        int showOnMonitor;
        bool _displaywellcome = true;
        string _Line1_Text;
        string _Line2_Text;
        string _Line1_Amount;
        string _Line2_Amount;
        public string Line1_Text
        {
            get { return _Line1_Text; }
            set { _Line1_Text = value; }
        }
        public string Line2_Text
        {
            get { return _Line2_Text; }
            set { _Line2_Text = value; }
        }
        public string Line1_Amount
        {
            get { return _Line1_Amount; }
            set { _Line1_Amount = value; }
        }
        public string Line2_Amount
        {
            get { return _Line2_Amount; }
            set { _Line2_Amount = value; }
        }
        public int ShowOnMonitor
        {
            get { return showOnMonitor; }
            set { showOnMonitor = value; }
        }
        public bool Displaywellcome
        {
            get { return _displaywellcome; }
            set { _displaywellcome = value; }
        }
        public Connecter()
        {
        }
        public void showtext()
        {
            try
            {
                bool isMyFormOpen = false;
                foreach (Form f in Application.OpenForms)
                {
                    if (f is CustomerDisplay)
                    {
                        isMyFormOpen = true;
                        DisplayTextInfo(f);
                    }
                }
                if (!isMyFormOpen)
                {
                    CustomerDisplay f = new CustomerDisplay();
 
                    DisplayTextInfo(f);
                  //logic for Display in second screeen.
 
                    Screen[] sc;
                    sc = Screen.AllScreens;
                    if (ShowOnMonitor >= sc.Length)
                    {
                        ShowOnMonitor = 0;
                    }
 
                    f.StartPosition = FormStartPosition.Manual;
                    f.Location = sc[ShowOnMonitor].Bounds.Location;
                    f.WindowState = FormWindowState.Normal;
                    f.WindowState = FormWindowState.Maximized;
                    f.ShowInTaskbar = false;
                    // f.ClientSize = new System.Drawing.Size(0, 0);
                    f.Show();
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("Error: " + Convert.ToString(ex.Message));
            }
        }
 
        private void DisplayTextInfo(Form f)
        {
            //Logic for Display Panel Middle of the Form.
            Panel Displaypanel = (Panel)f.Controls.Find("Displaypanel"true).FirstOrDefault();
            if (Displaypanel !=null)
            {
                Displaypanel.Location = new System.Drawing.Point(f.ClientSize.Width / 2 - Displaypanel.Size.Width / 2, f.ClientSize.Height / 2 - Displaypanel.Size.Height / 2);
                Displaypanel.Anchor = AnchorStyles.None;
            }
 
            TextBox txtwelcome = (TextBox)f.Controls.Find("txtwelcome"true).FirstOrDefault();
            TextBox txtline1Text = (TextBox)f.Controls.Find("txtline1Text"true).FirstOrDefault();
            TextBox txtline2Text = (TextBox)f.Controls.Find("txtline2Text"true).FirstOrDefault();
            TextBox txtline1Amount = (TextBox)f.Controls.Find("txtline1Amount"true).FirstOrDefault();
            TextBox txtline2Amount = (TextBox)f.Controls.Find("txtline2Amount"true).FirstOrDefault();
 
           // if (string.IsNullOrEmpty(Line1_Text)  && string.IsNullOrEmpty(Line2_Text) && string.IsNullOrEmpty(Line1_Amount) && string.IsNullOrEmpty(Line2_Amount))
            if(Displaywellcome)
            {
                txtwelcome.Visible = true;
                txtline1Text.Visible = false;
                txtline2Text.Visible = false;
                txtline1Amount.Visible = false;
                txtline2Amount.Visible = false;
            }
            else
            {
                if (txtline1Text != null)
                {
                    txtline1Text.Text = Line1_Text;
                }
 
                if (txtline2Text != null)
                {
                    txtline2Text.Text = Line2_Text;
                }
 
                if (txtline1Amount != null)
                {
                    txtline1Amount.Text = Line1_Amount;
                }
 
                if (txtline2Amount != null)
                {
                    txtline2Amount.Text = Line2_Amount;
                }
 
                txtwelcome.Visible = false;
                txtline1Text.Visible = true;
                txtline2Text.Visible = true;
                txtline1Amount.Visible = true;
                txtline2Amount.Visible = true;
            }
        }
    }
}

Step 3: Set it to register as COM:

img1.jpg

img2.jpg

Step 4: Now create a Windows application and add your class library ActiveX Control as a reference:

img3.jpg

Step 5: Set your Windows Forms application as the Startup page:

img4.jpg

img5.jpg


Similar Articles