Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Photos | Blogs | E-Books | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article 
 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:
Technologies: .NET 1.0/1.1, Windows Forms,Visual C# .NET
Total downloads : 376
Total page views :  61179
Rating :
 4.67/5
This article has been rated :  3 times
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
mcWinFormApp.zip
 
ArticleAd
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
 [Top] Rate this article
 About the author
 
Mahesh Chand
Mahesh is a software consultant, architect, author, MCP, MVP, and founder of C# Corner. He has over 13 years of experience building systems for Financial and Banking, Engineering & Architectural, Imaging, Construction, Biological & Pharmaceuticals, Healthcare and Education industries including Microsoft, Unisys, Barclay’s, Centocor (J&J), McGraw-Hill, Excelon, PMI, and University of Pennsylvania Hospital. Since year 2000, he is been working with, ASP.NET, SQL Server, C# and .NET. His latest experience and interest is Silverlight, WPF, WCF, XAML and .NET 3.5. If you need any consulting in ASP.NET, AJAX, WPF, WCF, or XAML, contact him 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.
Boost the performance of your .NET applications
“ANTS Profiler took us straight to the specific areas of our code which were the cause of our performance issues." Terry Phillips, Sr. Developer, Harley-Davidson Dealer Systems. Download your free trial of ANTS Profiler.
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.
 
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
mcWinFormApp.zip
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
ArticleAd
Become a Sponsor
Latest Comments:
Subject Posted By Posted On
upload a filehameed2/20/2007
how to upload a file
Reply | Email | Delete | Modify | 
 
 
Re: upload a fileMahesh2/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 uploaddkmahesh4/3/2007
how to upload Where the file uploads
Reply | Email | Delete | Modify | 
Error OpenFileDialogmeenoo4/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 goodsri11/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 umer12/16/2007
please tell me about the code for cancel button
Reply | Email | Delete | Modify | 
 
 
Re: code for cancel button triveni11/8/2008
this.Close();
it will work on cacel button.
Reply | Email | Delete | Modify | 
c#.netsampath3/10/2008
it's good
Reply | Email | Delete | Modify | 
windowsram2/19/2009
its very nice becoz he explain eaisly before whose dont this
Reply | Email | Delete | Modify | 
thanx :)praveen3/21/2009
Thnax a lot sir ... :)
Reply | Email | Delete | Modify | 
combo boxnarendra3/30/2009
how to fetch the table coloumn in a database table in to combo box
Reply | Email | Delete | Modify | 
 
 
Re: combo boxMahesh4/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 | 

 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
 © 1999 - 2009  Mindcracker LLC. All Rights Reserved