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
Code
Step 1: First create a Class Library project and add a window named CustomerDisplay.Code
Code
Your form's design .cs will look like:Code
using System.Drawing;Code
namespace testCustomerDisplayCode
{Code
partial class CustomerDisplayCode
{Code
/// <summary>Code
/// Required designer variable.Code
/// </summary>Code
private System.ComponentModel.IContainer components = null;Code
Code
/// <summary>Code
/// Clean up any resources being used.Code
/// </summary>Code
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>Code
protected override void Dispose(bool disposing)Code
{Code
if (disposing && (components != null))Code
{Code
components.Dispose();Code
}Code
base.Dispose(disposing);Code
}Code
Code
#region Windows Form Designer generated codeCode
Code
/// <summary>Code
/// Required method for Designer support - do not modifyCode
/// the contents of this method with the code editor.Code
/// </summary>Code
private void InitializeComponent()Code
{Code
this.Displaypanel = new System.Windows.Forms.Panel();Code
this.panel1 = new System.Windows.Forms.Panel();Code
this.txtwelcome = new System.Windows.Forms.TextBox();Code
this.txtline2Text = new System.Windows.Forms.TextBox();Code
this.txtline1Text = new System.Windows.Forms.TextBox();Code
this.txtline2Amount = new System.Windows.Forms.TextBox();Code
this.txtline1Amount = new System.Windows.Forms.TextBox();Code
this.pictureBox1 = new System.Windows.Forms.PictureBox();Code
this.Displaypanel.SuspendLayout();Code
this.panel1.SuspendLayout();Code
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();Code
this.SuspendLayout();Code
//Code
// DisplaypanelCode
//Code
this.Displaypanel.BackColor = System.Drawing.Color.Black;Code
this.Displaypanel.Controls.Add(this.panel1);Code
this.Displaypanel.Controls.Add(this.pictureBox1);Code
this.Displaypanel.Location = new System.Drawing.Point(66, 12);Code
this.Displaypanel.Name = "Displaypanel";Code
this.Displaypanel.Size = new System.Drawing.Size(581, 435);Code
this.Displaypanel.TabIndex = 0;Code
//Code
// panel1Code
//Code
this.panel1.Controls.Add(this.txtwelcome);Code
this.panel1.Controls.Add(this.txtline2Text);Code
this.panel1.Controls.Add(this.txtline1Text);Code
this.panel1.Controls.Add(this.txtline2Amount);Code
this.panel1.Controls.Add(this.txtline1Amount);Code
this.panel1.Location = new System.Drawing.Point(23, 12);Code
this.panel1.Name = "panel1";Code
this.panel1.Size = new System.Drawing.Size(539, 154);Code
this.panel1.TabIndex = 5;Code
//Code
// txtwelcomeCode
//Code
this.txtwelcome.BorderStyle = System.Windows.Forms.BorderStyle.None;Code
this.txtwelcome.Enabled = false;Code
this.txtwelcome.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));Code
this.txtwelcome.Location = new System.Drawing.Point(3, 69);Code
this.txtwelcome.Multiline = true;Code
this.txtwelcome.Name = "txtwelcome";Code
this.txtwelcome.Size = new System.Drawing.Size(520, 61);Code
this.txtwelcome.TabIndex = 4;Code
this.txtwelcome.Text = "\r\n\r\nWELCOME ";Code
this.txtwelcome.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;Code
//Code
// txtline2TextCode
//Code
this.txtline2Text.BorderStyle = System.Windows.Forms.BorderStyle.None;Code
this.txtline2Text.Enabled = false;Code
this.txtline2Text.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));Code
this.txtline2Text.Location = new System.Drawing.Point(3, 39);Code
this.txtline2Text.Multiline = true;Code
this.txtline2Text.Name = "txtline2Text";Code
this.txtline2Text.Size = new System.Drawing.Size(260, 30);Code
this.txtline2Text.TabIndex = 2;Code
//Code
// txtline1TextCode
//Code
this.txtline1Text.BorderStyle = System.Windows.Forms.BorderStyle.None;Code
this.txtline1Text.Enabled = false;Code
this.txtline1Text.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));Code
this.txtline1Text.Location = new System.Drawing.Point(3, 9);Code
this.txtline1Text.Multiline = true;Code
this.txtline1Text.Name = "txtline1Text";Code
this.txtline1Text.Size = new System.Drawing.Size(260, 30);Code
this.txtline1Text.TabIndex = 0;Code
//Code
// txtline2AmountCode
//Code
this.txtline2Amount.BorderStyle = System.Windows.Forms.BorderStyle.None;Code
this.txtline2Amount.Enabled = false;Code
this.txtline2Amount.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));Code
this.txtline2Amount.Location = new System.Drawing.Point(263, 39);Code
this.txtline2Amount.Multiline = true;Code
this.txtline2Amount.Name = "txtline2Amount";Code
this.txtline2Amount.Size = new System.Drawing.Size(260, 30);Code
this.txtline2Amount.TabIndex = 3;Code
this.txtline2Amount.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;Code
//Code
// txtline1AmountCode
//Code
this.txtline1Amount.BorderStyle = System.Windows.Forms.BorderStyle.None;Code
this.txtline1Amount.Enabled = false;Code
this.txtline1Amount.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));Code
this.txtline1Amount.Location = new System.Drawing.Point(263, 9);Code
this.txtline1Amount.Multiline = true;Code
this.txtline1Amount.Name = "txtline1Amount";Code
this.txtline1Amount.Size = new System.Drawing.Size(260, 30);Code
this.txtline1Amount.TabIndex = 1;Code
this.txtline1Amount.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;Code
//Code
// pictureBox1Code
//Code
this.pictureBox1.Location = new System.Drawing.Point(0, 0);Code
this.pictureBox1.Name = "pictureBox1";Code
this.pictureBox1.Size = new System.Drawing.Size(100, 50);Code
this.pictureBox1.TabIndex = 6;Code
this.pictureBox1.TabStop = false;Code
//Code
// CustomerDisplayCode
//Code
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);Code
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;Code
this.ClientSize = new System.Drawing.Size(728, 459);Code
this.ControlBox = false;Code
this.Controls.Add(this.Displaypanel);Code
this.MaximizeBox = false;Code
this.MinimizeBox = false;Code
this.Name = "CustomerDisplay";Code
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;Code
this.Displaypanel.ResumeLayout(false);Code
this.panel1.ResumeLayout(false);Code
this.panel1.PerformLayout();Code
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();Code
this.ResumeLayout(false);Code
Code
}Code
#endregionCode
private System.Windows.Forms.Panel Displaypanel;Code
private System.Windows.Forms.TextBox txtline2Amount;Code
private System.Windows.Forms.TextBox txtline2Text;Code
private System.Windows.Forms.TextBox txtline1Amount;Code
private System.Windows.Forms.TextBox txtline1Text;Code
private System.Windows.Forms.PictureBox pictureBox1;Code
private System.Windows.Forms.TextBox txtwelcome;Code
private System.Windows.Forms.Panel panel1;Code
}Code
}Code
Code
Step 2: Add one class Connecter and create one interface INAVDisplay.Code
SQL
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.Code
In Step 3 you must select the register for COM Interop.Code
using System;Code
using System.Collections.Generic;Code
using System.Linq;Code
using System.Text;Code
using System.Runtime.InteropServices;Code
using System.Windows.Forms;Code
Code
namespace testCustomerDisplayCode
{Code
[Guid("FDE2C8D0-E950-4874-9AE9-516221AFAC31")]Code
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]Code
[ComVisible(true)]Code
public interface INAVDisplayCode
{Code
[DispId(1)]Code
int ShowOnMonitor { get; set; }Code
[DispId(2)]Code
string Line1_Text { get; set; }Code
[DispId(3)]Code
string Line2_Text { get; set; }Code
[DispId(4)]Code
string Line1_Amount { get; set; }Code
[DispId(5)]Code
string Line2_Amount { get; set; }Code
[DispId(6)]Code
bool Displaywellcome { get; set; }Code
Code
[DispId(7)]Code
void showtext();Code
}Code
Code
[Guid("CBAEA8CE-0FA0-4524-97BA-5E204DD791F9")]Code
[ClassInterface(ClassInterfaceType.None)]Code
[ComVisible(true)]Code
public class Connecter : INAVDisplayCode
{Code
int showOnMonitor;Code
bool _displaywellcome = true;Code
string _Line1_Text;Code
string _Line2_Text;Code
string _Line1_Amount;Code
string _Line2_Amount;Code
public string Line1_TextCode
{Code
get { return _Line1_Text; }Code
set { _Line1_Text = value; }Code
}Code
public string Line2_TextCode
{Code
get { return _Line2_Text; }Code
set { _Line2_Text = value; }Code
}Code
public string Line1_AmountCode
{Code
get { return _Line1_Amount; }Code
set { _Line1_Amount = value; }Code
}Code
public string Line2_AmountCode
{Code
get { return _Line2_Amount; }Code
set { _Line2_Amount = value; }Code
}Code
public int ShowOnMonitorCode
{Code
get { return showOnMonitor; }Code
set { showOnMonitor = value; }Code
}Code
public bool DisplaywellcomeCode
{Code
get { return _displaywellcome; }Code
set { _displaywellcome = value; }Code
}Code
public Connecter()Code
{Code
}Code
public void showtext()Code
{Code
tryCode
{Code
bool isMyFormOpen = false;Code
foreach (Form f in Application.OpenForms)Code
{Code
if (f is CustomerDisplay)Code
{Code
isMyFormOpen = true;Code
DisplayTextInfo(f);Code
}Code
}Code
if (!isMyFormOpen)Code
{Code
CustomerDisplay f = new CustomerDisplay();Code
Code
DisplayTextInfo(f);Code
//logic for Display in second screeen.Code
Code
Screen[] sc;Code
sc = Screen.AllScreens;Code
if (ShowOnMonitor >= sc.Length)Code
{Code
ShowOnMonitor = 0;Code
}Code
Code
f.StartPosition = FormStartPosition.Manual;Code
f.Location = sc[ShowOnMonitor].Bounds.Location;Code
f.WindowState = FormWindowState.Normal;Code
f.WindowState = FormWindowState.Maximized;Code
f.ShowInTaskbar = false;Code
// f.ClientSize = new System.Drawing.Size(0, 0);Code
f.Show();Code
}Code
}Code
catch(Exception ex)Code
{Code
MessageBox.Show("Error: " + Convert.ToString(ex.Message));Code
}Code
}Code
Code
private void DisplayTextInfo(Form f)Code
{Code
//Logic for Display Panel Middle of the Form.Code
Panel Displaypanel = (Panel)f.Controls.Find("Displaypanel", true).FirstOrDefault();Code
if (Displaypanel !=null)Code
{Code
Displaypanel.Location = new System.Drawing.Point(f.ClientSize.Width / 2 - Displaypanel.Size.Width / 2, f.ClientSize.Height / 2 - Displaypanel.Size.Height / 2);Code
Displaypanel.Anchor = AnchorStyles.None;Code
}Code
Code
TextBox txtwelcome = (TextBox)f.Controls.Find("txtwelcome", true).FirstOrDefault();Code
TextBox txtline1Text = (TextBox)f.Controls.Find("txtline1Text", true).FirstOrDefault();Code
TextBox txtline2Text = (TextBox)f.Controls.Find("txtline2Text", true).FirstOrDefault();Code
TextBox txtline1Amount = (TextBox)f.Controls.Find("txtline1Amount", true).FirstOrDefault();Code
TextBox txtline2Amount = (TextBox)f.Controls.Find("txtline2Amount", true).FirstOrDefault();Code
Code
// if (string.IsNullOrEmpty(Line1_Text) && string.IsNullOrEmpty(Line2_Text) && string.IsNullOrEmpty(Line1_Amount) && string.IsNullOrEmpty(Line2_Amount))Code
if(Displaywellcome)Code
{Code
txtwelcome.Visible = true;Code
txtline1Text.Visible = false;Code
txtline2Text.Visible = false;Code
txtline1Amount.Visible = false;Code
txtline2Amount.Visible = false;Code
}Code
elseCode
{Code
if (txtline1Text != null)Code
{Code
txtline1Text.Text = Line1_Text;Code
}Code
Code
if (txtline2Text != null)Code
{Code
txtline2Text.Text = Line2_Text;Code
}Code
Code
if (txtline1Amount != null)Code
{Code
txtline1Amount.Text = Line1_Amount;Code
}Code
Code
if (txtline2Amount != null)Code
{Code
txtline2Amount.Text = Line2_Amount;Code
}Code
Code
txtwelcome.Visible = false;Code
txtline1Text.Visible = true;Code
txtline2Text.Visible = true;Code
txtline1Amount.Visible = true;Code
txtline2Amount.Visible = true;Code
}Code
}Code
}Code
}Step 3: Set it to register as COM:


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

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



GridLock ThePosted Sep 7, 2019, 12:11 PM EDT
How to create activeX control for excel like combobox or listbox ?
Arjun PanwarPosted Apr 8, 2012, 7:49 PM EDT
Thanks for sharing this useful article.
Sam HobbseditedPosted Apr 8, 2012, 9:15 AM EDTEdited Apr 8, 2012, 9:16 AM EDT
Thank you for this very useful article. I do not however understand what INAVDisplay is. Is that necessary for all ActiveX controls or just this one? For an article such as this, it helps to explain more. Also, the image in Step 3 is not clear; it would help to provide the text of what to do instead of just showing the image. Also, what is the purpose of creating an ActiveX control; is it to create something using managed code that can be used by unmanaged code?