How To Create Dot Net Component Using C#.Net And Windows Controls
Rate this article
0 out of 5
In this tutorial we will show you how to create .net Component using C#.Net and windows controls. After viewing the video tutorial, download the source code and try to modify the code so as to get a feel of what is learned in this video tutorial.


Source Code
Form1.cs

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading; 

namespace CounterThreadExample
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label labelCounter;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItemStart;
private System.Windows.Forms.MenuItem menuItemPause;
private System.Windows.Forms.MenuItem menuItemResume;

public Thread OnlineDisplayThread;
public bool bThreadStarted;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
bThreadStarted=false;
//
// TODO: Add any constructor code after InitializeComponent 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 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.label1 = new System.Windows.Forms.Label();
this.labelCounter = new System.Windows.Forms.Label();
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItemStart = new System.Windows.Forms.MenuItem();
this.menuItemPause = new System.Windows.Forms.MenuItem();
this.menuItemResume = new System.Windows.Forms.MenuItem();
this.SuspendLayout();
// 
// label1
// 
this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label1.Location = new System.Drawing.Point(24, 40);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(64, 23);
this.label1.TabIndex = 0;
this.label1.Text = "Count No";
// 
// labelCounter
// 
this.labelCounter.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.labelCounter.Location = new System.Drawing.Point(120, 40);
this.labelCounter.Name = "labelCounter";
this.labelCounter.Size = new System.Drawing.Size(64, 23);
this.labelCounter.TabIndex = 1;
// 
// mainMenu1
// 
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
 this.menuItem1});
// 
// menuItem1
// 
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
 this.menuItemStart,
 this.menuItemPause,
 this.menuItemResume});
this.menuItem1.Text = "Thread";
// 
// menuItemStart
// 
this.menuItemStart.Index = 0;
this.menuItemStart.Text = "Start";
this.menuItemStart.Click += new System.EventHandler(this.menuItemStart_Click);
// 
// menuItemPause
// 
this.menuItemPause.Index = 1;
this.menuItemPause.Text = "Pause";
this.menuItemPause.Click += new System.EventHandler(this.menuItemPause_Click);
// 
// menuItemResume
// 
this.menuItemResume.Index = 2;
this.menuItemResume.Text = "Resume";
this.menuItemResume.Click += new System.EventHandler(this.menuItemResume_Click);
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 189);
this.Controls.Add(this.labelCounter);
this.Controls.Add(this.label1);
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.Closed += new System.EventHandler(this.Form1_Closed);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main() 
{
Application.Run(new Form1());
}

private void menuItemStart_Click(object sender, System.EventArgs e)
{
OnlineDisplayThread=new Thread(new ThreadStart(ThreadCounter));
OnlineDisplayThread.Start();
bThreadStarted=true;
}

private void menuItemPause_Click(object sender, System.EventArgs e)
{
OnlineDisplayThread.Suspend(); 
}


private void menuItemResume_Click(object sender, System.EventArgs e)
{
OnlineDisplayThread.Resume();
}

private void Form1_Load(object sender, System.EventArgs e)
{
}

public void ThreadCounter()
{
long iCounter=0;
try
{
while(true)
{
iCounter++;
labelCounter.Text=iCounter.ToString();  
Thread.Sleep(1000);
}
}
catch(Exception ex)
{
}
}

private void Form1_Closed(object sender, System.EventArgs e)
{
try
{
if(bThreadStarted==true)
{
 
OnlineDisplayThread.Abort(); 
}
}
catch(Exception  Ex)
{
 
}
 
}
}
}

Assemblyinfo.cs

using System.Reflection;
using System.Runtime.CompilerServices;

//
// General Information about an assembly is controlled through the following 
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
//
[assembly: AssemblyTitle("")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("KTS")]
[assembly: AssemblyProduct("")]
[assembly: AssemblyCopyright("")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

//
// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version 
//      Build Number
//      Revision
//
// You can specify all the values or you can default the Revision and Build Numbers 
// by using the '*' as shown below:

[assembly: AssemblyVersion("1.0.*")]

//
// In order to sign your assembly you must specify a key to use. Refer to the 
// Microsoft .NET Framework documentation for more information on assembly signing.
//
// Use the attributes below to control which key is used for signing. 
//
// Notes: 
//   (*) If no key is specified, the assembly is not signed.
//   (*) KeyName refers to a key that has been installed in the Crypto Service
//       Provider (CSP) on your machine. KeyFile refers to a file which contains
//       a key.
//   (*) If the KeyFile and the KeyName values are both specified, the 
//       following processing occurs:
//       (1) If the KeyName can be found in the CSP, that key is used.
//       (2) If the KeyName does not exist and the KeyFile does exist, the key 
//           in the KeyFile is installed into the CSP and used.
//   (*) In order to create a KeyFile, you can use the sn.exe (Strong Name) utility.
//       When specifying the KeyFile, the location of the KeyFile should be
//       relative to the project output directory which is
//       %Project Directory%obj<configuration>. For example, if your KeyFile is
//       located in the project directory, you would specify the AssemblyKeyFile 
//       attribute as [assembly: AssemblyKeyFile("....mykey.snk")]
//   (*) Delay Signing is an advanced option - see the Microsoft .NET Framework
//       documentation for more information on this.
//
[assembly: AssemblyDelaySign(false)]
[assembly: AssemblyKeyFile("")]
[assembly: AssemblyKeyName("")]



Source Code

About The Author
KTS Training

KTS InfoTech Training division offers various short-term programming courses for Students and Professionals who are looking for a bright career in the field of Software development.

The programming video tutorials presented in this web site are taken from some of the training sessions conducted by KTS InfoTech.

You can also learn the Programming packages through Internet directly form our English Speaking Technical experts at an affordable rate.

For more details click here and submit your training requirements.



Number Of articles Posted
60
Number Of Messages Posted
3
Joined Date
14/11/2012
   
Author
Messages
Posted:
Post Your Comments
Name (Max 50 Chars)
Comments

 
TekTipsDownload
GateExam
Academic Projects
TekTipsExperts
Android