c# code in net app

download c# code in net app

of 36

Transcript of c# code in net app

  • 8/9/2019 c# code in net app

    1/36

    6. The C# code in .Net ApplicationsIn the previous chapter, we had explored all that was required to be learnt and discoveredabout C#, in order to understand the code generated by the Framework. A crucial point thatscreams for attention is, that any application built in Visual Studio.Net with Project Type asVisual C#, eventually is converted into C# code. In this chapter, we will build applications inVisual Studio.Net, and at every stage thereon, we shall explain the code generated by the .NetFramework. We could have written the same code manually, but it certainly gives ourproductivity a boost, if the code is automatically generated by the system.

    Let us revert back to Visual Studio.Net, to create a new application. As always, click on menuFile - New - Project. In the dialog box, select Visual C# Project in Project Type pane, andWindows Application in the Template pane. Enter the name z10, and ensure that the locationis set to c:\mukhi. On pressing F5 to run the application, you will come across a completely

    blank window.

    When you navigate into the folder c:\mukhi\z10, you will encounter a large number of files,created by this framework. One of the many files created, is named as Form1.cs. This is similarto the .cs file, which was created in the previous chapter. Now, run the compiler using thecommand

    >Csc Form1.cs

    The compiler creates an exe file named Form1.exe, which when executed, reveals the samewindow, as is seen with the F5 command. The program Form1.cs has been generated by VisualStudio.Net, which internally calls the C# compile and creates the executable file. Pressing F5runs the executable. Let us now attempt at understanding the code, that gets generated in

    Form1.cs.

    Form1.csusing System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;

    namespace z10{

    /// /// Summary description for Form1./// public class Form1 : System.Windows.Forms.Form{

    /// /// Required designer variable./// private System.ComponentModel.Container components = null;

    public Form1()

  • 8/9/2019 c# code in net app

    2/36

    {//// Required for Windows Form Designer support//InitializeComponent();

    //

    // TODO: Add any constructor code after InitializeComponent call//

    }

    /// /// Clean up any resources being used./// protected override void Dispose( bool disposing ){

    if( disposing ){

    if (components != null){

    components.Dispose();

    }}base.Dispose( disposing );

    }

    #region Windows Form Designer generated code/// /// Required method for Designer support - do not modify/// the contents of this method with the code editor./// private void InitializeComponent(){

    this.components = new System.ComponentModel.Container();this.Size = new System.Drawing.Size(300,300);this.Text = "Form1";

    }#endregion

    /// /// The main entry point for the application./// [STAThread]static void Main(){

    Application.Run(new Form1());}

    }

    }

    We hope to be able to refresh your memory by reiterating that, the above program has beenwritten by the C# compiler, and not by us. The file reveals a large number of statementsbeginning with the term 'using'. For those of you who tuned in late, the 'using' statements areinserted to ease the typing effort. The name of a namespace is provided after the 'using'keyword, so that the namespace is not required to be inserted before each 'class.function name'combination. As the project has been named z10, the entire C# program is depicted asenclosed within the namespace of z10. Since the solitary role of a namespace is to ensure that

  • 8/9/2019 c# code in net app

    3/36

    the class names in one project, do not clash with class names in the other projects, it cansafely be overlooked.

    You may recollect that in the last chapter, we had gathered that any line beginning with threeslashes, is an XML comment. This program appears very large, due to the comments insertedat every possible nook and corner. More often than not, comments are not of much utility,

    since they are computer generated; and thus, lack the essential human touch. In future, wewill strip-off all comments, before displaying the code.

    A class known as Form1, which is derived from the class Form, contains all the code requiredfor the current project. In spite of employing the term 'using', which includesSystem.Windows.Forms, the code generated, still contains the full name of the class. It isabsurd and illogical, but then; computers are not expected to generate code that makes perfectsense. In the first place, an instance variable called 'components' in the class, has beeninitialized to 'null', which represents no value. The 'private' modifier limits the access only tothe members of the current class.

    The Run function, located in the Main function, is called after passing it an object, which is aninstance of Form1. The newly created object calls the constructor of the Form1 class, and then,

    it calls the Run function. The constructor simply calls a function named InitializeComponent.

    But prior to this, study the square brackets above the Main function closely. It represents an'attribute'. Attributes have a range of functions, which undertake certain tasks, such as,determining the wherewithal of generating the code with the aid of the compiler, ordocumenting the classes in the executable file, etc. We shall not venture into the details of theattribute [STAThread], however, for those burning with curiosity, it suffices to say that the fullform of STA is Single Threaded Apartment. It implies that, once a window is created in athread, it will not be permitted to switch to another thread. If you are unable to comprehendthe finer nuances of the above statement, then cheer up, you have company!It is in this function, that all the form widgets get initialized. We employ this function, only inthe event of initializing the object components. The computer program that generated the code,prefixed all the instance variables with the term 'this'. In this case, eliminating the term 'this',

    does not affect the outcome. The class Form has a large number of instance variables, such asSize and Text, which we are entitled to be used in the class Form1.

    The Size variable or Size property, determines the size of the initial window, while the Textproperty decides the title of the window. The 'this' keyword is optional. A point to be noted isthat, all properties have a default value. No sooner do we change a single property in theProperty window, a line gets inserted in the InitializeComponent function. The Size property isinitialized to a Size object, which is passed the width and height of the window in pixels. Thus,a GUI is used to build applications and all actions get converted into C# code.

    The next function called Dispose, can only be called by the system, as and when it is required.A programmer does not enjoy the facility of calling it manually. We are completely aware aboutthe circumstances under which a constructor gets called. Further, we also know unequivocally,

    that the Dispose function gets called, when the resources in the program are to be disposed off.But, we are not aware as to when this disposal takes place; in other words, when is an objectgoing to die. In C#, unlike C++, there is no control at all, over the termination of an object. TheGarbage Collector is solely responsible for removing an object from memory; thereby, shieldingthe programmer from carrying out any memory management chores. The Dispose function canalso be ignored, since it merely calls the Dispose function from the Components object and thebase class. Hence, the use of keyword 'base' can be explained. In our next display, we do notintend to expose you to the Dispose function any longer.

  • 8/9/2019 c# code in net app

    4/36

    Now, its time we got back to Visual Studio.Net. Double click on the form. As is always the case,the Code Painter gets displayed. Now, press Ctrl+Home to move to the beginning of the page, orscroll up till the first line is visible.

    Screen 6.1

    Screen 6.1 presents the code that is displayed in our window. Here, we discern a list of 'using'statements and a minus sign, extending over an entire span of six 'using' statements. Now,click on the minus sign, and observe your screen change its display, as shown in screen 6.2.

    Screen 6.2

    To our utter astonishment, all the 'using' statements vanish, and what is remnant is a plussign and the term 'using', with a series of dots as residues. Thus, the editor is conscious of howit has to work with the C# language. It is not merely a word processor, but a word processorthat knows and discerns the C# programming language, better than we do. The words, alsoknown as keywords, that are a part of the C# language, are depicted in a different color. Also,every separate entity begins with a minus sign. Now, click on the minus sign in front of class.Screen 6.3 displays the Code Painter.

  • 8/9/2019 c# code in net app

    5/36

    Screen 6.3

    Clicking on the minus sign of the class, compresses it into a series of dots. This signifies that itis the class that encapsulates all the code. We can compress any entity in C# and thereby,remain at the level of detail we are comfortable with. With a small screen, we need to be veryselective about the level of detail that can be displayed. Now, incorporate a button onto theForm Design and double click on it. The addition of a button, results in a variation in the codealso. The program is shown below.

    Form1.csusing System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;

    using System.Data;

    namespace z10{

    public class Form1 : System.Windows.Forms.Form{

    private System.Windows.Forms.Button button1;private System.ComponentModel.Container components = null;public Form1(){

    InitializeComponent();}protected override void Dispose( bool disposing ){

    if( disposing ){

    if (components != null){

    components.Dispose();}

    }base.Dispose( disposing );

    }#region Windows Form Designer generated code

  • 8/9/2019 c# code in net app

    6/36

    private void InitializeComponent(){

    this.button1 = new System.Windows.Forms.Button();this.SuspendLayout();this.button1.Location = new System.Drawing.Point(40, 64);this.button1.Name = "button1";this.button1.TabIndex = 0;

    this.button1.Text = "button1";this.button1.Click += new System.EventHandler(this.button1_Click);this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);this.ClientSize = new System.Drawing.Size(292, 273);this.Controls.AddRange(new System.Windows.Forms.Control[] {

    this.button1});this.Name = "Form1";this.Text = "Form1";this.Load += new System.EventHandler(this.Form1_Load);this.ResumeLayout(false);

    }#endregion[STAThread]static void Main()

    {Application.Run(new Form1());

    }private void Form1_Load(object sender, System.EventArgs e){

    }

    private void button1_Click(object sender, System.EventArgs e){

    }}

    }

    The above program displays the code that is generated after the button is clicked. A largeportion of the code remains the same, as was noticed in the earlier program. Therefore, weshall desist from harping upon it any longer. Besides, from now onwards, only relevant parts ofthe code will be revealed, to avoid redundancy and confusion, and also to conserve paper.

    The newly added button in the form is called button1. Therefore, an instance variable iscreated with the same name, i.e. button1. The data type is Button, which is a class in theForms namespace. Any GUI code that needs to be executed before the window is displayed, isalways placed in the InitializeComponent function.

    Any line that originates with the # symbol, is known as a pre-processor directive. The pre-processor is a program that executes before the C# compiler commences its work. The #regioncommand displays a minus sign. If we click on it, the region which extends from this sign uptothe #endregion, collapses. The words displayed in the box, remain the same, as shown after the#region directive. This is amply evident in screen 6.4.

  • 8/9/2019 c# code in net app

    7/36

    Screen 6.4

    The InitializeComponent function contains a large number of statements that deal with theButton object. First and foremost, an instance of the button object is created, with the help ofthe 'new' keyword. The function SuspendLayout, as the name itself suggests, does not executeany code that deals with placement of controls or layout, until the function ResumeLayout iscalled. Invariably, this is the very last function to be called. The Location property is initializedto the position where the control is placed, and the name is the text displayed on the button.

    The Click property is called a 'delegate' in C#, which is associated with a function that getscalled, each time the button is clicked. The name of the function is passed as a parameter tothe constructor of class named EventHandler.

    There could be multiple buttons present in a form. Each of them would necessitate a separatefunction, which is to be called when the button is clicked. Therefore, the approach adopted bythe programmers for determining the function name, is as follows: The name of the button,

    followed by an underscore, followed by the word 'Click'. In this manner, distinct names areassigned to each of the functions, since the names of the controls are always unique.

    All this discussion proves to be inefficacious, unless we display a button on the screen. Toachieve this, the services of the AddRange function, belonging to the Controls object in theForms class, are employed. This function accepts an array of the Control datatype, which holdswidgets to be displayed. In this case, there is only a single button; however, in real life, therecould be numerous controls. This function is used to add multiple controls, simultaneously.

    Thus, by using the Graphical interface, life becomes a lot more simpler, since we can eschewthe drudgery of manually entering a horde of new changes, embodied in the form. Imagine howcumbersome it would be to write and re-write the code, each time the button is shifted, even alittle. A new value would have to be assigned to the Location property, every time the button is

    moved.Each time we double click on the Form, we are directed to a function called Form1_Load. Thisis due to the fact that the property Load, belonging to the Form class, is assigned this value.

    The code in the above function, is executed before the window is displayed, but after theexecution of the function InitializeComponent.

    Everything about Visual Studio.Net is dynamic. Any change incorporated in the value of aproperty in the Code Painter, gets reflected instantaneously. Locate the property called Text inthe Code Painter, which is shown as:

  • 8/9/2019 c# code in net app

    8/36

    this.button1.Text = "button1";

    Now, change it to the following:

    this.button1.Text = "vijay";

    Then, click on the Design Tab. Some processing takes place for a while, before the Form isactivated. In the form, the Text property changes to 'vijay' immediately. Now, add the followingline in the function InitializeComponent, just below the Location property:

    this.button1.Size = new System.Drawing.Size(72, 80);

    The moment we click on the Design Tab, the size of the button undergoes a transformation.The Form Design and the Properties window also change. In the Properties window, the valueof Size changes immediately. In the Design, delete the button by selecting it, and then, pressingthe Delete key. Switch to the Code Painter, and you will witness that, all references to thebutton named button1, have been removed from the code, including the ones entered by us.However, the function button1_Click, does not get erased. There appears to be a bug in theBeta copy of the product. We presume that this bug will be fixed in the final copy of theproduct.

    We have not saved anything so far. We would advise you against changing anything in theCode Painter. Instead, you may use the Properties Window for this purpose. The C# compilercouldn't care less about who has actually entered the code. It merely validates the syntax andcreates an exe file. This exe file is then executed, using the F5 option.

    The next example relates to the code generated by the Framework when data is displayed froma database. So, select the Data tab from the toolbox, and bring an OleDbDataAdapter into theform. In case you are unable to recollect the steps, peruse through Chapter 2 of this book.Select the data connection of VMUKHI.Northwind.dbo, and insert the following SQL statement:

    SELECT CustomerID, CompanyName, ContactName FROM Customers

    In the Form, the OleDbDataAdapter control also adds another control calledoleDbConnection1. Switch to the Code Painter and observe the modifications made to the code.

    private System.Data.OleDb.OleDbDataAdapter oleDbDataAdapter1;private System.Data.OleDb.OleDbCommand oleDbSelectCommand1;private System.Data.OleDb.OleDbCommand oleDbInsertCommand1;private System.Data.OleDb.OleDbCommand oleDbUpdateCommand1;private System.Data.OleDb.OleDbCommand oleDbDeleteCommand1;private System.Data.OleDb.OleDbConnection oleDbConnection1;

    At first, six instance variables are created. Apart from the oleDbDataAdapter1 andoleDbConnection1, four more variables are created, each belonging to the data type

    OleDbCommand. Now, expand the region 'Windows Form Designer generated code', and viewthe revised code in the InitializeComponent function.

    private void InitializeComponent(){this.oleDbDataAdapter1 = new System.Data.OleDb.OleDbDataAdapter();this.oleDbSelectCommand1 = new System.Data.OleDb.OleDbCommand();this.oleDbInsertCommand1 = new System.Data.OleDb.OleDbCommand();this.oleDbUpdateCommand1 = new System.Data.OleDb.OleDbCommand();this.oleDbDeleteCommand1 = new System.Data.OleDb.OleDbCommand();

  • 8/9/2019 c# code in net app

    9/36

    this.oleDbConnection1 = new System.Data.OleDb.OleDbConnection();this.oleDbDataAdapter1.DeleteCommand = this.oleDbDeleteCommand1;this.oleDbDataAdapter1.InsertCommand = this.oleDbInsertCommand1;this.oleDbDataAdapter1.SelectCommand = this.oleDbSelectCommand1;this.oleDbDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {new System.Data.Common.DataTableMapping("Table", "Customers", newSystem.Data.Common.DataColumnMapping[] {

    new System.Data.Common.DataColumnMapping("CustomerID", "CustomerID"),newSystem.Data.Common.DataColumnMapping("CompanyName", "CompanyName"), newSystem.Data.Common.DataColumnMapping("ContactName", "ContactName")})});this.oleDbDataAdapter1.UpdateCommand = this.oleDbUpdateCommand1;this.oleDbSelectCommand1.CommandText = "SELECT CustomerID, CompanyName, ContactNameFROM Customers";this.oleDbSelectCommand1.Connection = this.oleDbConnection1;this.oleDbInsertCommand1.CommandText = "INSERT INTO Customers(CustomerID, CompanyName,ContactName) VALUES (?, ?, ?);"+"SELECT CustomerID, CompanyName, ContactName FROM Customers WHERE (CustomerID = ?)";this.oleDbInsertCommand1.Connection = this.oleDbConnection1;this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("CustomerID",System.Data.OleDb.OleDbType.WChar, 5, System.Data.ParameterDirection.Input, false,((System.Byte)(0)), ((System.Byte)(0)), "CustomerID", System.Data.DataRowVersion.Current, null));

    this.oleDbInsertCommand1.Parameters.Add(newSystem.Data.OleDb.OleDbParameter("CompanyName", System.Data.OleDb.OleDbType.VarWChar, 40,System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)),"CompanyName", System.Data.DataRowVersion.Current, null));this.oleDbInsertCommand1.Parameters.Add(newSystem.Data.OleDb.OleDbParameter("ContactName", System.Data.OleDb.OleDbType.VarWChar, 30,System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)),"ContactName", System.Data.DataRowVersion.Current, null));this.oleDbInsertCommand1.Parameters.Add(newSystem.Data.OleDb.OleDbParameter("Select_CustomerID", System.Data.OleDb.OleDbType.WChar, 5,System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CustomerID",System.Data.DataRowVersion.Current, null));this.oleDbUpdateCommand1.CommandText = @"UPDATE Customers SET CustomerID = ?,CompanyName = ?, ContactName = ? WHERE (CustomerID = ?) AND (CompanyName = ?) AND(ContactName = ? OR ? IS NULL AND ContactName IS NULL); SELECT CustomerID, CompanyName,ContactName FROM Customers WHERE (CustomerID = ?)";this.oleDbUpdateCommand1.Connection = this.oleDbConnection1;this.oleDbUpdateCommand1.Parameters.Add(newSystem.Data.OleDb.OleDbParameter("CustomerID", System.Data.OleDb.OleDbType.WChar, 5,System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CustomerID",System.Data.DataRowVersion.Current, null));this.oleDbUpdateCommand1.Parameters.Add(newSystem.Data.OleDb.OleDbParameter("CompanyName", System.Data.OleDb.OleDbType.VarWChar, 40,System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)),"CompanyName", System.Data.DataRowVersion.Current, null));this.oleDbUpdateCommand1.Parameters.Add(newSystem.Data.OleDb.OleDbParameter("ContactName", System.Data.OleDb.OleDbType.VarWChar, 30,

    System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)),"ContactName", System.Data.DataRowVersion.Current, null));this.oleDbUpdateCommand1.Parameters.Add(newSystem.Data.OleDb.OleDbParameter("Original_CustomerID", System.Data.OleDb.OleDbType.WChar,5, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)),"CustomerID", System.Data.DataRowVersion.Original, null));this.oleDbUpdateCommand1.Parameters.Add(newSystem.Data.OleDb.OleDbParameter("Original_CompanyName",System.Data.OleDb.OleDbType.VarWChar, 40, System.Data.ParameterDirection.Input, false,

  • 8/9/2019 c# code in net app

    10/36

    ((System.Byte)(0)), ((System.Byte)(0)), "CompanyName", System.Data.DataRowVersion.Original,null));this.oleDbUpdateCommand1.Parameters.Add(newSystem.Data.OleDb.OleDbParameter("Original_ContactName",System.Data.OleDb.OleDbType.VarWChar, 30, System.Data.ParameterDirection.Input, true,((System.Byte)(0)), ((System.Byte)(0)), "ContactName", System.Data.DataRowVersion.Original,null));

    this.oleDbUpdateCommand1.Parameters.Add(newSystem.Data.OleDb.OleDbParameter("Original_ContactName1",System.Data.OleDb.OleDbType.VarWChar, 30, System.Data.ParameterDirection.Input, true,((System.Byte)(0)), ((System.Byte)(0)), "ContactName", System.Data.DataRowVersion.Original,null));this.oleDbUpdateCommand1.Parameters.Add(newSystem.Data.OleDb.OleDbParameter("Select_CustomerID", System.Data.OleDb.OleDbType.WChar, 5,System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CustomerID",System.Data.DataRowVersion.Current, null));this.oleDbDeleteCommand1.CommandText = "DELETE FROM Customers WHERE (CustomerID = ?) AND(CompanyName = ?) AND"+ "(ContactName = ? OR ? IS NULL AND ContactName IS NULL)";this.oleDbDeleteCommand1.Connection = this.oleDbConnection1;this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("CustomerID",System.Data.OleDb.OleDbType.WChar, 5, System.Data.ParameterDirection.Input, false,

    ((System.Byte)(0)), ((System.Byte)(0)), "CustomerID", System.Data.DataRowVersion.Original, null));this.oleDbDeleteCommand1.Parameters.Add(newSystem.Data.OleDb.OleDbParameter("CompanyName", System.Data.OleDb.OleDbType.VarWChar, 40,System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)),"CompanyName", System.Data.DataRowVersion.Original, null));this.oleDbDeleteCommand1.Parameters.Add(newSystem.Data.OleDb.OleDbParameter("ContactName", System.Data.OleDb.OleDbType.VarWChar, 30,System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)),"ContactName", System.Data.DataRowVersion.Original, null));this.oleDbDeleteCommand1.Parameters.Add(newSystem.Data.OleDb.OleDbParameter("ContactName1", System.Data.OleDb.OleDbType.VarWChar, 30,System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)),"ContactName", System.Data.DataRowVersion.Original, null));this.oleDbConnection1.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;UserID=sa;Initial Catalog=Northw" +"ind;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation" +" ID=VMUKHI;Use Encryption for Data=False;Tag with column collation when possible" +"=False";this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);this.ClientSize = new System.Drawing.Size(292, 273);this.Name = "Form1";this.Text = "Form1";this.Load += new System.EventHandler(this.Form1_Load);}

    The code for the function InitializeComponent has been inserted without us having to makeany changes at all. By now, you would have realized, as to why we are so fascinated with

    Visual Studio.Net. It generates all the code required for a certain task, without a single error.Let us spend a little time, trying to construe the above code. As there are six instance variables,each of them will have a new statement, which will initialize them.

    The first variable is of type OleDbDataAdapter class, which is used to fetch data from adatabase. Then, we need SQL commands, which will help us in working with the records froma table in the database. Thus, the 4 property statements of type OleDbCommand,oleDbSelectCommand1, oleDbInsertCommand1, oleDbUpdateCommand1 and

  • 8/9/2019 c# code in net app

    11/36

    oleDbDeleteCommand1, represent SQL statements, which are to be used while working withrecords.

    The columns in our table, require to be mapped to the columns in the data source. In order toaccomplish this, the TableMappings property of type DataTableMappingCollection, is employed.

    This class contains a function called AddRange, which accepts an array of DataTableMapping

    objects. A DataTableMapping class has a constructor, which accepts three parameters. Thefirst parameter is a string, which represents a Table. The second parameter is Customers,which is the name of the table in the dataset, to map to. The third parameter is an array ofColumn names.

    The column names furnished in the last parameter are represented with the help of aDataColumnMapping class. The constructor to this class is passed two parameters. The first isthe name of the column in the data source, while the second is the name of the column in thedataset to map to. We have maintained both of these identical, although they could haveeasily varied.

    The SQL statement that we have written, generates this AddRange function. It associates ormaps the three chosen columns, with the columns in the Customers table. The

    oleDbSelectCommand1 object requires an SQL select statement, which will determine the datato be retrieved. The CommandText property is therefore, initialized to the Select statemententered earlier in the Query Builder. The Connection property is initialized to theOleDbConnection control, for each of the select, update, delete or insert objects.

    For inserting data, the SQL statement has a? symbol, or a placeholder to hold text, which is tobe entered at run time. The syntax starts with the reserved word 'Insert into', followed by thename of the table, followed by the field names, followed by the reserved word Values, andfinally, followed by a set of brackets, containing the actual data. This data can also be theresultant data of a Select statement. Either of the three, i.e. ? or placeholder or parameter, isfilled with text supplied by the user at runtime. This is done for reasons of efficiency. The Insertor Select statements are first parsed, and then, verified for various types of errors. This is aprotracted and time-consuming exercise. Therefore, the product of this action is stored for

    reuse, thereby speeding things up. Thus, by the use parameters, execution can be speeded up,since the database merely needs add the data, without checking the syntax. Three parametersare to be added to the Insert object, since there are three column names, viz. CustomerID,CompanyName and ContactName. The rules for adding a parameter remain the same, whileimplementing all the three parameters.

    The Parameters collection takes an OleDbParameter object, whose constructor accepts 10parameters, which are as follows:

    The first parameter is the name of the parameter, and in our case, it is termed asCustomerID. The second parameter is the data type of the parameter, which is of type OleDbType.It may be any one of the numerous data types that are available. One such data type iswchar, which is a series of Unicode characters, ending in a null. It maps to a string data

    type in C#. The third parameter is the size or width of the column, which is 5 characters long. The fourth parameter, of type ParameterDirection, can contain only four values,which specify whether the parameter is used for input or output; or both; or for thereturn value of some code being executed on the server. The fifth parameter is a Boolean, which accepts a value of either True or False. In adatabase, you can specify whether a field can contain a Null value or not. Null cannotbe equated to zero, since it specifically implies that the field is bereft of any valuewhatsoever. A value of False necessitates the presence of a value in this field.

  • 8/9/2019 c# code in net app

    12/36

    The sixth parameter stands for 'precision', which indicates the exact number ofdecimal places that a number can possibly have. The seventh parameter is the 'scale' parameter, which is also responsible for thenumber of decimal places that the value can hold. The eighth parameter is the name of the source column in the table. The ninth parameter is of type DataRowVersion, and specifies the version of the

    DataRow object being retrieved. The type Current represents the current or presentvalue. It can have three other values, viz. Default, Original and Proposed. The tenth parameter is Null. It stands for the value of any of the parameters. We mayuse any data type, to assign a value to the parameter.

    It would be so cumbersome if we had to write the above code by hand, for each of theparameters in the Insert statement. This is surely a dreary job! Since someone has to do this

    job, why not let the computer do it?

    The 'update' command necessitates a similar treatment. So, we use the Update statement,which starts with the reserved word Update, followed by the name of the table i.e. Customers,and the 'set' keyword, which specifies the fields that need to be updated. There are threeplaceholders or ? signs, since three fields have been chosen. The term 'where' is like a filter,

    since in its absence, the update statement will update all records in the database. The eightparameters that need to be annexed for the Update statement, are responsible for theextremely lengthy code. The Delete statement is considerably more tractable than the codespecified above. It starts with the Delete keyword, followed by the name of the table, and a'where' condition, which filters the number of records. The four placeholders require fourparameter statements.

    Next comes the Connection object, oleDbConenction1. It is this particular object, whichunderstands how to aptly communicate with a database. The data that specifies how tocommunicate with a database, is passed in the ConenctionString property. The Connectionstring starts with the word 'provider', with the name of the database that we wish to talk to,followed by a semi-colon, which acts as a separator. Then, we have the User ID of 'sa', since 'sa'was entered in the dialog box. Next, we have the Catalog as the name of the database,

    NorthWind. We shall explore the others, a little later.

    The visible controls get added to the Forms collection, whereas the invisible controls do not.The only difference between a visible control and an invisible control is that, the former showsup in the form, while the latter shows up at the bottom. These concepts are divulged only afterthe code that is generated, has been deciphered thoroughly. Otherwise, we shall just begroping in the dark. Thus, we endeavor to understand the code from the absolutely basicfundamentals.

    Next, we generate the Data Set object, using the menu option Data- Generate Dataset, and wename the Datatset as 'ddd'. The screen 6.5 represents the dataset dialog box.

  • 8/9/2019 c# code in net app

    13/36

    Screen 6.5

    Once this is done, switch to the Code Painter to view the new sets of commands. An instancevariable, called ddd1, gets created with the data type of z10.ddd. This is because, we namedour DataSet as 'ddd', and the dataset object in Visual Studio.Net was named 'ddd1'.

    The next thing that we need to do is, to discover where the class ddd has been created. It doesnot show up in the current file. We plan to unveil this class mystery, in just a short while.

    But for now, we will take a look at the code of the dataset, which was created in the functionInitializeComponent.

    private z10.ddd ddd1;private void InitializeComponent (){

    this.ddd1 = new z10.ddd();((System.ComponentModel.ISupportInitialize)(this.ddd1)).BeginInit();this.ddd1.DataSetName = "ddd";this.ddd1.Locale = new System.Globalization.CultureInfo("en-US");this.ddd1.Namespace = "http://www.tempuri.org/ddd.xsd";}

    As always, a new instance is created, and thereafter, a function called BeginInit is called. Thisfunction is present in the interface IsupportInitialize, which explains the use of the castoperator. This function merely enlightens the whole world with the news, that the object isabout to initialize itself. Unless the program reaches the EndInit function, initialization will notbe completed.

    This is akin to fixing a 'Do Not Disturb' sign outside your door. The cast that we have above, is

    really unnecessary, since the DataSet class implements the interface. Whenever computerprograms write code, they are extremely specific. The DataSetName is set to 'ddd', since weopted for this name in the dialog box, during the creation of the dataset. The Locale property isused to compare strings present in the table. The Namespace property is set to the name of anxsd file. File ddd.xsd is shown below.

    ddd.xsd

  • 8/9/2019 c# code in net app

    14/36

    xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" attributeFormDefault="qualified"elementFormDefault="qualified">

    This file displays a large number of tags, which we shall explicate while discussing XML. Theline of code in which the table name Customers is stored, is the most significant one. Withinthe tag sequence, the three field names are present along with their data types. There is also anattribute called PrimaryKey, which specifies that the CustomerID field is unique. The aboveprogram named Form1.cs, cannot be compiled at this stage, since it does not contain any codefor the ddd class. The only way out is, to create this class from the xsd file. So, we use the xsdprogram for this purpose. We run it as follows:

    C:\mukhi\z10>xsd /d /n:z10 ddd.xsd

    Output

    Microsoft (R) Xml Schemas/DataTypes support utility[Microsoft (R) .NET Framework, Version 1.0.2914.16]Copyright (C) Microsoft Corp. 1998-2001. All rights reserved.Writing file 'C:\mukhi\z10\ddd.cs'.

    The /d option creates a class named ddd, derived from DataSet. The /n option places all thecode in the namespace z10. Finally, a file called ddd.cs gets created, from which, we haveextracted the initial few lines. Remember that, none of this 10K sized code, has been written byus.

    namespace z10{public class ddd : System.Data.DataSet{

    Now that the class ddd is available, compile the Form program, using the command

    >csc Form1.cs ddd.cs

    The compiler can be passed as many C# program files as you desire. We are not out of thewoods yet, since we have to introduce a DataGrid, which shall display all the data.

  • 8/9/2019 c# code in net app

    15/36

    The moment we incorporate a DataGrid object, an instance variable named dataGrid1 getscreated.

    private System.Windows.Forms.DataGrid dataGrid1;

    Double click on the datagrid and you will see the following code in the function

    InitializeComponent

    this.dataGrid1 = new System.Windows.Forms.DataGrid();((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();this.dataGrid1.DataMember = "";this.dataGrid1.Location = new System.Drawing.Point(56, 40);this.dataGrid1.Name = "dataGrid1";this.dataGrid1.TabIndex = 0;this.dataGrid1.Navigate += newSystem.Windows.Forms.NavigateEventHandler(this.dataGrid1_Navigate);this.Controls.AddRange(new System.Windows.Forms.Control[] {this.dataGrid1});((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();

    private void dataGrid1_Navigate(object sender, System.Windows.Forms.NavigateEventArgs ne)

    {}

    The Navigate event gets fired, whenever we move into a new table in the data grid, since theDataMember property is blank by default. The whole world appears to be pre-occupied withcalling the BeginInit and EndInit functions. They all behave in a similar manner.

    We finally set DataSource to ddd1 and DataMember to Customers. These are the two mostimportant properties in the Properties window.

    this.dataGrid1.DataSource = this.ddd1;this.dataGrid1.DataMember = "Customers";

    The Fill function is placed in the Form1_Load function before running the program, as thedataset needs to be filled up with data.

    private void Form1_Load(object sender, System.EventArgs e){oleDbDataAdapter1.Fill(ddd1);}

    The next application will selectively retrieve data from the customers table. To be precise, wewill provide a customer number, and instantly, specific details about that customer will bedisplayed. But prior to this, we intend to display a single record from the customers table inthe textboxes.

    Save all the files, and close the current Solution. Then, click on File - New - Project, and chooseVisual C# Project in the first pane and Windows Application in the second pane. Assign the

    name z14 to the project, and locate it in C:\mukhi. Then, click on the OK button. Do ensurethat the Properties and the ToolBox windows are visible.

    Click on the Data tab in the toolbox, and then usher-in the OleDbDataAdapter control. Thisactivates the wizard. On the first screen, simply click on the Next button. To obtain the name ofData Connection, click on the drop down listbox, and select the one named Northwinddatabase. Then, click on the Next button, leaving the SQL statements selected; and then, writethe following SQL statement:

  • 8/9/2019 c# code in net app

    16/36

    SELECT CustomerID, CompanyName, ContactName FROM Customers

    Finally, click on the Finish button. The two invisible objects get created in the lower pane. Weare also aware of the fact that, great many lines of code have been generated.

    Click on menu Data - Generate Dataset. Change the name of the dataset to ddd, and then,

    click on OK. Thereafter, drag-and-drop three textboxes onto the form, from the ToolBox in theWindows Form tag.

    Select the first textbox, and in the Properties Window, click on the plus sign in front ofDataBindings. This will bring up the DataBindings options. Click on the drop down listbox forthe property Text. This will reveal the dataset ddd1, with a plus sign in front of it. A datasetcontains a large number of tables. Hence, we see a plus sign along with it, in the PropertiesWindow. Click on the plus sign with ddd, to display a list of tables present in it. Since there isonly one table present, we see only the Customers table listed, but with a plus sign. Every tablecontains columns. Clicking on the plus sign, will display the three column names contained incustomers. Select CustomerID, as shown in screen 6.6.

    Screen 6.6

    By selecting the column CustomerID for the first textbox, we confine the display of all valuesrelated to CustomerID, to this particular textbox alone. Follow the same procedure to selectCompanyName for the second textbox, and finally, to select ContactName for the third textbox.After effecting this, double click on the form, to enter the Fill function in the functionForm1_Load.

    oleDbDataAdapter1.Fill(ddd1);

    Then, press F5 to run the application.

  • 8/9/2019 c# code in net app

    17/36

    Screen 6.7

    In screen 6.7, the three fields of the first record are displayed in the three textboxes.

    Let us now introduce a button from the Windows Form tab so that the record pointer moves tothe next record when clicked on the button. Change the text property in the Properties windowto Next, and then, double click on the button. You will be transported to the functionbutton1_Click. Now, enter the following line of code:

    BindingContext[ddd1, "Customers"].Position += 1;

    The Form class has a property called BindingContext, of data type BindingContext. Thisproperty, in turn, has an Indexer that accepts two parameters. As was explained in theprevious chapter, an Indexer looks like an array, walks like an array and talks like an array;but it is not an array!

    The two parameters that this Indexer accepts are: The name of the dataset, i.e. ddd1. The name of the table, which is within that DataSet, i.e. Customers.

    This indexer returns a BindingManagerBase object, which has a property called Position. Thisproperty contains the current record pointer.

    The record pointer or active record, is an abstract entity, which marks a single row of data inthe dataset that becomes visible to everyone. The value contained in the Position propertyactivates the specific row in the dataset. The initial value starts at zero.

    Each time we click on the button, the value is incremented by 1, using the += syntax.

  • 8/9/2019 c# code in net app

    18/36

    Screen 6.8

    The above code can be re-written effortlessly, as follows:

    BindingManagerBase a;a= BindingContext[ddd1, "Customers"];a.Position = a.Position + 1;

    This code is a simplified version of the earlier one. However, it is infested with a small bug. Ifthe value of the Position parameter exceeds the number of records, no error is generated. Insuch a situation, the last record will be displayed. So, how do we redress this problem?

    Introduce another button, and change the Text property to Prev. Then, double click on thisbutton, and in the Code Generator for the function button2_Click, add the following line ofcode.

    BindingContext[ddd1, "Customers"].Position -= 1;

    Now, on running the application, two buttons will be visible, as shown in screen 6.9.

  • 8/9/2019 c# code in net app

    19/36

    Screen 6.9

    Each time we click on the Next button, the record pointer moves to the next record; whereas,every time we click on the Prev button, it moves to the previous record.

    Thus, each and every record can be accessed sequentially, in the forwards or backwards

    direction.

    Now, we add one more button, which moves the record pointer directly to the first record.Change its text to 'First', and then, double click on it, to add the following line of code:

    BindingContext[ddd1, "Customers"].Position = 0;

    Once this is done, build the project, and press F5 to run the program. Click on the Next buttona couple of times, and then, click on the button labeled First. On doing so, the record pointer

    jumps directly to the first record, and it gets displayed. This occurs on account of the fact that,when the Position property is set to zero, the first row in the dataset becomes the active row.Finally, select a button in the Toolbox, and change its Text property to Last. Then, add thefollowing lines of code to it:

    int i;i = BindingContext[ddd1, "Customers"].Count;BindingContext[ddd1, "Customers"].Position = i-1;

    The BindingManagerBase class has a member called Count, which provides the total count ofrows or record sets available in the dataset. The variable i is set to the value of this property.

    Then, the Position parameter is initialized to one less than the value of i, since the index of thePosition parameter begins with a value of zero. Thus, if we have 10 rows, the value of the countproperty will be 10. The index value of the first record will be 0, and that of the last record willbe 9.

    There is yet another feature remaining, which is a textbox that reveals the current position, orthe current record number, in the dataset. Each time the Position property changes, the newrecord number must be displayed. In fact, the position changes whenever any button isclicked. The code required to display the new record numbers, remains unchanged. So,instead of repeating the same code four times, once for each of the four different buttons, weshall enclose this code within a function. This function will then be called by each of the fourbuttons.

    So, select the textbox from the Toolbox window. The textbox will be named textBox4.

    Then, enter the function abc, as shown below, after the last function. The four buttons shallnow call this function.

  • 8/9/2019 c# code in net app

    20/36

    Screen 6.10

    void abc(){int cnt,pos;cnt = BindingContext[ddd1, "Customers"].Count;pos = BindingContext[ddd1, "Customers"].Position;pos = pos+1;textBox4.Text = "Record No " + pos + " of Record " + cnt;}

    Also, call the abc function after the Fill function in Form_Load.

    private void Form1_Load(object sender, System.EventArgs e){

    oleDbDataAdapter1.Fill(ddd1);abc();

    }

    In the function abc, we initialize the variables 'cnt' and 'pos' to the total number of rows andcurrent record position, respectively. The 'pos' variable must be increased by one, since theindex is zero-based, i.e. it starts at zero.

    The Text property of the textbox named textBox4, is initialized to the new values. The stringsare placed within double quotes, whereas, the integers are automatically converted to strings,so that no extra effort is required on our part. The plus sign is employed to join two strings,and is known as the 'string concatenation operator'.

  • 8/9/2019 c# code in net app

    21/36

    Screen 6.11

    There are numerous records in the dataset. So, we shall now essay towards displaying onlythose records that meet certain criteria. To do so, select the OleDbDataAdapter object in theform, and then, for the property SelectCommand, click on the 'plus' sign, to view the Dynamicproperties.

    When the button for the property CommandText having 3 dots is clicked, it displays the screenas reflected below.

    Screen 6.12

    Here, we notice the same screen that was displayed in the wizard. The SQL statement ismodified to the following:

    SELECT CustomerID, CompanyName, ContactName FROM Customers WHERE City = ?

    Addition of the WHERE clause results in the display of customers belonging to a specific cityonly. The name of the city will be provided by the user, during program execution. The ? sign isa parameter or placeholder, which accommodates the name of the city.

  • 8/9/2019 c# code in net app

    22/36

    Before building and running the application, introduce a textbox and a button.

    Change the Text property of the button to 'Show', and then, double click on the button to enterthe following lines of code:

    oleDbDataAdapter1.SelectCommand.Parameters["city"].Value = textBox5.Text;ddd1.Clear();oleDbDataAdapter1.Fill(ddd1);abc();

    There is just one more assignment to complete, before we wind up. Empty the functionForm1_Load. Now, run the program, and in the newly inserted textbox, enter 'London'. Then,click on the Show button. The output reveals that the city of London has six customers. Screen6.13 provides the proof.

    Screen 6.13

    Now, if you change 'London' to 'Paris', you will see only two customers from this city. Thus,from amongst the 91 customers, we can selectively view only those who belong to a certain city.

    Now let us try to appreciate the 'behind the scene' activities.

    The SelectCommand property in the oleDbDataAdapter, has a property named Parameters. It isactually an indexer that requires a string parameter to reference the parameter 'name'. TheValue property of the indexer is initialized to the city name provided in the Text property of thetextBox5. It is indeed a good idea to clear the dataset of all records, using the clear function.

    Then, the same Fill function is called, followed by the abc function. Thus, the presence of

    parameters makes our program extremely generic. The above code can be re-written in asimple format, as shown below.

    System.Data.OleDb.OleDbCommand a;a= oleDbDataAdapter1.SelectCommand;System.Data.OleDb.OleDbParameterCollection p;p = a.Parameters;System.Data.OleDb.OleDbParameter p1;p1 = p["city"];p1.Value = textBox5.Text;

  • 8/9/2019 c# code in net app

    23/36

    ddd1.Clear();oleDbDataAdapter1.Fill(ddd1);abc();

    The property SelectCommand is of data type OleDbCommand. It belongs to theSystem.Data.OleDb namespace. If the 'using' clause is not provided for this namespace, wehave to write the entire name of the namespace. This can be avoided by employing the 'using'keyword. The OleDbCommand has a property named Parameters, which is of data typeOleDbParameterCollection. This property, in turn, has an indexer that takes the parametername as a string parameter, and returns an OleDbParameter. The Value member of this objectp1, is initialized to the Text property of the textbox.

    Finally, you can change the Text property of all the textboxes to empty strings, because theinitial display lacks in aesthetic appeal. There are no bounds to the number of enhancementsthat we can effect to the above program.

    The presence of four copies of the function abc, one for each of the above four click functions,also does not appeal to us. So, we eliminate the calls to the function abc from the four clickfunctions, and add the following code to the click function of button5:

    BindingManagerBase z;

    z= BindingContext[ddd1, "Customers"];z.PositionChanged += new EventHandler(abc);

    Also, the abc function requires to be amended, to read as follows:

    void abc( object s, System.EventArgs e)

    The BindingManagerBase object z is first initialized, and then, the PositionChanged delegate isassigned to the function abc. Thus, each time the Position property changes, the abc functiongets called. There is no need for us to call the abc function explicitly, since the system calls it,whenever there is a change in the Position property.

    Every Windows Application comprises of a menu. So, how do we place menus in our Windows

    Application? As is generally done, we shall examine this process, one step at a time. We shallalso display the code generated by Visual Studio.Net, so that you are able to grasp the conceptof Menu Handling in C#.

    As always, save all the files, and start with a new project, using the File - New - Project menuoption. In the dialog box, select the Project type as Visual C# Project, and in the Templatepane, select the template as Windows Application. Name the project as s2, and locate it atc:\mukhi. Then, click on OK. You also need to ensure that the Properties and the Toolboxwindows are visible. From the ToolBox, under the Windows From tab, select the MainMenucontrol and drag-and-drop it into the Form. We merely witness a message Type Here. This isshown in screen 6.14. When we insert the word File, the menu changes to the one shown inscreen 6.15.

  • 8/9/2019 c# code in net app

    24/36

    Screen 6.14 Screen 6.15

    On building and running the program, the screen displays the word File, as shown in screen6.16.

    Screen 6.16

    On clicking this menu item, nothing gets displayed, and in effect, nothing transpires. We nowgo back to the Code Generator, and have a look at the code that is generated.

    private System.Windows.Forms.MainMenumainMenu1;private System.Windows.Forms.MenuItem menuItem1;

    There are two instance variables, mainMenu1 of data type MainMenu, and menuItem1 of datatype MenuItem.

    If you click on the word File in the Design Mode, the properties window will display theproperties for this menu item object. This is shown in screen 6.17.

  • 8/9/2019 c# code in net app

    25/36

    Screen 6.17

    private void InitializeComponent(){this.mainMenu1 = new System.Windows.Forms.MainMenu();this.menuItem1 = new System.Windows.Forms.MenuItem();this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {this.menuItem1});this.menuItem1.Index = 0;this.menuItem1.Text = "File";this.Menu = this.mainMenu1;}

    The function InitializeComponent contains the entire menu-generated code. Here, we firstcreate an instance for the two menu objects. The mainMenu1 object has the MenuItemsproperty, which in turn, calls the AddRange function. This function adds an array of MenuItemobjects. As we are in possession of only one such object, the array displays this solitary objectnamed menuItem1. This procedure is similar to the one used earlier for adding controls to theForm.

    The Index property shows 0, since it is the first menu item. The text property displays File. TheForm class has a property called Menu, which is initialized to the object MainMenu1. Thismain menu object knows all about the other menu items that are to be displayed.

    To add another menu item, click on the word File. This will open up an additional box on theright. Enter the text 'Edit', as shown in screen 6.18.

  • 8/9/2019 c# code in net app

    26/36

    Screen 6.18 Screen 6.19

    Build and run the application. A screen is displayed, containing two menu items named Fileand Edit, as is clearly seen in screen 6.19.

    With the introduction of one more menu item, the default name assigned to it becomesmenuItem2. An instance variable of the same name is created in the code file.

    private System.Windows.Forms.MenuItem menuItem2;

    private void InitializeComponent(){this.menuItem2 = new System.Windows.Forms.MenuItem();this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {this.menuItem1, this.menuItem2});

    this.menuItem2.Index = 1;this.menuItem2.Text = "Edit";}

    In the InitializeComponent function, additional code gets added for the second menu item. Theobject menuItem2 is first initialized and then, it is added to the array, which is to be providedto the AddRange function. We would have had to add them individually, if the array did notexist. Thus, adding 10 menu items would have entailed calling the function 10 times.

    The index property for this menu item is shown as 1, since it is the second menu item. The textproperty shows the newly entered text as Edit.

    Now, we shall add an item to the File menu. Click on the word File, and enter the word New, asdisplayed in screen 6.20.

  • 8/9/2019 c# code in net app

    27/36

    Screen 6.20 Screen 6.21

    Then, build and run the application. Click on the File menu, and it will display a popup menu,containing the word New. This is presented in screen 6.21.

    Let us now look at the code, which generates the popup menu.

    private System.Windows.Forms.MenuItem menuItem3;

    private void InitializeComponent(){this.menuItem3 = new System.Windows.Forms.MenuItem();this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {this.menuItem1,this.menuItem2});this.menuItem1.Index = 0;this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {this.menuItem3});this.menuItem1.Text = "File";this.menuItem3.Index = 0;this.menuItem3.Text = "New";}

    In order to store the word New, one more MenuItem named menuItem3, gets added. A newinstance is also created for this purpose. This menu item is not added to the array provided inthe AddRange function of mainMenu1.MenuItems. However, the menuItem1 object has asimilar property named MenuItems, which has an AddRange function. This function alsoaccepts an array of MenuItems.

    Since only a single MenuItem named New is to be added, this array holds only one member.

    The Index property of menuItem3 is assigned a value of zero, since it is the first menu itemunder File.

    Similarly, if we add one more item named Open just below New, another variable namedmenuItem4 will be created, and the Addrange function will now read as follows:

    this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {this.menuItem3,this.menuItem4});

  • 8/9/2019 c# code in net app

    28/36

    Thus, there will be two menu item objects in the array, which will be passed to the AddRangefunction of the menuItem1 object. Now, each time that the menu item Open is clicked, wedesire that some specific code should be called. Click on the left side of the word Open, withthe left mouse button. You will arrive at the screen shown in 6.22. Here, we see a tick-mark onthe menu.

    Screen 6.22 Screen 6.23

    Build and run the program. Now, click on the File menu. You will see the tick-mark displayedon the menu item Open. This is shown in screen 6.23.

    The tick-mark indicates that the menu has already been checked. This exists primarily for thepurpose of reminding the user that the menu had been selected earlier.

    this.menuItem4.Checked = true;

    this.menuItem4.Index = 1;this.menuItem4.Text = "Open";

    The MenuItem Open is called menuItem4, and has a property called Checked. This propertywhen set to True, merely checks the menu.

    Now, we desire that each time we click on the Open menu, the menu labeled New should getdisabled. To make this happen, double click on the word Open and enter the following code inthe Code Painter.

    private void menuItem4_Click(object sender, System.EventArgs e){

    menuItem3.Enabled = false;

    }

    The above function gets called only because the Click event of the menu item objectmenuItem4, has been set to the function MenuItem4_Click. As before, the name of the object isfollowed by the underscore sign, and then, by the magic word Click. The above functiondisables the menu item labeled New or menuItem3.

    Build and run the program. Then, Click on File - Open. On doing so, the above function getscalled; thus disabling the New option. Therefore, you are in complete control of the code that isto be executed, when the menu option is clicked.

  • 8/9/2019 c# code in net app

    29/36

    Screen 6.24

    Save all the files and create a new project. In the dialog box, select the project type as VisualC# Project, and the Template as a Windows Application. Name the project as s3, and locate itat c:\mukhi. Then, click on OK. As before, the Properties Window and the Toolbox must beactivated. Move down the scroll bar in the toolbox, and select the control ContextMenu. Drag-and-drop it into the form.

    Screen 6.25 shows the screen that encompasses the introduction of the ContextMenu.

    Screen 6.25

    The generated code contains the instance variable contextMenu1, of data type ContextMenu.

    private System.Windows.Forms.ContextMenu contextMenu1;

    private void InitializeComponent (){this.contextMenu1 = new System.Windows.Forms.ContextMenu();

  • 8/9/2019 c# code in net app

    30/36

    }

    Also, in the function InitializeComponent, the instance variable contextMenu1 is initialized to aContextMenu object. No additional code is required, since a ContextMenu is always added to acontrol. Clicking with the right mouse button on the control, displays this menu. Hence, it istermed as a ContextMenu.

    Now, we shall add two menu items to this Context Menu. Make sure that the Context Menuobject is highlighted at the bottom of the screen. Then, click on the words ContextMenu, asexhibited in screen 6.26.

    Screen 6.26

    Replace the words 'Type Here' with the word 'New'. Then, click on the words 'Type Here' givenbelow, and replace it with 'Open'.

    Thus, we now obtain two menu items in the context menu. But, the moment we click on theForm, the context menu disappears, since it is required to be displayed only when the contextmenu control is selected. This is unlike a main menu object. The extra code that is required foradding the items to a context menu, is as follows:

    private System.Windows.Forms.MenuItem menuItem1;private System.Windows.Forms.MenuItem menuItem2;

    private void InitializeComponent (){this.menuItem1 = new System.Windows.Forms.MenuItem();this.menuItem2 = new System.Windows.Forms.MenuItem();this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {this.menuItem1,this.menuItem2});this.menuItem1.Index = 0;this.menuItem1.Text = "New";this.menuItem2.Index = 1;this.menuItem2.Text = "Open";}

    For the two menu items New and Open, there exist two instance variables, menuItem1 andmenuItem2. In the function InitializeComponent, the two menu item objects are initialized;

  • 8/9/2019 c# code in net app

    31/36

    then, the AddRange function is used, which is present in the MenuItems collection property ofthe context menu object contextMenu1. The array passed to this function contains the twomenu items as its members. Thus, a context menu is no different from any other menu.

    Let us return back to the Design Mode and incorporate a button and a textbox in the Form.Select the button, and in the Properties Window, move to the property called ContextMenu.

    Click on the drop down listbox to arrive at a list of context menus, which are currentlyavailable for the Form. Since only one contextmenu1 is present, a solitary option is revealed.Select this option. Then, choose the textbox, and in the drop down listbox of the propertyContextMenu, pick the context menu named contextMenu1.

    On having done so, nothing earth-shattering occurs. If you cast a glance at the code, you willnotice that the property ContextMenu of the Textbox, and that of the Button, has beeninitialized to contextmenu1. Press F5 to run the program, and then, right click with the mouseon the Button. And there you go! The menu is now visible on the screen, containing two items,viz. New and Open. This is shown in screen 6.27.

    Screen 6.27

    We now incorporate a few amendments to the application, by adding a few more menu items tothe context menu. We also activate a function, when the control associated with the contextmenu is clicked.

    To do so, add the following line of code to the InitializeComponent function:

    this.contextMenu1.Popup += new System.EventHandler(abc);

    The ContextMenu class has an event called Popup, which is associated with the function abc.

    This event gets triggered, whenever we click on the right mouse button. It is a good practice toplace similar code together. This is merely a suggestion. Accordingly, we have placed all coderelated to the ContextMenu together.

    We have placed the above code immediately after the AddRange function. The exact locationdoes not matter, as long as it is placed inside the function, and is located after an instance hasbeen created.

  • 8/9/2019 c# code in net app

    32/36

    We also need to create the function abc, and call the Show function in it. The code specifiedbelow is placed after the Main function.

    private void abc(object sender, System.EventArgs e){MessageBox.Show("hi");}

    Press F5 to run the program, and then, click with the right mouse button. Before the popupsprings up, we notice a Message Box, as shown in screen 6.28.

    Screen 6.28 Screen 6.29

    Close the application. Select the context menu and double click on the menu item Open. In thefunction menuItem2_Click, call the MessageBox.Show function, as demonstrated below.

    private void menuItem2_Click(object sender, System.EventArgs e){MessageBox.Show("bye");}

    This function gets called, because the Click event of the menuItem2 has been added to ourcode as follows:

    this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);

    Build and run the program. Then, click on either the button or the textbox, with the rightmouse button. We first click on the Message Box displaying "hi", and then, on the Open menu

    item in the popup. This will bring up the Message Box displaying "bye". Thus, a menu itemlocated in a popup behaves akin to an ordinary menu.

    When we click on the popup, we desire to add one more menu item. So, all that we need to dois, add the following line of code in the function abc:

    contextMenu1.MenuItems.Add("mukhi");

    The Add function in the MenuItems will add a menu with the text mukhi. Remove the Showfunction, since it is of no use at all.

  • 8/9/2019 c# code in net app

    33/36

    Now, build and run the application, and then right click on the button. The menu that popsup, contains the menu item of "mukhi". Then, right click on the textbox. This will display themenu item "mukhi" twice.

    Screen 6.30 Screen 6.31

    We want to make a slight modification here. We intend that the menu item be added only whenthe button is selected. We also want specific code to be called. To achieve this, make thefollowing changes in the function abc:

    private void abc(object sender, System.EventArgs e){contextMenu1.MenuItems.Add("mukhi");if ( ActiveControl is Button)

    contextMenu1.MenuItems.Add("sonal",new System.EventHandler(pqr));}

    The 'if' statement makes use of the property ActiveControl, which belongs to the Form class.This is a dynamic property of data type Control. It contains the currently active control or thecontrol that has 'focus'. If it happens to be a Button, the 'if' statement evaluates to True. Theterm 'is' is a keyword in the C# programming language. It has a literal meaning, just as inEnglish. Thus, we use the Add function of the MenuItems collection, to add a menu called'sonal'. This is the first parameter, and the Eventhandler is the second parameter, representinga function named pqr.

    private void pqr(object sender, System.EventArgs e){MessageBox.Show("end");}

    The pqr function merely displays a Message Box.

    On running the application, we see the button already selected as it is the first control on theform, or more precisely has the tab index of 0. A selected button adds sonal to the list ofmenuoptions, so right clicking on any of the control will display sonal along with mukhi.

  • 8/9/2019 c# code in net app

    34/36

    Click on the textbox to select the textbox control. As the button looses focus, the if statementwith the ActiveControl property check on Button results in false. As a result, sonal is notadded again to the menu options. This can be verified by right clicking on any control. Clickingon 'sonal' will display a MessageBox, containing the text "end".

    Screen 6.32 Screen 6.33

    Thus, we can change the state of any property that we desire, since we have access to themenu objects.

    Now, save all the files, and start afresh with a new project. Name the C# Windows Applicationas s4, and select the location as c:\mukhi. Then, click on OK. The Properties window and the

    Toolbox should be visible.

    Now, incorporate the MainMenu control, and for the first menu item, enter "Vijay", and for the

    one below it, enter "Mukhi". The screen 6.34 displays these two menu items.

    Screen 6.34

  • 8/9/2019 c# code in net app

    35/36

    Now, usher-in another MainMenu object on the form, and for the first menu item, assign thetext "Sonal", and for the one below it, enter "File". The screen 6.35 displays the outcome ofthese actions.

    Screen 6.35

    We may acquire as many MainMenu objects as desired, but only one of them will be active atany given time. So, the form will initially show the first mainMenu1 to be active. The secondmenu object mainMenu2 will become visible, only when it is clicked on, in the bottom frame.

    Now introduce two buttons into the Form.

    Change the Text property of the first button to 'First', and then, double click on the button. Inthe function button1_Click, enter the following code:

    Menu = mainMenu1;

    Similarly, change the Text property of the second button to 'Second', and double click on thebutton, to enter the following line of code:

    Menu = mainMenu2;

    Once this is achieved, build and run the program. The first menu called "vijay", will be visibleby default. Clicking on the button labeled "Second", will immediately result in a switch to"Sonal". This is shown in screen 6.36.

  • 8/9/2019 c# code in net app

    36/36

    Screen 6.36

    Clicking on the button labeled "First", will again revert the menu back to the original, i.e. to"Vijay".

    Thus, we are empowered to change the main menu at any given time. We are also permitted tohave as many menu objects as we like. Further, we can point the Menu property to any Menuobject that we desire. Thus, at startup, a user sees a solitary menu, and after accomplishingcertain tasks, the menu switches to a more complex layout.

    Thus, you may have realized by now, that it is much more productive to work with a MenuPainter, than to manually write lines of tedious code.