Blue Theme Orange Theme Green Theme Red Theme
 
World Class ASP.NET Hosting – Click Here for 3 Months Free/NO Setup Fee!
Home | Forums | Videos | Photos | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » Tutorials » Developing Windows Applications

Developing Windows Applications

This tutorial explains you step by step how to create your Windows Applications using Visual C#.

Author Rank:
Total page views :  82532
Total downloads :  593
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
mcWinFormApp.zip
 
Become a Sponsor

Ok, here I am with one more beginners tutorial. This time the pick is Windows (GUI) Application. Creating a GUI application using Visual C# is way easier than that in VC++ previous versions. This tutorial guides you to create your first Windows Application using the Visual C# Project Wizard.

Creating Skeleton of the Application

Select New->Project->Visual C# Projects->Windows Application from your VS .NET IDE and type your application name. I use mcWinFormApp. 


 
And hit OK. The IDE will take you to the design view of a form ( similar to VB or Delphi ). In right side, you will see the Solution Explorer. The Wizard adds one cs file for new from called Form1.cs. This class has code for the form and all of its child windows.

 

When you double click (or user View Code menu option on the right click) on the Form1.cs, you see this code:

/// Required designer variable.
/// </summary>

private System.ComponentModel.Container components = null
;
public
Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// 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.components = new
System.ComponentModel.Container();
this.Size = new
System.Drawing.Size(300,300);
this
.Text = "Form1";
}
#endregion

/// <summary>

///
The main entry point for the application.
/// </summary>

[STAThread]
static void
Main()
{
Application.Run(
new
Form1());
}
}
}  

This wizards adds a default namespace and adds reference to various namespaces require by WinForms. The Form1 class is derived from System.WinForms.Form. The InitializeComponent method initializes (creates) Form and its controls. You will see it in more details when you will drop some controls on the form. The Dispose method cleans up all the resource which are not being used anymore.

Adding Controls

To add controls ( Child Windows ) to a form, you need to open ToolBox ( Controls in VC++ previous versions ). This toolbox concepts came from VB. You can open ToolBox by clicking View->ToolBox menu item.

The ToolBox window looks like the below picture. Now you add controls similar to previous version of Visual Studio either by drag-drop or by double clicking on the control.

Now I am going to drop few controls and let's see what Designer adds to InitializeComponent. I drop a button and an edit box on the form and change Button property "Text" to Browse.

 
You use Properties Window to set properties of controls. This is similar to VB. You call Properties Windows by right clicking on the control and clicking Properties menu item.

The Properties window for a button control looks like following. As you can see from the following figure, I change Text property of button control to "Browse".

The Now if you see the InitializeComponent method you will see this code has been added to the method. You can even modify this code by your hand.

#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.button1 = new
System.Windows.Forms.Button();
this.textBox1 = new
System.Windows.Forms.TextBox();
this
.SuspendLayout();
//
// button1
//
this.button1.Location = new
System.Drawing.Point(8, 24);
this
.button1.Name = "button1";
this.button1.Size = new
System.Drawing.Size(88, 24);
his
.button1.TabIndex = 0;
this
.button1.Text = "Browse";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(128, 24);
this
.textBox1.Name = "textBox1";
this.textBox1.Size = new
System.Drawing.Size(136, 20);
this
.textBox1.TabIndex = 1;
this
.textBox1.Text = "textBox1";
//
// Form1
/
this.AutoScaleBaseSize = new
System.Drawing.Size(5, 13);
this.ClientSize = new
System.Drawing.Size(292, 273);
this.Controls.AddRange(new
System.Windows.Forms.Control[] { this.textBox1, this.button1});
this
.Name = "Form1";
this
.Text = "Form1";
this.ResumeLayout(false
);
}
#endregion
  

Adding Event Handler

The last part of this tutorial is to add an event handler for the button and browse for a file in the text box. You double click on the button to add an event handler for the button. Button1_Click is the event handler for button. In same way you can write event handler for any control.

private void button1_Click(object sender, System.EventArgs e)
{
OpenFileDialog fdlg =
new
OpenFileDialog();
fdlg.Title = "C# Corner Open File Dialog" ;
fdlg.InitialDirectory = @"c:\" ;
fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*" ;
fdlg.FilterIndex = 2 ;
fdlg.RestoreDirectory =
true
;
if
(fdlg.ShowDialog() == DialogResult.OK)
{
textBox1.Text = fdlg.FileName ;
}
}
 

