<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-343356482335293710</id><updated>2011-11-27T15:14:12.939-08:00</updated><category term='Winform'/><title type='text'>Winform Tutorial</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://winformtutorial.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/343356482335293710/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://winformtutorial.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>sandy</name><uri>http://www.blogger.com/profile/02503582209971047801</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>5</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-343356482335293710.post-323459587022993956</id><published>2008-08-25T02:37:00.001-07:00</published><updated>2008-12-22T21:26:18.849-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Winform'/><title type='text'>Serialization</title><content type='html'>&lt;p style="margin: 0pt;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Before we begin with main program let us now see how to attach a menu to a blank form. Carry out the following steps:&lt;/span&gt;&lt;/p&gt; &lt;ol type="a"&gt;&lt;li&gt;     &lt;p style="margin: 0pt;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Drag in the 'MainMenu'     control from the toolbox and release it on the blank form.&lt;/span&gt;   &lt;/p&gt;&lt;/li&gt;&lt;li&gt;     &lt;p style="margin: 0pt;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Click on the control and     type the menu item name as 'File'. &lt;/span&gt;   &lt;/p&gt;&lt;/li&gt;&lt;li&gt;     &lt;p style="margin: 0pt;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Add the items 'New',     'Open', 'Save', 'Generate' and 'Exit' to the 'File' menu in a similar     manner.&lt;/span&gt; &lt;/p&gt;&lt;/li&gt;&lt;/ol&gt; &lt;p style="margin: 0pt;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;The resultant form is shown in the following figure.&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;" align="center"&gt;&lt;img src="http://www.kicit.com/freebies/csharp_net_source_code/WinForms/winfor11.jpg" width="311" border="0" height="275" /&gt;&lt;/p&gt; &lt;p style="margin: 0pt;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Each menu item is governed by a control variable of the type &lt;b&gt;MenuItem&lt;/b&gt;. We can change the names of these variables through the 'Properties' window. For the program that follows we have used the names as &lt;b&gt;filemenu&lt;/b&gt; that will hold the other menu items, &lt;b&gt;newmenu&lt;/b&gt;, &lt;b&gt;openmenu&lt;/b&gt;, &lt;b&gt;savemenu&lt;/b&gt;, &lt;b&gt;generatemenu&lt;/b&gt; and &lt;b&gt;exitmenu&lt;/b&gt;.&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;"&gt; &lt;/p&gt; &lt;p style="margin: 0pt;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Let us now decide what should happen when these menu items are selected. When we select the �Generate� menu item, shapes like rectangle, ellipse and line should get generated at random and in random colors. On selecting the �Save� menu item these shapes should be saved in a file. We must be able to load this file and display the shapes again. This would be achieved through the �Open� menu item. When we click the �New� menu item the earlier shapes should vanish and we must get a new form to draw new shapes. &lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;"&gt; &lt;/p&gt; &lt;p style="margin: 0pt;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;While saving the shapes we should not save the image of the shape. Instead we should save the relevant information of the shape using which we should be able to regenerate the shape again when the file is loaded. This means we must write the object onto the disk while saving it and load it back while opening the file. This process of writing the state of an object is called serialization and reading it back is called deserialization.&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;"&gt; &lt;/p&gt; &lt;p style="margin: 0pt;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;To make all these activities to happen we need to add handlers for these menu items. The Windows Forms programming model is event based. When we click on a menu item it raises an event. In order to handle an event, our application should register an event-handling method. For adding these handlers click on the menu item for which we wish to add the handler. Then go to the 'Properties' window and select the 'Events' tab (shown by a yellow lightening icon). From the list of events select 'Click'. As a result an event handler called say, &lt;b&gt;openmenu_Click( )&lt;/b&gt; would get added to our code. On similar lines rest of the handlers can be added. All these handlers will get added to the &lt;b&gt;Form1&lt;/b&gt; class, which is the default class name.&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;"&gt; &lt;/p&gt; &lt;p style="margin: 0pt;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Before we add code to these menu handlers let us insert four new classes. The first amongst these is an abstract class called &lt;b&gt;shapes&lt;/b&gt;. In this class we will store the color of the shape in variables &lt;b&gt;r&lt;/b&gt;, &lt;b&gt;g&lt;/b&gt;, &lt;b&gt;b&lt;/b&gt;, representing red, green and blue components of the color. The other three classes that we would add are &lt;b&gt;line&lt;/b&gt;, &lt;b&gt;rectangle&lt;/b&gt; and &lt;b&gt;ellipse&lt;/b&gt;. These classes are derived from the &lt;b&gt;shapes&lt;/b&gt; class. In these classes we will store the coordinates of the shapes. Each of these three classes would have a constructor to initialize the data members. Each class would have a function &lt;b&gt;draw( )&lt;/b&gt; which would contain the logic to draw the respective shape. This function would be declared as &lt;b&gt;abstract&lt;/b&gt; in the base class &lt;b&gt;shapes&lt;/b&gt;. To make all the classes capable of carrying out serialization/deserialization we need to add the attribute &lt;b&gt;Serializable&lt;/b&gt; as shown in the program listing given below.&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;"&gt; &lt;/p&gt; &lt;p style="margin: 0pt;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;using System ;&lt;br /&gt;using System.Drawing ;&lt;br /&gt;using System.Collections ;&lt;br /&gt;using System.ComponentModel ;&lt;br /&gt;using System.Windows.Forms ;&lt;br /&gt;using System.Data ;&lt;br /&gt;using System.IO ;&lt;br /&gt;using System.Threading ;&lt;br /&gt;using System.Runtime.Serialization ;&lt;br /&gt;using System.Runtime.Serialization.Formatters.Binary;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;"&gt; &lt;/p&gt; &lt;p style="margin: 0pt;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;namespace myapp&lt;br /&gt;{&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 20px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt; [Serializable]&lt;br /&gt;abstract class shapes &lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 20px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;{&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;protected int r, g, b ;&lt;br /&gt;Random rd = new Random( ) ;&lt;br /&gt;public shapes( )&lt;br /&gt;{&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 60px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;r = rd.Next ( 255 ) ;&lt;br /&gt;g = rd.Next ( 255 ) ;&lt;br /&gt;b = rd.Next ( 255 ) ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 60px;"&gt; &lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 60px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt; // put the thread to sleep for next 5&lt;br /&gt;// milliseconds to ensure proper color&lt;br /&gt;// generation&lt;br /&gt;Thread.Sleep ( 5 ) ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;}&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;public abstract void draw ( Graphics g ) ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 20px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;}&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;"&gt; &lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 20px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;[Serializable]&lt;br /&gt;class line : shapes&lt;br /&gt;{&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;int x1, y1 ;&lt;br /&gt;int x2, y2 ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt; public line ( int i, int j, int k, int l )&lt;br /&gt;{&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 60px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;x1 = i ;&lt;br /&gt;y1 = j ;&lt;br /&gt;x2 = k ;&lt;br /&gt;y2 = l ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;}&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;"&gt; &lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt; public override void draw ( Graphics gg )&lt;br /&gt;{&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 60px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Color c = Color.FromArgb ( r, g, b ) ;&lt;br /&gt;Pen p = new Pen ( c, 4 ) ;&lt;br /&gt;gg.DrawLine ( p, x1, y1, x2, y2 ) ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;}&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 20px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;}&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 20px;"&gt; &lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 20px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;[Serializable]&lt;br /&gt;class rectangle : shapes&lt;br /&gt;{&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;int x1, y1 ;&lt;br /&gt;int width, height ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt; public rectangle ( int x, int y, int h, int w )&lt;br /&gt;{&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 60px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;x1 = x ;&lt;br /&gt;y1 = y ;&lt;br /&gt;height = h ;&lt;br /&gt;width = w ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;}&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;public override void draw ( Graphics gg )&lt;br /&gt;{&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 60px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Color c = Color.FromArgb ( r, g, b ) ;&lt;br /&gt;Pen p = new Pen ( c, 4 ) ;&lt;br /&gt;gg.DrawRectangle ( p, x1, y1, width, height ) ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;}&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 20px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;}&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 20px;"&gt; &lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 20px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;[Serializable]&lt;br /&gt;class ellipse : shapes&lt;br /&gt;{&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;int x1, y1 ;&lt;br /&gt;int width, height ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;"&gt; &lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt; public ellipse ( int x, int y, int h, int w )&lt;br /&gt;{&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 60px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;x1 = x ;&lt;br /&gt;y1 = y ;&lt;br /&gt;height = h ;&lt;br /&gt;width = w ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;}&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;public override void draw ( Graphics gg )&lt;br /&gt;{&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 60px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Color c = Color.FromArgb ( r, g, b ) ;&lt;br /&gt;Pen p = new Pen ( c, 4 ) ;&lt;br /&gt;gg.DrawEllipse ( p, x1, y1, width, height ) ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;}&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;"&gt; &lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt; public class Form1 : System.Windows.Forms.Form&lt;br /&gt;{&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 60px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;private System.Windows.Forms.MainMenu&lt;br /&gt;mainMenu1 ;&lt;br /&gt;private System.Windows.Forms.MenuItem&lt;br /&gt;filemenu ;&lt;br /&gt;private System.Windows.Forms.MenuItem&lt;br /&gt;newmenu ;&lt;br /&gt;private System.Windows.Forms.MenuItem&lt;br /&gt;openmenu ;&lt;br /&gt;private System.Windows.Forms.MenuItem&lt;br /&gt;savemenu ;&lt;br /&gt;private System.Windows.Forms.MenuItem&lt;br /&gt;generatemenu ;&lt;br /&gt;private System.Windows.Forms.MenuItem&lt;br /&gt;exitmenu ;&lt;br /&gt;private System.ComponentModel.Container&lt;br /&gt;components = null ;&lt;br /&gt;ArrayList s = new ArrayList( ) ;&lt;br /&gt;BinaryFormatter b = new BinaryFormatter ( ) ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 60px;"&gt; &lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 60px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt; public Form1( )&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 60px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;{&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 80px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;InitializeComponent( ) ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 60px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;}&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 60px;"&gt; &lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 60px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt; protected override void Dispose ( bool disposing )&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 60px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;{&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 80px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;if( disposing )&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 80px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;{&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 100px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;if ( components != null )&lt;br /&gt;{&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 120px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;components.Dispose( ) ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 100px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;}&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 80px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;}&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 80px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;base.Dispose( disposing ) ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 60px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;}&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 60px;"&gt; &lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 60px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;[STAThread]&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 60px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;static void Main( ) &lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 60px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;{&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 80px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Application.Run ( new Form1 ( ) ) ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 60px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;}&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 60px;"&gt; &lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 60px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt; private void openmenu_Click ( object sender, System.EventArgs e )&lt;br /&gt;{&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 80px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;OpenFileDialog od = new&lt;br /&gt;OpenFileDialog( ) ;&lt;br /&gt;od.Filter = "dat files ( *.dat )|*.dat" ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 80px;"&gt; &lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 80px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt; if ( od.ShowDialog( ) == DialogResult.OK )&lt;br /&gt;{&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 100px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;FileInfo f=new FileInfo ( od.FileName);&lt;br /&gt;Stream st = f.Open ( FileMode.Open );&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 100px;"&gt; &lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 100px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt; while ( st.Position != st.Length )&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 100px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;s.Add ( b.Deserialize ( st ) ) ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 100px;"&gt; &lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 100px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt; st.Close ( ) ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 80px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;}&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 80px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Invalidate( ) ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 60px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;}&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 60px;"&gt; &lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 60px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt; private void savemenu_Click ( object sender, System.EventArgs e )&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 60px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;{&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 80px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;SaveFileDialog sd = new&lt;br /&gt;SaveFileDialog( );&lt;br /&gt;sd.Filter = "dat files ( *.dat ) | *.dat" ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 80px;"&gt; &lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 80px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt; if ( sd.ShowDialog( ) == DialogResult.OK )&lt;br /&gt;{&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 100px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;FileInfo f = new FileInfo(sd.FileName);&lt;br /&gt;Stream st = f.Open ( FileMode.Create,&lt;br /&gt;FileAccess.ReadWrite ) ;&lt;br /&gt;foreach ( shapes ss in s )&lt;br /&gt;b.Serialize ( st, ss ) ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 100px;"&gt; &lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 100px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt; st.Close ( ) ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 80px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;}&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 60px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;}&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 60px;"&gt; &lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 60px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt; private void generatemenu_Click ( object sender, System.EventArgs e )&lt;br /&gt;{&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 80px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt; Size sz = ClientSize ;&lt;br /&gt;Random rd = new Random( ) ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 80px;"&gt; &lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 80px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt; for ( int i = 0 ; i &lt; 10 ; i++ )&lt;br /&gt;{&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 100px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;int shapeno = rd.Next ( 3 ) ;&lt;br /&gt;int x1 = rd.Next ( sz.Width ) ;&lt;br /&gt;int y1 = rd.Next ( sz.Height ) ;&lt;br /&gt;int x2 = rd.Next ( sz.Height - y1 ) ;&lt;br /&gt;int y2 = rd.Next ( sz.Width - x1 ) ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 100px;"&gt; &lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 100px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt; switch ( shapeno )&lt;br /&gt;{&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 120px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;case 0:&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 140px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;s.Add ( new line ( x1, y1, x2, y2 ) ) ;&lt;br /&gt;break ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 120px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;case 1:&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 140px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;s.Add ( new rectangle ( x1, y1, x2, y2 ) ) ;&lt;br /&gt;break ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 120px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;case 2:&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 140px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;s.Add ( new ellipse ( x1, y1, x2, y2 ) ) ;&lt;br /&gt;break ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 100px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;}&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 80px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;}&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 80px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Invalidate( ) ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;}&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;"&gt; &lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt; private void exitmenu_Click ( object sender, System.EventArgs e )&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;{&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 60px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Dispose( ) ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt; }&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;"&gt; &lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt; private void Form1_Paint ( object sender,&lt;br /&gt;System.Windows.Forms.PaintEventArgs e )&lt;br /&gt;{&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 60px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt; Graphics g = e.Graphics ;&lt;br /&gt;foreach ( shapes ss in s )&lt;br /&gt;ss.draw ( g ) ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;}&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;"&gt; &lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt; private void newmenu_Click ( object sender, System.EventArgs e )&lt;br /&gt;{&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 60px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;s.Clear( ) ;&lt;br /&gt;Invalidate( ) ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt; }&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 20px;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt; }&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt; }&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;"&gt; &lt;/p&gt; &lt;p style="margin: 0pt;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt; When we click the �Generate� menu item its handler gets called. In this handler an object of the &lt;b&gt;Random&lt;/b&gt; class is created. The &lt;b&gt;Next( )&lt;/b&gt; method of this class generates a positive random number less than the specified number passed to it.&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;"&gt; &lt;/p&gt; &lt;p style="margin: 0pt;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;We have used this function to not only decide which shape should be generated but also the coordinates of this shape. Using these coordinates we have created an object of &lt;b&gt;rectangle&lt;/b&gt;, &lt;b&gt;ellipse&lt;/b&gt; or &lt;b&gt;line&lt;/b&gt; class. While creating these objects the constructors of the respective classes get called. Since all these classes are derived from the &lt;b&gt;shapes&lt;/b&gt; class, firstly the base class constructor gets called. This constructor selects a random color.&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;"&gt; &lt;/p&gt; &lt;p style="margin: 0pt;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;The references of objects of &lt;b&gt;line&lt;/b&gt;, &lt;b&gt;rectangle&lt;/b&gt; and &lt;b&gt;ellipse&lt;/b&gt; are stored using a collection class called &lt;b&gt;ArrayList&lt;/b&gt;. The object &lt;b&gt;s&lt;/b&gt; of this class now consists of references of objects of the &lt;b&gt;line&lt;/b&gt;, &lt;b&gt;rectangle&lt;/b&gt; and &lt;b&gt;ellipse&lt;/b&gt; classes. As these classes are derived from &lt;b&gt;shapes&lt;/b&gt;, it is perfectly legitimate for a reference to &lt;b&gt;shapes&lt;/b&gt; to be set up to point to either &lt;b&gt;shapes&lt;/b&gt; or one of its derived classes.&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;"&gt; &lt;/p&gt; &lt;p style="margin: 0pt;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Next we have called the &lt;b&gt;Invalidate( )&lt;/b&gt; method which results in the &lt;b&gt;Form1_Paint( )&lt;/b&gt; method getting called. Here we have collected back each reference from the array &lt;b&gt;s&lt;/b&gt; into a reference &lt;b&gt;ss&lt;/b&gt; of type &lt;b&gt;shapes&lt;/b&gt;. Using this reference it has then called the &lt;b&gt;draw( )&lt;/b&gt; method. Depending upon which (&lt;b&gt;line&lt;/b&gt;, &lt;b&gt;rectangle&lt;/b&gt; or &lt;b&gt;ellipse&lt;/b&gt;) reference is present in &lt;b&gt;ss&lt;/b&gt; the &lt;b&gt;draw( )&lt;/b&gt; function of that class gets called. The resulting form is shown below.&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;" align="center"&gt;&lt;img src="http://www.kicit.com/freebies/csharp_net_source_code/WinForms/winfor22.jpg" width="325" border="0" height="286" /&gt;&lt;/p&gt; &lt;p style="margin: 0pt;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;If we wish to save the file we can click on �Save� menu item. When we do so &lt;b&gt;savemenu_Click( )&lt;/b&gt; gets called. We have created an object of the &lt;b&gt;SaveFileDialog&lt;/b&gt; class and used a �*.dat� filter for it. When we type in a file name and click Ok, a &lt;b&gt;FileInfo&lt;/b&gt; object gets created with the selected name. The &lt;b&gt;Open( )&lt;/b&gt; function returns a &lt;b&gt;Stream&lt;/b&gt; object associated with the file. We have collected it in a &lt;b&gt;Stream&lt;/b&gt; reference. We have used the &lt;b&gt;BinaryFormatter&lt;/b&gt; object to serialize the objects. The &lt;b&gt;BinaryFormatter&lt;/b&gt; serializes and deserializes an object, in binary format. We have added a &lt;b&gt;BinaryFormatter&lt;/b&gt; object &lt;b&gt;b&lt;/b&gt; to our class. The &lt;b&gt;Serialize( )&lt;/b&gt; method of this class serializes the object to the given stream. After serializing all the elements of the array we have closed the stream using the &lt;b&gt;Close( )&lt;/b&gt; method.&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;"&gt; &lt;/p&gt; &lt;p style="margin: 0pt;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;When we click �Open� menu item an &lt;b&gt;OpenFileDialog&lt;/b&gt; is popped with the appropriate filter. Here also we have created a &lt;b&gt;FileInfo&lt;/b&gt; object and collected the corresponding &lt;b&gt;Stream&lt;/b&gt; of the specified file. Next we have used a &lt;b&gt;while&lt;/b&gt; loop to deserialize the objects until the end of stream is reached. The &lt;b&gt;Deserialize( )&lt;/b&gt; method deserializes the specified stream into an object. We have collected these objects into our array, closed the stream and called &lt;b&gt;Invalidate( )&lt;/b&gt; function for painting these shapes.&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;"&gt; &lt;/p&gt; &lt;p style="margin: 0pt;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;When we click �New� menu item the array is cleared by deleting all the elements in the array. After this we have called &lt;b&gt;Invalidate( )&lt;/b&gt;. This time the method &lt;b&gt;Form1_Paint( )&lt;/b&gt; draws nothing as the array is empty, thereby resulting a clean form.&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;"&gt; &lt;/p&gt; &lt;p style="margin: 0pt;"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;On clicking the �Exit� menu item the &lt;b&gt;Dispose( )&lt;/b&gt; method gets called and the form is disposed.&lt;/span&gt;           &lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/343356482335293710-323459587022993956?l=winformtutorial.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://winformtutorial.blogspot.com/feeds/323459587022993956/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=343356482335293710&amp;postID=323459587022993956' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/343356482335293710/posts/default/323459587022993956'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/343356482335293710/posts/default/323459587022993956'/><link rel='alternate' type='text/html' href='http://winformtutorial.blogspot.com/2008/08/serialization.html' title='Serialization'/><author><name>sandy</name><uri>http://www.blogger.com/profile/02503582209971047801</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-343356482335293710.post-721573544753931298</id><published>2008-08-25T02:36:00.003-07:00</published><updated>2008-12-22T21:26:18.849-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Winform'/><title type='text'>Building Custom Controls</title><content type='html'>&lt;span style="font-family:Arial;font-size:85%;"&gt;Besides the standard controls we may wish to create our own controls. For e.g. the existing controls might not fulfill our application�s needs. So these are nothing but user-defined controls. &lt;/span&gt; &lt;p align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;We plan to make a user-defined timer control. The timer will just show the current date and time in six textboxes. Two functions will be provided to start and stop the timer. &lt;/span&gt;&lt;/p&gt; &lt;p align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;To create a Custom Control, create a new Windows Control Library project in VS.NET: &lt;/span&gt;&lt;/p&gt; &lt;p align="center"&gt;&lt;img src="http://www.kicit.com/freebies/csharp_net_source_code/WinForms/winfor6.jpg" width="413" border="0" height="301" /&gt;&lt;/p&gt;    &lt;p align="left"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Click on OK and a form will be created which would be derived from &lt;b&gt; UserControl&lt;/b&gt; class.&lt;/span&gt;&lt;/p&gt; &lt;p align="left"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Drag in 6 textboxes each for day, month, year, hour, minute and second and four labels two with text �/� and two with text �:�. This is how it should look after adding the controls.&lt;/span&gt;&lt;/p&gt; &lt;p align="center"&gt;&lt;img src="http://www.kicit.com/freebies/csharp_net_source_code/WinForms/winfor7.jpg" width="149" border="0" height="106" /&gt;&lt;/p&gt; &lt;p align="left"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;All the text boxes should be made read-only. Intuitively name them as day, month, year, hour, min and sec. Also add one &lt;b&gt; bool&lt;/b&gt; variable &lt;b&gt;st&lt;/b&gt;, which denotes the status of timer, whether it is on or off. To stop the timer this variable is set to true and to start it this variable is set to false.&lt;/span&gt;&lt;/p&gt; &lt;p align="left"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;To this class we will add 3 functions viz: - &lt;b&gt; settime( )&lt;/b&gt;, &lt;b&gt; start( )&lt;/b&gt; and &lt;b&gt; stop( )&lt;/b&gt;.&lt;/span&gt;&lt;/p&gt; &lt;p align="left"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;To start the timer following code should be written&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;public void start ( )&lt;br /&gt;{&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 20px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt; Thread t = new Thread ( new ThreadStart ( settime ) ) ;&lt;br /&gt; st = false ;&lt;br /&gt; t.Start ( ) ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;}&lt;/span&gt;&lt;/p&gt; &lt;p align="left"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Here a new Thread t is started whose delegate function is &lt;b&gt; settimer( )&lt;/b&gt;. Before starting the Thread the &lt;b&gt; st&lt;/b&gt; variable is set to false. Stopping the timer is as simple as:&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;public void stop ( )&lt;br /&gt;{&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 20px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt; st = true ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;}&lt;/span&gt;&lt;/p&gt; &lt;p align="left"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;In the &lt;b&gt; settimer( ) &lt;/b&gt; function we set the Text properties of all the textboxes to Current Date and Time using the &lt;b&gt; DateTime&lt;/b&gt; class. We made the thread wait for 100 milliseconds and kept on updating the textboxes with the appropriate values until st was made to be true, This could happen only if the stop function was called.&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;public void settime ( )&lt;br /&gt;{&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 20px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;while ( ! st )&lt;br /&gt; {&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;day.Text = DateTime.Now.Day.ToString ( ) ;&lt;br /&gt;  month.Text = DateTime.Now.Month.ToString ( );&lt;br /&gt;  year.Text = DateTime.Now.Year.ToString ( ) ;&lt;br /&gt;  hour.Text = DateTime.Now.Hour.ToString ( ) ;&lt;br /&gt;  min.Text = DateTime.Now.Minute.ToString ( ) ;&lt;br /&gt;  sec.Text = DateTime.Now.Second.ToString ( ) ;&lt;br /&gt;  Thread.Sleep ( 100 ) ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 20px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt; }&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;}&lt;/span&gt;&lt;/p&gt; &lt;p align="left"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Now we need to build and not compile the code. This will create the &lt;b&gt; mycontrol.dll&lt;/b&gt; file.&lt;/span&gt;&lt;/p&gt;   &lt;center&gt; &lt;table width="100%" border="0" cellpadding="0" cellspacing="0"&gt;   &lt;tbody&gt;&lt;tr&gt;     &lt;td width="13%"&gt;&lt;hr size="4" color="#990033"&gt;&lt;/td&gt;     &lt;td width="16%" bg style="color:#cc0099;"&gt;&lt;span style="font-family:Comic Sans MS;font-size:100%;color:#ffffff;"&gt; Client &lt;/span&gt;&lt;/td&gt;     &lt;td width="71%"&gt;&lt;hr size="4" color="#990033"&gt;&lt;/td&gt;   &lt;/tr&gt; &lt;/tbody&gt;&lt;/table&gt;   &lt;/center&gt; &lt;p align="left"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;To use this control we have to create a client for it. To make a client, create a Windows Application project and name it as client. To add the custom control to the toolbox right click in the Toolbox area, and select Customize Toolbox. Select the .Net Framework Components tab, and then the browse button. Add in the dll we just created. After this gets added, we can drag and drop it in our form. We will add two buttons to start and stop the timer.&lt;/span&gt;&lt;/p&gt; &lt;p align="center"&gt;&lt;img src="http://www.kicit.com/freebies/csharp_net_source_code/WinForms/winfor8.jpg" width="168" border="0" height="168" /&gt;&lt;/p&gt; &lt;p align="left"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;The start_click( ) and stop_click( ) look like this:&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;private void start_Click (object sender, System.EventArgs e )&lt;br /&gt;{&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 20px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt; mytimer.start ( ) ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;}&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;private void stop_Click ( object sender, System.EventArgs e )&lt;br /&gt;{&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 20px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;mytimer.stop ( ) ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;}&lt;/span&gt;&lt;/p&gt; &lt;p align="left"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;And at any instance of time when we press start we should get the current time and date as shown:&lt;/span&gt;&lt;/p&gt;       &lt;p align="center"&gt;&lt;img src="http://www.kicit.com/freebies/csharp_net_source_code/WinForms/winfor9.jpg" width="168" border="0" height="168" /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/343356482335293710-721573544753931298?l=winformtutorial.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://winformtutorial.blogspot.com/feeds/721573544753931298/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=343356482335293710&amp;postID=721573544753931298' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/343356482335293710/posts/default/721573544753931298'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/343356482335293710/posts/default/721573544753931298'/><link rel='alternate' type='text/html' href='http://winformtutorial.blogspot.com/2008/08/building-custom-controls.html' title='Building Custom Controls'/><author><name>sandy</name><uri>http://www.blogger.com/profile/02503582209971047801</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-343356482335293710.post-5248996596865896929</id><published>2008-08-25T02:36:00.001-07:00</published><updated>2008-12-22T21:26:18.849-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Winform'/><title type='text'>Image Explorer</title><content type='html'>&lt;span style="font-family:Arial;font-size:85%;"&gt;In this program we have used 4 controls viz: a &lt;b&gt; TreeView&lt;/b&gt; Control, a &lt;b&gt;ListView&lt;/b&gt; Control, a Panel, and a &lt;b&gt; PictureBox&lt;/b&gt;. The &lt;b&gt; TreeView&lt;/b&gt; Control is used to explore directories and files. The &lt;b&gt; ListView&lt;/b&gt; control is used to display the list of all the �.bmp�, �.jpg�, and �.gif� files. The &lt;b&gt; PictureBox&lt;/b&gt; is used to display the file selected in the &lt;b&gt; ListView&lt;/b&gt;. The Panel is used to make the &lt;b&gt; PictureBox&lt;/b&gt; scrollable.&lt;/span&gt; &lt;p align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;We also plan to add a status bar with two panels in it to display the Path and Size of the selected file.&lt;/span&gt;&lt;/p&gt; &lt;p align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;To start with, we have dragged the &lt;b&gt; TreeView&lt;/b&gt;, &lt;b&gt; ListView&lt;/b&gt; and the &lt;b&gt; Panel&lt;/b&gt; into our form. Next we have dragged in the &lt;b&gt; PictureBox&lt;/b&gt;. We have dragged the &lt;b&gt; PictureBox&lt;/b&gt; into the panel and not in the Form. Here we should take care that our &lt;b&gt; PictureBox&lt;/b&gt; should be larger in size than our &lt;b&gt;Panel&lt;/b&gt;. This will make the whole thing scrollable. If the Picture is not big enough to fit in the &lt;b&gt; Panel&lt;/b&gt; the &lt;b&gt; Panel&lt;/b&gt; will automatically be provided with vertical and horizontal scroll bars. The &lt;b&gt; PictureBox&lt;/b&gt; control is non-scrollable and it clips the regions of the picture, which do not fit in it.&lt;/span&gt;&lt;/p&gt; &lt;p align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;We then have dragged in 3 &lt;b&gt; GroupBoxes&lt;/b&gt; to enclose these three controls. We should make sure to right click on the &lt;b&gt; GroupBox&lt;/b&gt; and select �Send to back�, to have the &lt;b&gt; GroupBoxes&lt;/b&gt; in background.&lt;/span&gt;&lt;/p&gt; &lt;p align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Next we have dragged in a &lt;b&gt; StatusBar&lt;/b&gt; in our Form. Next we clicked the &lt;b&gt; StatusBar&lt;/b&gt; properties window, clicked on Panels (Collection) and added two panels by clicking the �Add� button with names: - myspanel1 and myspanel2 We then made the following changes in the properties window of the corresponding controls&lt;/span&gt;&lt;/p&gt; &lt;div align="center"&gt;   &lt;center&gt;   &lt;table width="50%" border="1" border cellpadding="0" cellspacing="0" style="color:#000000;"&gt;     &lt;tbody&gt;&lt;tr&gt;       &lt;td width="33%"&gt;&lt;b&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Control&lt;/span&gt;&lt;/b&gt;&lt;/td&gt;       &lt;td width="33%"&gt;&lt;b&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Change&lt;/span&gt;&lt;/b&gt;&lt;/td&gt;       &lt;td width="34%"&gt;&lt;b&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;To&lt;/span&gt;&lt;/b&gt;&lt;/td&gt;     &lt;/tr&gt;     &lt;tr&gt;       &lt;td width="33%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;TreeView&lt;/span&gt;&lt;/td&gt;       &lt;td width="33%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Name&lt;/span&gt;&lt;/td&gt;       &lt;td width="34%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;tree&lt;/span&gt;&lt;/td&gt;     &lt;/tr&gt;     &lt;tr&gt;       &lt;td width="33%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;ListView&lt;/span&gt;&lt;/td&gt;       &lt;td width="33%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Name&lt;/span&gt;&lt;/td&gt;       &lt;td width="34%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;list&lt;/span&gt;&lt;/td&gt;     &lt;/tr&gt;     &lt;tr&gt;       &lt;td width="33%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Panel&lt;/span&gt;&lt;/td&gt;       &lt;td width="33%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Name&lt;/span&gt;&lt;/td&gt;       &lt;td width="34%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;mypanel&lt;/span&gt;&lt;/td&gt;     &lt;/tr&gt;     &lt;tr&gt;       &lt;td width="33%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Panel&lt;/span&gt;&lt;/td&gt;       &lt;td width="33%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;BorderStyle&lt;/span&gt;&lt;/td&gt;       &lt;td width="34%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;FixedSingle&lt;/span&gt;&lt;/td&gt;     &lt;/tr&gt;     &lt;tr&gt;       &lt;td width="33%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;PictureBox&lt;/span&gt;&lt;/td&gt;       &lt;td width="33%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Name&lt;/span&gt;&lt;/td&gt;       &lt;td width="34%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;pic&lt;/span&gt;&lt;/td&gt;     &lt;/tr&gt;     &lt;tr&gt;       &lt;td width="33%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;1st GroupBox&lt;/span&gt;&lt;/td&gt;       &lt;td width="33%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Name&lt;/span&gt;&lt;/td&gt;       &lt;td width="34%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;List&lt;/span&gt;&lt;/td&gt;     &lt;/tr&gt;     &lt;tr&gt;       &lt;td width="33%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;2nd GroupBox&lt;/span&gt;&lt;/td&gt;       &lt;td width="33%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Name&lt;/span&gt;&lt;/td&gt;       &lt;td width="34%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Files&lt;/span&gt;&lt;/td&gt;     &lt;/tr&gt;     &lt;tr&gt;       &lt;td width="33%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;3rd GroupBox&lt;/span&gt;&lt;/td&gt;       &lt;td width="33%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Name&lt;/span&gt;&lt;/td&gt;       &lt;td width="34%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Image&lt;/span&gt;&lt;/td&gt;     &lt;/tr&gt;     &lt;tr&gt;       &lt;td width="33%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;StatusBar&lt;/span&gt;&lt;/td&gt;       &lt;td width="33%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Name&lt;/span&gt;&lt;/td&gt;       &lt;td width="34%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Mysbar&lt;/span&gt;&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;   &lt;/center&gt; &lt;/div&gt;       &lt;p align="center"&gt;&lt;img src="http://www.kicit.com/freebies/csharp_net_source_code/WinForms/winfor3.jpg" width="413" border="0" height="250" /&gt;       &lt;/p&gt;&lt;p align="left"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Next we have added the       following events to our program.&lt;br /&gt;       &lt;/span&gt; &lt;/p&gt;&lt;div align="center"&gt;   &lt;center&gt;   &lt;table width="370" border="1" border cellpadding="0" cellspacing="0" style="color:#000000;"&gt;     &lt;tbody&gt;&lt;tr&gt;       &lt;td width="57"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Control&lt;/span&gt;&lt;/td&gt;       &lt;td width="136"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Event&lt;/span&gt;&lt;/td&gt;       &lt;td width="155"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Meaning&lt;/span&gt;&lt;/td&gt;     &lt;/tr&gt;     &lt;tr&gt;       &lt;td width="57"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;TreeView&lt;/span&gt;&lt;/td&gt;       &lt;td width="136"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;AfterExpand&lt;/span&gt;&lt;/td&gt;       &lt;td width="155"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Occurs when a node has been expanded&lt;/span&gt;&lt;/td&gt;     &lt;/tr&gt;     &lt;tr&gt;       &lt;td width="57"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;TreeView&lt;/span&gt;&lt;/td&gt;       &lt;td width="136"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;AfterSelect&lt;/span&gt;&lt;/td&gt;       &lt;td width="155"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Occurs when selection has been changed&lt;/span&gt;&lt;/td&gt;     &lt;/tr&gt;     &lt;tr&gt;       &lt;td width="57"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;ListView&lt;/span&gt;&lt;/td&gt;       &lt;td width="136"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;SelectedIndexChanged&lt;/span&gt;&lt;/td&gt;       &lt;td width="155"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Occurs when an selected index is changed&lt;/span&gt;&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;         &lt;/center&gt; &lt;/div&gt;   &lt;p&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;We have also manually added the following method:- &lt;b&gt; getpath( )&lt;/b&gt;, &lt;b&gt; adddrives( )&lt;/b&gt;, and &lt;b&gt; check( )&lt;/b&gt;.We will look at these events and methods one by one.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;We have first called the &lt;b&gt; adddrives( )&lt;/b&gt; method in the &lt;b&gt; Form1&lt;/b&gt; constructor after the &lt;b&gt; InitializeComponent( )&lt;/b&gt; call.&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;public Form1( )&lt;br /&gt;{&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 20px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt; InitializeComponent ( ) ;&lt;br /&gt; adddrives ( ) ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;}&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;" align="justify"&gt; &lt;/p&gt; &lt;p style="margin: 0pt;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;&lt;b&gt;adddrives( )&lt;/b&gt; &lt;/span&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;function looks like this:&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt; &lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;public void adddrives ( )&lt;br /&gt;{&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 20px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt; string [ ]  drives = Directory.GetLogicalDrives ( ) ;&lt;br /&gt;foreach ( string  i in drives )&lt;br /&gt; {&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;  TreeNode t = new  TreeNode ( ) ;&lt;br /&gt;  t = tree.Nodes.Add ( i ) ;&lt;br /&gt;  t.Nodes.Add ( " " ) ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 20px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt; }&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;}&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;The static function &lt;b&gt; GetLogicalDrives( )&lt;/b&gt; retrieves the names of the logical drives on the machine in the form of strings. We have collected them in a string array called drives. We have added a node for every drive present in the machine. To do this we have created a new &lt;b&gt; TreeNode&lt;/b&gt; every time and added it to the tree using the &lt;b&gt; Nodes.Add( )&lt;/b&gt; method. The last &lt;b&gt; t.Nodes.Add( )&lt;/b&gt; is used to display the �+� sign, and it adds a blank node .We should now add events to our program.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;When we executed this program on our machine we got this&lt;/span&gt;&lt;/p&gt; &lt;p align="center"&gt;&lt;img src="http://www.kicit.com/freebies/csharp_net_source_code/WinForms/winfor4.jpg" width="413" border="0" height="250" /&gt;&lt;/p&gt; &lt;p align="left"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Lets look at the events we added for the &lt;b&gt; TreeView&lt;/b&gt; control.&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;protected void tree_AfterExpand ( object sender, System.WinForms.TreeViewEventArgs e )&lt;br /&gt;{&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 20px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;TreeNode  t = e.node ;&lt;br /&gt; DirectoryInfo  d = null ;&lt;br /&gt; if ( t.Parent == null )&lt;br /&gt; {&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;t.Nodes.Remove( 0 ) ;&lt;br /&gt;  d = new  DirectoryInfo ( e.node.Text ) ;&lt;br /&gt;  Check ( d, t ) ; &lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 20px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt; }&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;}&lt;/span&gt;&lt;/p&gt; &lt;p align="left"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;This event is fired when a node is expanded. We have collected the reference of the node to be expanded in t. Then we have created a directory referencing a null value. We have then checked if it�s a parent node or not. If it is, we have deleted the first blank node we had added using the&lt;b&gt; Remove( )&lt;/b&gt; method. We have stored the reference of this directory in d and have called the &lt;b&gt; Check( )&lt;/b&gt; method. The &lt;b&gt; Check( )&lt;/b&gt; method is shown below&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;public static void Check ( Directory d, TreeNode t )&lt;br /&gt;{&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 20px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt; FileSystemInfo [ ] f = d.GetFileSystemInfos ( ) ;&lt;br /&gt; foreach ( FileSystemInfo i in f )&lt;br /&gt; {&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;  if ( i.GetType ( ).ToString ( ) == "System.IO.DirectoryInfo" )&lt;br /&gt;  {&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 60px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;   DirectoryInfo  d1 = new  DirectoryInfo ( i.FullName ) ;&lt;br /&gt;   TreeNode  t1 = t.Nodes.Add ( i.Name ) ;&lt;br /&gt;   Check ( d1, t1 ) ; &lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;  }&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 20px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;&lt;br /&gt; }&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;}&lt;/span&gt;&lt;/p&gt; &lt;p align="left"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;This is a recursive method. In this method we wish to explore all the directories and sub-directories. We have passed a directory object and a node to this function. We have collected them in d and t respectively. The &lt;b&gt; GetFileSystemEntries( )&lt;/b&gt; method returns all �&lt;b&gt;Entries&lt;/b&gt;� in that particular directory, be it a file or a sub directory. We have used an array of the &lt;b&gt; FileSystemEntry&lt;/b&gt; class to store them. We have then used a &lt;b&gt;foreach&lt;/b&gt; loop to iterate through all the elements in the array. If the element happened to be a file, we neglected it. But if was a directory we added it as a node to the tree and called the &lt;b&gt; Check( )&lt;/b&gt; function on this directory to explore any sub directories, any sub-sub directories and so on within it. At the end of the function we have all the directories, sub-directories and so on, added to the tree.&lt;/span&gt;&lt;/p&gt; &lt;p align="left"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;If we select any of the directories in the &lt;b&gt; TreeView&lt;/b&gt; we should get a list of .bmp, .jpg and .gif files, contained in that directory, in the &lt;b&gt; ListView&lt;/b&gt;. For this the code added to the &lt;b&gt; tree_AfterSelect( ) &lt;/b&gt; event handler is&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;protected void tree_AfterSelect ( object sender,  System.WinForms.TreeViewEventArgs e )&lt;br /&gt;{&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 20px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;selpath = getpath ( e.node ) ;&lt;br /&gt; DirectoryInfo  d = new DirectoryInfo ( selpath ) ;&lt;br /&gt; FileInfo [ ] f = d.GetFiles ( ) ;&lt;br /&gt; int n = 0 ;&lt;br /&gt; list.Clear( ) ;&lt;br /&gt; list.Invalidate( ) ;&lt;br /&gt; foreach ( FileInfo i in f )&lt;br /&gt; {&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;  string str = i.Extension ;&lt;br /&gt;  if ( str == ".bmp" || str == ".jpg" || str == ".gif" )&lt;br /&gt;  {&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 60px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;    list.Items.Add ( i.Name,n ) ;&lt;br /&gt;    n++ ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;}&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 20px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt; }&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;}&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;" align="justify"&gt; &lt;/p&gt; &lt;p style="margin: 0pt;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;We have added the &lt;b&gt; string&lt;/b&gt; variable &lt;b&gt; selpath&lt;/b&gt; as a private data member to the Form1 class, because we have to use it in the method &lt;b&gt; list_ItemActivated( )&lt;/b&gt; event. It stores the path. The &lt;b&gt; getpath( )&lt;/b&gt; method returns the full path of that particular node (selected directory) which we have collected in &lt;b&gt; selpath&lt;/b&gt;. We will look at this method in a moment. After obtaining the complete path we have created a new directory object with this path. The call &lt;b&gt; d.GetFiles( )&lt;/b&gt; returns an array of all the files present in that particular directory. The &lt;b&gt; Clear( )&lt;/b&gt; and &lt;b&gt; Invalidate( )&lt;/b&gt; methods help in repainting and clearing the &lt;b&gt; ListView&lt;/b&gt;. We have then used a foreach loop on all the files in the array. The .bmp, .jpg, and .gif files are filtered out and added to the &lt;b&gt; ListView&lt;/b&gt; using the &lt;b&gt; InsertItem( )&lt;/b&gt; method. We have passed the index and Name of the file to this method. Let�s now look at the &lt;b&gt; getpath( )&lt;/b&gt; method&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;" align="justify"&gt; &lt;/p&gt; &lt;p style="margin: 0pt;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;string getpath ( TreeNode h )&lt;br /&gt;{&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 20px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt; string result = h.Text ;&lt;br /&gt; TreeNode hparent ;&lt;br /&gt; while ( ( hparent = h.Parent ) != null )&lt;br /&gt; {&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;  string  str = hparent.Text ;&lt;br /&gt;  if ( !str.EndsWith ( "\\" ) )&lt;br /&gt;   str += "\\" ;&lt;br /&gt;  result = str + result ;&lt;br /&gt;  h = hparent ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 20px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt; }&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 20px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt; return result ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;}&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt; &lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;We have first stored the &lt;b&gt; Text&lt;/b&gt; of the node (directory), which gives the name of the node in the &lt;b&gt; string&lt;/b&gt; variable result. The logic here is, we have to create a path for the directory chosen. What we have in hand is only the name of the node and not the full path. We have to keep on prefixing the name with its parent�s name until we don�t get the topmost node, which will obviously be the drive name and then finally we will get the full path. We have collected the parent�s name in variable str. To do this we have used a while loop until we reached the drive (highest node). The statement, result = str + result, keeps on prefixing the parent�s name to result. h = hparent keeps on iterating from a node to its parent. When we come across a directory we have added a �\� to str. So, eventually the path is stored in result and returned back to the calling function.&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Now if we double click on any item present in the &lt;b&gt; ListView&lt;/b&gt; we should get the image in the &lt;b&gt; PictureBox&lt;/b&gt;. For this the following code should be added to the &lt;b&gt; list_ItemActivate( )&lt;/b&gt; method.&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;" align="justify"&gt; &lt;/p&gt; &lt;p style="margin: 0pt;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;protected void list_ItemActivate (object sender, System.EventArgs e)&lt;br /&gt;{&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 20px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;string str = list.FocusedItem.Text;&lt;br /&gt;&lt;br /&gt; if ( !selpath.EndsWith("\\" ) ) &lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;  selpath += "\\" ;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 20px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt; str = selpath + str ;&lt;br /&gt; Image img = Image.FromFile ( str ) ;&lt;br /&gt; FileInfo f = new FileInfo(str) ;&lt;br /&gt; myspanel1.Text = "Path: " + f.FullName ;&lt;br /&gt; myspanel2.Text = "Size in bytes: " + f.Length ;&lt;br /&gt; pic.Width = img.Width ;&lt;br /&gt; pic.Height = img.Height ;&lt;br /&gt; pic.Image = img;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;}&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;" align="justify"&gt; &lt;/p&gt; &lt;p style="margin: 0pt;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;We have first completed the full path of the file chosen by concatenating &lt;b&gt; selpath&lt;/b&gt; with the selected Item�s text (nothing but the file name). Next we have created an object of the Image class. The&lt;b&gt; FromFile( )&lt;/b&gt; method  creates an image from the specified file. We have displayed the File Path and Size of the selected file on the &lt;b&gt; StatusBar&lt;/b&gt;. To do so we have to set the Text properties of both the &lt;b&gt; StatusBars&lt;/b&gt; to &lt;b&gt; FullName&lt;/b&gt; and &lt;b&gt; Length&lt;/b&gt; as shown in the following snippet.&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;" align="justify"&gt; &lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 20px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;myspanel1.Text = "Path: " + f.FullName ;&lt;br /&gt;myspanel2.Text = "Size in bytes: " + f.Length ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;" align="justify"&gt; &lt;/p&gt; &lt;p style="margin: 0pt;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;&lt;b&gt;FullName&lt;/b&gt; and &lt;b&gt; Length&lt;/b&gt; are properties of the &lt;b&gt; FileInfo&lt;/b&gt; class. The only thing left is to display the image in the &lt;b&gt; PictureBox&lt;/b&gt;, which is accomplished by using the following statements.&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;" align="justify"&gt; &lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 20px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;pic.Width = img.Width ;&lt;br /&gt;pic.Height = img.Height ;&lt;br /&gt;pic.Image = img;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;" align="justify"&gt; &lt;/p&gt; &lt;p style="margin: 0pt;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;The &lt;b&gt; Width&lt;/b&gt; and &lt;b&gt; Height&lt;/b&gt; properties of the &lt;b&gt; PictureBox&lt;/b&gt; are set to the Width and Height properties of the Image object. Last but not the least we have set the Image property of the &lt;b&gt; PictureBox&lt;/b&gt; to our image object. This will display our image in the &lt;b&gt; PicturBox&lt;/b&gt;. Following should be the output when we compile it:&lt;/span&gt;&lt;/p&gt;   &lt;p style="margin: 0pt;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt; &lt;/span&gt;&lt;/p&gt;   &lt;p style="margin: 0pt;" align="center"&gt;&lt;img src="http://www.kicit.com/freebies/csharp_net_source_code/WinForms/winfor5.jpg" width="422" border="0" height="249" /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/343356482335293710-5248996596865896929?l=winformtutorial.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://winformtutorial.blogspot.com/feeds/5248996596865896929/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=343356482335293710&amp;postID=5248996596865896929' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/343356482335293710/posts/default/5248996596865896929'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/343356482335293710/posts/default/5248996596865896929'/><link rel='alternate' type='text/html' href='http://winformtutorial.blogspot.com/2008/08/image-explorer.html' title='Image Explorer'/><author><name>sandy</name><uri>http://www.blogger.com/profile/02503582209971047801</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-343356482335293710.post-7571520407585749900</id><published>2008-08-25T02:28:00.001-07:00</published><updated>2008-12-22T21:26:18.849-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Winform'/><title type='text'>Simple Winform</title><content type='html'>&lt;span style="font-family:Arial;font-size:85%;"&gt;Lets look at a better program now, which takes in some information about an employee and displays out the same. We plan to add the following Controls in our program.&lt;/span&gt; &lt;ol type="a"&gt;&lt;li&gt;     &lt;p align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Lables&lt;/span&gt;   &lt;/p&gt;&lt;/li&gt;&lt;li&gt;     &lt;p align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;TextBoxes&lt;/span&gt;   &lt;/p&gt;&lt;/li&gt;&lt;li&gt;     &lt;p align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;ComboBox&lt;/span&gt;   &lt;/p&gt;&lt;/li&gt;&lt;li&gt;     &lt;p align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;GroupBoxes&lt;/span&gt;   &lt;/p&gt;&lt;/li&gt;&lt;li&gt;     &lt;p align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;RadioButons&lt;/span&gt;   &lt;/p&gt;&lt;/li&gt;&lt;li&gt;     &lt;p align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;ListBox&lt;/span&gt;   &lt;/p&gt;&lt;/li&gt;&lt;li&gt;     &lt;p align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Button&lt;/span&gt; &lt;/p&gt;&lt;/li&gt;&lt;/ol&gt; &lt;p align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;These Controls are available in the Toolbox. If u don�t see the Toolbox just press Ctrl+Alt+X .We now have to drag the controls onto the form. When we do this VS.Net will automatically add some code to the &lt;b&gt; InitializeComponent( )&lt;/b&gt; method. Whenever we drag a control in the form, its reference is created first. Next the Location, Size, Text and TabIndex properties of the corresponding controls also get initialized.&lt;/span&gt;&lt;/p&gt; &lt;p align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;In all we will have to drag 2 TextBoxes, 3 Lables, 1 ComboBox, 2 GroupBoxes, 2 RadioButtons, 1 ListBox, and 1 Button.&lt;/span&gt;&lt;/p&gt; &lt;p align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Open the �&lt;b&gt;Properties&lt;/b&gt;� window and keep it pinned. Pinning the window down prevents auto hiding of the window, i.e. the window does not slide back when mouse pointer is not on it. To Pin the window down click the small pin present on the top right corner of the window. Whenever we click a control to highlight it, the Properties window shows that control�s properties.&lt;/span&gt;&lt;/p&gt; &lt;p align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;We lhave made the following changes in the properties window&lt;/span&gt;&lt;/p&gt; &lt;div align="center"&gt;   &lt;center&gt;   &lt;table width="50%" border="1" border cellpadding="0" cellspacing="0" style="color:#000000;"&gt;     &lt;tbody&gt;&lt;tr&gt;       &lt;td width="36%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;&lt;b&gt;Control&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;       &lt;td width="30%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;&lt;b&gt;Change&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;       &lt;td width="34%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;&lt;b&gt;To&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;     &lt;/tr&gt;     &lt;tr&gt;       &lt;td width="36%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Form1&lt;/span&gt;&lt;/td&gt;       &lt;td width="30%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Text&lt;/span&gt;&lt;/td&gt;       &lt;td width="34%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Employee&lt;/span&gt;&lt;/td&gt;     &lt;/tr&gt;     &lt;tr&gt;       &lt;td width="36%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;1st label&lt;/span&gt;&lt;/td&gt;       &lt;td width="30%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Name&lt;/span&gt;&lt;/td&gt;       &lt;td width="34%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;lname&lt;/span&gt;&lt;/td&gt;     &lt;/tr&gt;     &lt;tr&gt;       &lt;td width="36%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;1st label&lt;/span&gt;&lt;/td&gt;       &lt;td width="30%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Text&lt;/span&gt;&lt;/td&gt;       &lt;td width="34%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Name&lt;/span&gt;&lt;/td&gt;     &lt;/tr&gt;     &lt;tr&gt;       &lt;td width="36%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;2nd label&lt;/span&gt;&lt;/td&gt;       &lt;td width="30%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Name&lt;/span&gt;&lt;/td&gt;       &lt;td width="34%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;lage&lt;/span&gt;&lt;/td&gt;     &lt;/tr&gt;     &lt;tr&gt;       &lt;td width="36%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;2nd label&lt;/span&gt;&lt;/td&gt;       &lt;td width="30%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Text&lt;/span&gt;&lt;/td&gt;       &lt;td width="34%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Age&lt;/span&gt;&lt;/td&gt;     &lt;/tr&gt;     &lt;tr&gt;       &lt;td width="36%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;3rd label&lt;/span&gt;&lt;/td&gt;       &lt;td width="30%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Name&lt;/span&gt;&lt;/td&gt;       &lt;td width="34%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;lsal&lt;/span&gt;&lt;/td&gt;     &lt;/tr&gt;     &lt;tr&gt;       &lt;td width="36%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;3rd label&lt;/span&gt;&lt;/td&gt;       &lt;td width="30%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Text&lt;/span&gt;&lt;/td&gt;       &lt;td width="34%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Salary&lt;/span&gt;&lt;/td&gt;     &lt;/tr&gt;     &lt;tr&gt;       &lt;td width="36%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;1st Text Box&lt;/span&gt;&lt;/td&gt;       &lt;td width="30%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Name&lt;/span&gt;&lt;/td&gt;       &lt;td width="34%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;name&lt;/span&gt;&lt;/td&gt;     &lt;/tr&gt;     &lt;tr&gt;       &lt;td width="36%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;1st Text Box&lt;/span&gt;&lt;/td&gt;       &lt;td width="30%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Name&lt;/span&gt;&lt;/td&gt;       &lt;td width="34%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;age&lt;/span&gt;&lt;/td&gt;     &lt;/tr&gt;     &lt;tr&gt;       &lt;td width="36%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Combo Box&lt;/span&gt;&lt;/td&gt;       &lt;td width="30%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Name&lt;/span&gt;&lt;/td&gt;       &lt;td width="34%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;sal&lt;/span&gt;&lt;/td&gt;     &lt;/tr&gt;     &lt;tr&gt;       &lt;td width="36%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;1st Group Box&lt;/span&gt;&lt;/td&gt;       &lt;td width="30%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Name&lt;/span&gt;&lt;/td&gt;       &lt;td width="34%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;grpgenr&lt;/span&gt;&lt;/td&gt;     &lt;/tr&gt;     &lt;tr&gt;       &lt;td width="36%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;1st Group Box&lt;/span&gt;&lt;/td&gt;       &lt;td width="30%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Text&lt;/span&gt;&lt;/td&gt;       &lt;td width="34%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Gender&lt;/span&gt;&lt;/td&gt;     &lt;/tr&gt;     &lt;tr&gt;       &lt;td width="36%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;2nd Group Box&lt;/span&gt;&lt;/td&gt;       &lt;td width="30%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Name&lt;/span&gt;&lt;/td&gt;       &lt;td width="34%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;grpexp&lt;/span&gt;&lt;/td&gt;     &lt;/tr&gt;     &lt;tr&gt;       &lt;td width="36%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;2nd Group Box&lt;/span&gt;&lt;/td&gt;       &lt;td width="30%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Text&lt;/span&gt;&lt;/td&gt;       &lt;td width="34%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Experience&lt;/span&gt;&lt;/td&gt;     &lt;/tr&gt;     &lt;tr&gt;       &lt;td width="36%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;List Box&lt;/span&gt;&lt;/td&gt;       &lt;td width="30%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Name&lt;/span&gt;&lt;/td&gt;       &lt;td width="34%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;exp&lt;/span&gt;&lt;/td&gt;     &lt;/tr&gt;     &lt;tr&gt;       &lt;td width="36%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;1st Radio Button&lt;/span&gt;&lt;/td&gt;       &lt;td width="30%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Name&lt;/span&gt;&lt;/td&gt;       &lt;td width="34%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;male&lt;/span&gt;&lt;/td&gt;     &lt;/tr&gt;     &lt;tr&gt;       &lt;td width="36%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;1st Radio Button&lt;/span&gt;&lt;/td&gt;       &lt;td width="30%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Text&lt;/span&gt;&lt;/td&gt;       &lt;td width="34%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Male&lt;/span&gt;&lt;/td&gt;     &lt;/tr&gt;     &lt;tr&gt;       &lt;td width="36%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;2nd Radio Button&lt;/span&gt;&lt;/td&gt;       &lt;td width="30%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Name&lt;/span&gt;&lt;/td&gt;       &lt;td width="34%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;female&lt;/span&gt;&lt;/td&gt;     &lt;/tr&gt;     &lt;tr&gt;       &lt;td width="36%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;2nd Radio Button&lt;/span&gt;&lt;/td&gt;       &lt;td width="30%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Text&lt;/span&gt;&lt;/td&gt;       &lt;td width="34%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Female&lt;/span&gt;&lt;/td&gt;     &lt;/tr&gt;     &lt;tr&gt;       &lt;td width="36%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Button&lt;/span&gt;&lt;/td&gt;       &lt;td width="30%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Text&lt;/span&gt;&lt;/td&gt;       &lt;td width="34%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;OK&lt;/span&gt;&lt;/td&gt;     &lt;/tr&gt;     &lt;tr&gt;       &lt;td width="36%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Button&lt;/span&gt;&lt;/td&gt;       &lt;td width="30%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Name&lt;/span&gt;&lt;/td&gt;       &lt;td width="34%"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;ok&lt;/span&gt;&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;   &lt;/center&gt; &lt;/div&gt; &lt;p&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;We have kept the &lt;b&gt; Text&lt;/b&gt; fields of both &lt;b&gt; TextBoxes&lt;/b&gt; to null. This is because when we start the application nothing should be already written in the text boxes.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;This is how our Form should look now&lt;/span&gt;&lt;/p&gt; &lt;p align="center"&gt;&lt;img src="http://www.kicit.com/freebies/csharp_net_source_code/WinForms/winforms2.gif" width="403" border="0" height="340" /&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;&lt;br /&gt;But then we have not yet added any items to the &lt;b&gt; ListBox&lt;/b&gt; and &lt;b&gt; ComboBox&lt;/b&gt; ! To do so, click on the small button near the Items (Collection) property in the Properties window of the &lt;b&gt; ListBox&lt;/b&gt; and add the strings separated by paragraphs (or Enters).&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;This will make the VS.Net to add the following code in Form1.cs&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 20px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;this.exp.Items.AddRange ( new object [ ] { "Less than a year", "1 - 2 yrs",&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 120px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;"2 - 5 yrs", "more than 5 yrs" } ) ;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;In the same manner, add items for the &lt;b&gt; ComboBox&lt;/b&gt;. The following code gets added&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 20px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;this.sal.Items.AddRange ( new  object [ ]&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 80px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;{"2500","3500","5000","10000","20000" }) ;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;We can see that the items �2500� and �Less than a year� are already selected. This is because of the following Code&lt;/span&gt;&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;sal.SelectedIndex = 0 ;&lt;br /&gt;exp.SelectedIndex = 0;&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt; &lt;p&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;We should add these statements in the constructor after the call to the &lt;b&gt; InitializeComponent( ) &lt;/b&gt; method.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Next we have added an event for the Button �OK�. To add an event, open the Properties window of this Button. Click on the button, which has an icon of a yellow lightning on it. This gives us a list of Events we can add for that control. Double click on the �Click� entry. This adds an event �&lt;b&gt;ok_Click( )&lt;/b&gt;� to our application., and the following gets added in the &lt;b&gt; InitializeComponent( ) &lt;/b&gt; method.&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 20px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;ok.Click += new  System.EventHandler ( this.ok_Click ) ;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Such code gets added for each event we add. In this event we plan to display the information entered by our user.&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;protected void ok_Click (object sender, System.EventArgs e)&lt;br /&gt;{&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 20px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt; string str1,str = "Name:          " + name.Text +"\n" ;&lt;br /&gt; str += "Age:             " + age.Text + "\n" ;&lt;br /&gt; str += "Salary:          " + sal.Text + "\n" ;&lt;br /&gt; str += "Experience:  " + exp.Text+ "\n" ;&lt;br /&gt; if( male.Checked == true )&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;  str1 = "male" ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 20px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt; else&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;  str1 = "female" ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 20px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt; str += "Gender:        " + str1 ;&lt;br /&gt; MessageBox.Show( str,"Emp Information" ) ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;}&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;The code is pretty much self-explanatory. What we are doing here is nothing but concatenating our string object str every time with the information. The Text field will contain text entered by the user. In the end we display the whole string using the static &lt;b&gt; Show( )&lt;/b&gt; method of the class &lt;b&gt; MessageBox&lt;/b&gt; . The first parameter is the text to be displayed while the second gives the caption of the message box.&lt;/span&gt;&lt;/p&gt; &lt;p align="center"&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;&lt;img src="http://www.kicit.com/freebies/csharp_net_source_code/WinForms/winfor2.jpg" width="168" border="0" height="152" /&gt;&lt;/span&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/343356482335293710-7571520407585749900?l=winformtutorial.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://winformtutorial.blogspot.com/feeds/7571520407585749900/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=343356482335293710&amp;postID=7571520407585749900' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/343356482335293710/posts/default/7571520407585749900'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/343356482335293710/posts/default/7571520407585749900'/><link rel='alternate' type='text/html' href='http://winformtutorial.blogspot.com/2008/08/simple-winform.html' title='Simple Winform'/><author><name>sandy</name><uri>http://www.blogger.com/profile/02503582209971047801</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-343356482335293710.post-4616638320871104946</id><published>2008-08-25T02:27:00.000-07:00</published><updated>2008-12-22T21:26:18.849-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Winform'/><title type='text'>Introduction to Winforms</title><content type='html'>&lt;span style="font-family:Arial;font-size:85%;"&gt;WinForms is a programming model used to create Windows Applications. .NET framework offers base classes for building Windows applications. Most of the functionality for these classes is in the &lt;b&gt; System.Windows.Forms&lt;/b&gt; namespace. The Visual Studio is used to design these applications visually, and to automatically generate the code.&lt;br /&gt; &lt;/span&gt; &lt;table width="100%" border="0" cellpadding="0" cellspacing="0"&gt;   &lt;tbody&gt;&lt;tr&gt;     &lt;td width="13%"&gt;&lt;hr size="4" color="#990033"&gt;&lt;/td&gt;     &lt;td width="16%" bg style="color:#cc0099;"&gt;&lt;span style="font-family:Comic Sans MS;font-size:100%;color:#ffffff;"&gt; Hello       World&lt;/span&gt;&lt;/td&gt;     &lt;td width="71%"&gt;&lt;hr size="4" color="#990033"&gt;&lt;/td&gt;   &lt;/tr&gt; &lt;/tbody&gt;&lt;/table&gt; &lt;p&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Lets look at the simplest traditional �hello world� example which will help us create our first Windows application in C#.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;Click on the File menu and select New | Project. Now select Windows Application from the Templates and click OK. You are now presented with an empty form.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;We can now drag in controls in our form. But we will not drag controls in our program. Rather we will only change the title of the form.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;To do so, right click on the form and select Properties. A Window named Properties pops up from the right. This will be a very useful window all the way long. Just change the Text field to �hello world�.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;When we open the code part, we see the following:&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;using System ;&lt;br /&gt;using System.Drawing ;&lt;br /&gt;using System.Collections ;&lt;br /&gt;using System.ComponentModel ;&lt;br /&gt;using System.Windows.Forms ;&lt;br /&gt;using System.Data ;&lt;br /&gt;&lt;br /&gt;namespace  Hello_world&lt;br /&gt;{&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 20px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt; public class myform : System.Windows.Forms.Form&lt;br /&gt; {&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;  private System.ComponentModel.Container components = null;&lt;br /&gt;&lt;br /&gt;  public myform ( )&lt;br /&gt;  {&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 60px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;   InitializeComponent ( ) ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;  }&lt;br /&gt;&lt;br /&gt;  protected override void Dispose( bool disposing )&lt;br /&gt;  {&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 60px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;   if ( disposing )&lt;br /&gt;   {&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 80px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;    if ( components != null )&lt;br /&gt;    {&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 100px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;     components.Dispose ( ) ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 80px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;    }&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 60px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;   }&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 60px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;   base.Dispose( disposing ) ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;  }&lt;br /&gt;&lt;br /&gt;  #region Windows Form Designer generated code&lt;br /&gt;  private void InitializeComponent ( )&lt;br /&gt;  {&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 60px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;   this.AutoScaleBaseSize = new  System.Drawing.Size ( 5,              13 ) ;&lt;br /&gt;   this.ClientSize = new System.Drawing.Size ( 160, 85 ) ;&lt;br /&gt;   this.Name = "myform" ;&lt;br /&gt;   this.Text = "Hello World" ;//change the form title&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;  }&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;  #endregion&lt;br /&gt; &lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;  [STAThread]&lt;br /&gt;  static void Main ( )&lt;br /&gt;  {&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 60px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;   Application.Run ( new  myform ( ) ) ;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 40px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;  }&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt 0pt 0pt 20px;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt; }&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0pt;" align="justify"&gt;&lt;span style="font-family:Arial;font-size:85%;color:#0000ff;"&gt;}&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;We have removed the comments for clarity.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;The form class is derived form the &lt;b&gt; System.Windows.Forms.Form&lt;/b&gt; class. In the constructor of this class a function called &lt;b&gt; InitializeComponents( )&lt;/b&gt; is called. Whatever changes we make in the Properties Window are reflected in form of code in this function. This code is written by the Windows Form Designer and is hence defined in a separate region.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;If we add controls, they are also initialized in this function with corresponding changed properties. So in our form only the text and name have been changed.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;In &lt;b&gt; Main( )&lt;/b&gt; the &lt;b&gt; Run( )&lt;/b&gt; method from the &lt;b&gt; Application&lt;/b&gt; class is called. To this method an instance of the form has been passed. The Application class provides static methods and properties to manage an application, such as methods to start and stop an application, to process Windows messages, and properties to get information about an application. The &lt;b&gt; Run( )&lt;/b&gt; method begins running a standard application message loop on the current thread.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Arial;font-size:85%;"&gt;When we press Ctrl+F5 to compile and run the program We get the following output.&lt;/span&gt;&lt;/p&gt; &lt;p&gt; &lt;/p&gt; &lt;p align="center"&gt;&lt;img src="http://www.kicit.com/freebies/csharp_net_source_code/WinForms/winfor1.jpg" width="168" border="0" height="112" /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/343356482335293710-4616638320871104946?l=winformtutorial.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://winformtutorial.blogspot.com/feeds/4616638320871104946/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=343356482335293710&amp;postID=4616638320871104946' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/343356482335293710/posts/default/4616638320871104946'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/343356482335293710/posts/default/4616638320871104946'/><link rel='alternate' type='text/html' href='http://winformtutorial.blogspot.com/2008/08/introduction-to-winforms.html' title='Introduction to Winforms'/><author><name>sandy</name><uri>http://www.blogger.com/profile/02503582209971047801</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
