Building Windows Applcation using C# Author Date Of Submission User Level Mahesh Chand 07/23/2001 Beginner Tools Used Project Download Visual C# .NET Final Release mcWinFormApp.zip 20 KB 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); this.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. Mahesh Chand is a .NET consultant, author and the admin and founder of C# Corner. He has been working with .NET technology since pre beta releases. Mahesh's background includes Master's in Computer Science and Applications and B.Sc. Mathematics. He is also a Microsoft Certified Professional. Publication: Mahesh's publication includes the following titles Graphics Programming with GDI+ "This is the most comprehensive book about graphics programming using GDI+ so far. This book will be a very useful handbook for everyone who does graphics programming for Windows." --Min Liu, Software Design Engineer of GDI+, Microsoft Corporation Graphics Programming with GDI+ is the .NET developer's guide to writing graphics applications for Windows and the Web. Through the use of detailed examples it provides experienced programmers with a deep understanding of the entire GDI+ API defined in the .NET Framework class library. Applied ADO.NET: Building Data-Driven Solutions User level: Intermediate This book is written for intermediate to advanced developers who want to design database based Windows and Web solutions Applied ADO.NET: Building Data-Driven Solutions provides extensive coverage of ADO.NET technology including ADO.NET internals, namespaces, classes, and interfaces. Where most books cover only SQL and OLE DB data providers, Mahesh Chand and David Talbot cover SQL, OLE DB, ODBC data providers and the latest additions to ADO.NET: Oracle, MySQL, and XML .NET data providers. Chand and Talbot also cover internals of data binding and they provide detailed coverage on both Windows Forms and Web Forms data binding and data-bound controls. Since XML plays a major role in .NET development, the authors also provide a comprehensive look at XML namespaces and classes, and how to integrate both with ADO.NET. The Complete Guide for Visual C# Programmers User level: Intermediate This book covers major components that make up C# and the .NET environment. The book is geared toward the intermediate programmer, but contains enough material to satisfy the advanced developer. The book starts by introducing the reader to the .NET environment and some basic C# program examples. The book then goes in depth on how to navigate the Visual Studio environment and introduces the reader to the C# language From here the book dives into the .NET framework and how to take advantage of its capabilities using C#. Some of the topics covered in great detail include ADO.NET database programming, ASP.NET and Web Services, COM interoperability programming, security in .NET, mobile phone programming, GDI+ graphic programming, multithreading, Windows Services, XML and SOAP in .NET, and much more. Chapters in the book contain programming examples, reference tables, and step-by-step tutorials to guide you through one of Microsoft’s greatest achievements in programming environments. A Programmer's Guide to ADO .NET in C# User level: Beginner This book is written for beginners who want to write database applications using ADO.NET and C#. This is the book on ADO.NET. ADO.NET is the latest database technology from Microsoft and represents the most powerful way to manipulate a database to date. A Programmer's Guide to ADO.NET in C# begins by taking readers through an overview of C# and then delves into ADO.NET. Author Mahesh Chand provides details on each of the major data providers of the .NET platform, such as OleDb, Sql, and ODBC, which enable developers to read and write data to the targeted databases. This book also serves as a good reference for finding detailed methods and properties for these data provider classes.