Now you run the program and click the Browse button. It calls Open File Dialog, which lets you browse for a file. I select a file.

And click Open. Action adds file name to the text box and application looks like following figure.


Login to add your contents and source code to this article
 About the author
 
Mahesh Chand
Mahesh is a software developer with over 13 years of experience building systems for Financial and Banking, Engineering & Architectural, Imaging, Construction, Biological & Pharmaceuticals, Healthcare and Education industries. His expertise is Windows Forms, ASP.NET, Silverlight, WPF, WCF, Visual Studio 2010, SQL Server, and Oracle. If you are looking for a Windows Forms, ASP.NET, WPF, Silverlight, C#, VB.NET, Oracle, and SQL Server Consultant in Philadelphia area or remote location, drop me a line at MAHESH [AT] C-SHARPCORNER [DOT] COM.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010 Professional
Microsoft Visual Studio 2010 Professional will launch on April 12, but you can beat the rush and secure your copy today by pre-ordering at the affordable estimated retail price of $549 (US). Pre-order now.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Developer-Ready ASP.NET 2.0 Web Hosting with 3 MONTHS FREE
Now supporting .NET 3.0 Framework with Windows Workflow Foundation, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), windows CardSpace (WCS)! Providing more flexibility for Developers with Web Services Support and a User/Permission Manger. Also supporting MS SQL 2005/2000 with Real-Time Backups, FREE Automated Attach .MDF Tool, FREE SQL Restore and Shrink SQL DB Tools, and SQL
 
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
mcWinFormApp.zip
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
Become a Sponsor
 Comments
upload a file by hameed On February 20, 2007
how to upload a file
Reply | Email | Delete | Modify | 
Re: upload a file by Mahesh On February 20, 2007
Are you trying to upload a file on a local machine from a Windows Forms application or upload a file on a Web site? If you want to upload a file in a Windows forms applicaiton, you will have to use File.Copy method. If you want to upload a file on a Web Server, you will have to use HTTP or FTP classes. See Internet and Web section for samples.
Reply | Email | Delete | Modify | 
how to upload by dkmahesh On April 3, 2007
how to upload Where the file uploads
Reply | Email | Delete | Modify | 
Error OpenFileDialog by meenoo On April 17, 2007
When i try to build this code it gives following errors: Error 1 'System.Windows.Forms.OpenFileDialog' does not contain a definition for 'Title' Error 2 'System.Windows.Forms.OpenFileDialog' does not contain a definition for 'RestoreDirectory' could you please help me in resolving this? Thanks
Reply | Email | Delete | Modify | 
was good by sri On November 12, 2007
thank u very much... its really helpful like a beginner like me.... would like more of these kind thank you.
Reply | Email | Delete | Modify | 
code for cancel button by umer On December 16, 2007
please tell me about the code for cancel button
Reply | Email | Delete | Modify | 
Re: code for cancel button by triveni On November 8, 2008
this.Close();
it will work on cacel button.
Reply | Email | Delete | Modify | 
c#.net by sampath On March 10, 2008
it's good
Reply | Email | Delete | Modify | 
windows by ram On February 19, 2009
its very nice becoz he explain eaisly before whose dont this
Reply | Email | Delete | Modify | 
thanx :) by praveen On March 21, 2009
Thnax a lot sir ... :)
Reply | Email | Delete | Modify | 
combo box by narendra On March 30, 2009
how to fetch the table coloumn in a database table in to combo box
Reply | Email | Delete | Modify | 
Re: combo box by Mahesh On April 2, 2009
Please post your questions on forums by clicking on forums link on the header. These comments are article related only.
Reply | Email | Delete | Modify | 
File Browser by krishna On December 7, 2009

Good

Reply | Email | Delete | Modify | 
win forms by seenu On December 17, 2009
it is good for beginners please sendme windows forms  seenukonga52@gmail.com
Reply | Email | Delete | Modify | 
Re: win forms by Mahesh On December 20, 2009
On C# Corner home page, click on Windows Forms link in the left side categories. All articles I published are there.
Reply | Email | Delete | Modify | 
Re: Re: win forms by Amit On February 11, 2010
Hii Mahesh,

Can u send me some examples of WCF , WPF
.
Thank You
Reply | Email | Delete | Modify | 
Require more examples by Kranthi Kumar On February 23, 2010
hi all ur example are very good i need some more examples  on differnt topics so that i can improve myself and learn.

hoping a positive response

Reply | Email | Delete | Modify | 

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 2010  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.