Blue Theme Orange Theme Green Theme Red Theme
 
Nevron Chart
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Discover the top 5 tips for understanding .NET Interop
Search :       Advanced Search »
Home » Windows Forms C# » Developing Windows Applications

Developing Windows Applications

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

Author Rank :
Page Views : 146099
Downloads : 1239
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
mcWinFormApp.zip
 
 
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
DevExpress Free UI Controls
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

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.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Mahesh Chand
Mahesh is the founder of C# Corner and Mindcracker Network, an author of several .NET programming books and a Microsoft MVP for 6 consecutive years. In his day to day work, Mahesh is a Senior Software Consultant with over 14 years of IT industry 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 Sharepoint, 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.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
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.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
 Comments
upload a file by hameed On February 20, 2007
how to upload a file
Reply | Email | 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 | Modify 
how to upload by dkmahesh On April 3, 2007
how to upload Where the file uploads
Reply | Email | 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 | 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 | Modify 
code for cancel button by umer On December 16, 2007
please tell me about the code for cancel button
Reply | Email | Modify 
Re: code for cancel button by triveni On November 8, 2008
this.Close();
it will work on cacel button.
Reply | Email | Modify 
c#.net by sampath On March 10, 2008
it's good
Reply | Email | Modify 
windows by ram On February 19, 2009
its very nice becoz he explain eaisly before whose dont this
Reply | Email | Modify 
thanx :) by praveen On March 21, 2009
Thnax a lot sir ... :)
Reply | Email | 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 | 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 | Modify 
File Browser by krishna On December 7, 2009

Good

Reply | Email | Modify 
win forms by seenu On December 17, 2009
it is good for beginners please sendme windows forms  seenukonga52@gmail.com
Reply | Email | 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 | 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 | 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 | Modify 
C# by Devasubha On March 25, 2010

if you click button the result will stored in text box

Reply | Email | Modify 
want a project by rashid On June 28, 2010
want a project of "HOSPITAL MANAGMENT" using sql server in ado.net,i just want to see the best front end forms for design..its urgent.tnx
Reply | Email | Modify 
good by Mahesh On August 25, 2010
very nice to use this
Reply | Email | Modify 
Team Foundation Server Hosting
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.