Monday, March 29, 2010
C# Language
1. Implicit Typed Local Variables
Declaring variable without specifying data-type.
E.g. var count = 1 ; instead of int count = 1;
E.g. var str = "Hello World"; instead of string str = "Hello World";
- var is not like an object or a variant type that has expensive cost.
- The compiler will set the data-type depending on what you initialize the variable.
Usage of Type Inference
- You can use it When deal with Anonymous type in LINQ.
- Declaration must include an initializer.
e.g. var test = "Hello World";
- Initializer must be an expression and not an object or collection initializer and also can not initialize it with an array.
e.g. var test = {1,2,3} ; //Gives error
- Compile time type of the initialzer expression can not be the null type.
e.g. var test = null;
- Implicity typed local variable can be initialize more than once but not with different type.
e.g. var test = 1;
test = "Hello World";
Benefits of Type Inference
- Save the typing time.
- It induces/brings on variable initialization
Limitations
- Scope is limited to local variable
2. Auto Implemented Properties
A way to declare property without code block in property accessors.
Usage of Auto Implemented Properties
- We can use it in data-accessor classes.
Note: VB 9.0 does not have auto implemented properties feature while C# 3.0 have.
- It makes property declaration more concise when there is no custom logic required in property accessors.
For example,
Suppose we have to create a class Employee with private attributes First-Name, Last-Name and ID. Now in order to access its attributes values either we can create properties or functions for each attribute.
In C# 2.0, we implement Employee class with above requirements as follows
public class Employee { int _id; string _firstName; string _lastName; public int ID { get { return _id; } set { _id = value; } } public string FirstName { get { return _firstName; } set { _firstName = value; } } public string LastName { get { return _lastName; } set { _lastName = value; } } }
In C# 3.0, we implement Employee class with few lines for above requirements.
public class Employee { public int ID { get; set; } public string FirstName { set; get; } public string LastName { get; set; } }
How to create read-only property?
We can create read-only property by adding private keyword private before set.
e.g.
public class Employee { public int ID { get; private set; } }
How to initialize read-only property?
public class Employee { public Employee(int ID) { this.ID = ID; } public int ID { get; private set; } }
You can not create write only auto implemented properties but you can create it by traditional way.
public int ID { set { _id = value; } }
Benefits of Auto Implemented Properties
- Save typing time
- Increase productivity
3. Anonymous Types
A way to encapsulate a set of read-only properties into a object without defining class explicitly.
- The compiler will define a type/class automatically that represents the Anonymous Types in background.
- Anonymous types have been created with the help of "Object initializers" and implicitly typed variables.
- The Anonymous type is created with read-only properties mean you can not assign values to it after creation.
- It is strongly type.
- Mutable in VB and immutable in C#.
For example,
var product = new {Name = "C# Rocks!", Price = 3}; Automatically gets translated by the compiler to something like this: class __Anonymous1 { private string name; private int price; public string Name { get { return name; } set { name = value; } } public int Price { get { return price; } set { price = value; } } } __Anonymous1 product = new __Anonymous1(); product.Name = "C# Rocks!"; product.Price = 3;
Usage of Anonymous Types
- Use anonymous types when you deal with Linq to store query results. Anonymous type can be used in "LINQ to SQL" to access some of the fields from a whole set of fields.
Limitations of Anonymous Types
- The scope of Anonymous type is limited to the method in which it defines.
- It can not be used in method parameters or return values, class level (global) variables, properties.
4. Extension Methods
A way to extend an existing type
For example,
public static class StringExntensionMethods { public static void AlertMessageBox(this string str) { MessageBox.Show(str); } } string str = "Extesion Method Example"; str.AlertMessageBox();
Benefits of Extension Methods
- To extend an existing type.
- To specify validation on value of an existing type instance.
5. Object and Collection Initializers
A way to initialize accessible members of a class at object creation time without invoking constructor explicitly.
For example,
class Employee { // Auto-implemented properties public int Age { get; set; } public string Name { get; set; } } // Object initializer Employee obj = new Employee {Age = 24, Name = "Test"}; public class CoOrdinate { public int x ; public int y; }
Now we can create and initialize the object via this.
var myCoOrd = new CoOrdinate{ x = 0, y= 0} ;
The above code can also be replaced by creating constructor with arguments Age and Name. Then what is the benefit of Object Initializers? The benefit here is that the arguments order and the number of arguments.
- Collection initializers allow to specify one or more element initializers when you create an object of collection class that implements IEnumerable.(i.e. Element initializers can be a simple value, an expression or an object initializers.)
In C# 2.0, we create collection and add elements in collection.
For example,
Listanimals = new List (); animals.Add("monkey"); animals.Add("donkey"); animals.Add("cow"); animals.Add("dog"); animals.Add("cat");
In C# 3.0, the above code will be replaced by collection initializers features in C# 3.0
List
"monkey", "donkey", "cow", "dog", "cat" } ;
//Element initializers as a simple value
List
//Element initializers as a Object initializers
List
Benefits of Object and Collection Initializer
- The benefit of object initializers is that the arguments order and the number of arguments.
6. Lambda Expressions
It is a substitution for anonymous method – declaring method inline.
For example,
1) Traditional way of assigning method to delegate
delegate bool SomeDelegate(int i); public void SomeMethod() { SomeDelegate sd = new SomeDelegate(OtherMethod); } private bool OtherMethod(int i) { return i > 2; }
2) Replacing traditional way of declaring method and then assign it to delegate by declaring method inline via anonymous method.
delegate bool SomeDelegate(int i); private void SomeMethod() { SomeDelegate sd = delegate(int i){return i > 2;} }
3) Replacing anonymous method using Lambda Expression
delegate bool SomeDelegate(int i); private void SomeMethod() { SomeDelegate sd = (int i) => { return i > 2;} }
Benefits of Lambda Expressions
- Save typing time via minimal syntax - write inline code block where delegates are expected. No need to specify return types and name explicitly.
- It provides support for expression tree.
Usage of Lambda Expressions
- Replacement of Anonymous Methods.
- It can be also be used for filtering, sorting, iterating in generic collection and LINQ.
To make an instance of an object globally available and guarantee that only one instance of the class
Singleton provides a global,
Making the class create a single instance of itself.
Making the class create a single instance of itself.
public class SampleSingleton { //SingleTon private static volatile SampleSingleton _instance; private static object syncRoot = new Object(); public static SampleSingleton Instance { get { if (_instance == null) { lock (syncRoot) { if (_instance == null) { _instance = new SampleSingleton(); } } } return _instance; } } public string getDisplay() { return "SingleTon"; } } ///Access singleton SampleSingleton.Instance.getDisplay();
Sunday, March 28, 2010
Convert number to word
Convert number to word
Dim n As Long 'The output is saved in variable strOutPut. Dim strOutPut As String = "" Dim l, r As Integer 'Here 74 is considred as input.You may change as 'per your requirement. n = 74 'Get the floor value l = Math.Floor(n / 10) r = n Mod 10 If l = 1 Then 'For returning number from 10 to 19 Select Case r Case 0 strOutPut = strOutPut & " Ten" Case 1 strOutPut = strOutPut & " Eleven" Case 2 strOutPut = strOutPut & " Twelve" Case 3 strOutPut = strOutPut & " Thirteen" Case 4 strOutPut = strOutPut & " Fourteen" Case 5 strOutPut = strOutPut & " Fifteen" Case 6 strOutPut = strOutPut & " Sixteen" Case 7 strOutPut = strOutPut & " Seventeen" Case 8 strOutPut = strOutPut & " Eighteen" Case 9 strOutPut = strOutPut & " Nineteen" End Select Else 'For returning the number in multiple of 10. Select Case l Case 2 strOutPut = strOutPut & " Twenty" Case 3 strOutPut = strOutPut & " Thirty" Case 4 strOutPut = strOutPut & " Forty" Case 5 strOutPut = strOutPut & " Fifty" Case 6 strOutPut = strOutPut & " Sixty" Case 7 strOutPut = strOutPut & " Seventy" Case 8 strOutPut = strOutPut & " Eighty" Case 9 strOutPut = strOutPut & " Ninety" End Select 'For returning single digit number. Select Case r Case 1 strOutPut = strOutPut & " One" Case 2 strOutPut = strOutPut & " Two" Case 3 strOutPut = strOutPut & " Three" Case 4 strOutPut = strOutPut & " Four" Case 5 strOutPut = strOutPut & " Five" Case 6 strOutPut = strOutPut & " Six" Case 7 strOutPut = strOutPut & " Seven" Case 8 strOutPut = strOutPut & " Eight" Case 9 strOutPut = strOutPut & " Nine" End Select End If MessageBox.Show(strOutPut)
Issues related to the deployment of data access application
Here are Some of the basic issues related to the deployment
of the data access applications.
1)Can we suppose the required database exists at the target location?
2)If the database does exist then how can we get the connection string of it? Should we get it at the installation time? Or at the first run?
3)If the database does not exist then how it can be created? Is it the responsibility of human installing our application? Or the application should create it at the time of installation?
4)If the database is to be created by the application installation, then how does the installation setup know where (on which machine) to create the database? And what user name and password the setup should use to create the database?
5)Once, the database is created or its connection string is found at the setup time, then how to store the connection string for later use (regular execution)?
6)What if the database address (connection string) is changed during the application life cycle then what should be the application behavior? How can the application be aware of the new connection string?
7)If the database schema is hard-coded in the code (like table and field names) and the DB schema is changed (table/field name or data type is changed or new field/table added or some fields/tables deleted) because of any reason, then how to fix this problem?
1)Can we suppose the required database exists at the target location?
2)If the database does exist then how can we get the connection string of it? Should we get it at the installation time? Or at the first run?
3)If the database does not exist then how it can be created? Is it the responsibility of human installing our application? Or the application should create it at the time of installation?
4)If the database is to be created by the application installation, then how does the installation setup know where (on which machine) to create the database? And what user name and password the setup should use to create the database?
5)Once, the database is created or its connection string is found at the setup time, then how to store the connection string for later use (regular execution)?
6)What if the database address (connection string) is changed during the application life cycle then what should be the application behavior? How can the application be aware of the new connection string?
7)If the database schema is hard-coded in the code (like table and field names) and the DB schema is changed (table/field name or data type is changed or new field/table added or some fields/tables deleted) because of any reason, then how to fix this problem?
Thursday, March 25, 2010
Smtp Mail send
Smtp Mail send
Using This Code Send Smtp Mail as Simple way Where No attachment Write This code In Send mail event Change Smatp Server as your mailserverip instead of "localhost"
Server.ScriptTimeout = 1000; Response.Flush(); SmtpMail.SmtpServer = "localhost"; MailMessage mail = new MailMessage(); mail.To = this.TextBoxTo.Text; mail.Cc = this.TextBoxCc.Text; mail.Bcc = this.TextBoxBcc.Text; mail.From = this.TextBoxFrom.Text; mail.Subject = this.TextBoxSubject.Text; mail.Body = this.TextBoxBody.Text; try { SmtpMail.Send(mail); Response.Write(" The Mail has been sent to:
"); Response.Write("• To: " + mail.To + ""); Response.Write("• Cc: " + mail.Cc + ""); Response.Write("• Bcc: " + mail.Bcc + ""); } catch(System.Exception ex) { Response.Write("An erros has occured: " + ex.Message + "
"); } Response.Flush();
Craete Simple quiz Application in C#
1) copy this code in notepad and name the file as quiz.cs and
keep in a folder named QuizApp
2) Now keep that folder in C: or D: drive
3) Open your VS 2005 Command prompt and give the path of folder where your code is lying.
4) When you reach that path type in csc quiz [quiz your .cs filename]
5) Now goto to your folder and check. .Exe would be generated. Just click on it and enjoy with the application. I have given my default username and password.
2) Now keep that folder in C: or D: drive
3) Open your VS 2005 Command prompt and give the path of folder where your code is lying.
4) When you reach that path type in csc quiz [quiz your .cs filename]
5) Now goto to your folder and check. .Exe would be generated. Just click on it and enjoy with the application. I have given my default username and password.
using System; using System.Windows.Forms; using System.Drawing; using System.ComponentModel; public class app:Form { Label log_name = new Label(); TextBox logname = new TextBox(); Label pass_name = new Label(); TextBox passt_name = new TextBox(); Button submit = new Button(); int y=150; int x=100; int x1=225; public app() { this.IsMdiContainer=true; //this will allow multiple window to be display. this.Text = "Home Page";// This will set Title for application window this.Size = new Size(550,400); this.MaximizeBox = false;//window can not be maximized by setting this property. this.BackColor = Color.Pink; this.AcceptButton=submit; this.StartPosition = FormStartPosition.CenterScreen;// set window in the middle of screen. this.FormBorderStyle = FormBorderStyle.Fixed3D;// setting this property window can not be resize. this.AutoScroll = true;//Scroll bar is activated if component placing exceeds the form dimension textBox1() ;// this function will display Login Form for user } public void textBox1() { log_name.Text = "Login Id :";//set label text to be displayed log_name.Location = new Point(x,y); //place label at mention position in the Form log_name.Size = new Size (112,23); //allocate space for label log_name.Font = new Font ("Verdana", 10); log_name.ForeColor = Color.Red; //setting forecolor for lable this.Controls.Add(log_name);//Adding label into the form logname.TabIndex = 0; logname.Location = new Point(x1,y); logname.Size = new Size (150,20); logname.Text="amit"; logname.Font = new Font ("Arial", 10,FontStyle.Bold); logname.MaxLength = 15;// Only 15 or less than 15 characters can be enter in the textbox. logname.BorderStyle= System.Windows.Forms.BorderStyle.Fixed3D; this.Controls.Add( logname); //Last name pass_name.Text = "Password :"; pass_name.Location = new Point(x,y+50); pass_name.Size = new Size (112,23); pass_name.Font = new Font ("Verdana", 10); pass_name.ForeColor = Color.Red; this.Controls.Add(pass_name); passt_name.TabIndex = 1; passt_name.Location = new Point(x1,y+50); passt_name.Size = new Size (150,20); passt_name.PasswordChar = '*';//setting * to be displayed for the password charater entered by user passt_name.Text="amit"; passt_name.Font = new Font ("Arial", 10,FontStyle.Bold); passt_name.BorderStyle= System.Windows.Forms.BorderStyle.Fixed3D; passt_name.MaxLength = 15; this.Controls.Add(passt_name); Button submit = new Button(); submit.Text= "Submit"; submit.Location= new Point(200,y+100); submit.Size = new Size(75,25); submit.Font= new Font("verdana",10,FontStyle.Bold); submit.BackColor=Color.Red; submit.ForeColor=Color.Yellow; submit.Click+= new EventHandler(submit_click); this.Controls.Add(submit); Button reset = new Button(); reset.Text= "Reset"; reset.Location= new Point(300,y+100); reset.Size = new Size(75,25); reset.Font= new Font("verdana",10,FontStyle.Bold); reset.ForeColor=Color.Yellow; reset.BackColor=Color.Red; reset.Click+= new EventHandler(submit_click); this.Controls.Add(reset); Label Reg_name = new Label(); Reg_name.Text = "For New Registration :"; Reg_name.Location = new Point(x1-25,y-100); //Set label position Reg_name.Size = new Size (175,23); Reg_name.Font = new Font ("Verdana", 10); Reg_name.ForeColor = Color.Blue; this.Controls.Add(Reg_name);//Add Button Reg = new Button(); Reg.Text= "Registration"; Reg.Location= new Point(400,y-100); Reg.Size = new Size(110,25); Reg.Font= new Font("verdana",10,FontStyle.Bold); Reg.ForeColor=Color.Yellow; Reg.BackColor=Color.Red; Reg.Click+= new EventHandler(submit_click); this.Controls.Add(Reg); } public void submit_click( object sender, EventArgs e)//this block of code will called when user clicked on the any button. { String b1 = ((Button)sender).Text; Console.WriteLine(" b1:"+b1); if(b1=="Registration") { logname.Text = ""; passt_name.Text = ""; Console.WriteLine(" in side Registration"); Client client1 = new Client(this); this.Hide();//this will hide login Form client1.Show();// this will display Registration Form } if(b1=="Submit") { //if login name and password are correct then allow user to move ahead. if((logname.Text.Trim()=="deepika")&&(passt_name.Text.Trim()=="deepika"))// will check login name and password { MessageBox.Show("Form has been submited Successfully ","Confirmation Message",MessageBoxButtons.OK,MessageBoxIcon.Information); User frmTemp= new User(this,logname);//will display Category select Form, passing main Form (this) as object frmTemp.Show();//this will Category select Form this.Hide();//this will hide login Form } else { MessageBox.Show("Please enter correct Login Id as amit and Password as amit","Error Message",MessageBoxButtons.OK,MessageBoxIcon.Information); logname.Focus(); logname.Text = ""; } } if(b1=="Reset")//if user clicked on the reset this code will execute { logname.Text = "";//cleared all the field data passt_name.Text = ""; logname.Focus(); } } public static void Main() { Application.Run(new app()); } protected override void Dispose( bool disposing ) { if( disposing ) { try{ MessageBox.Show("You are Closing this From ","Alert Message",MessageBoxButtons.OK,MessageBoxIcon.Exclamation); } catch(Exception ){ } } base.Dispose( disposing ); } } // THIS CODE WILL DISPLAY CATEGORY FORM public class User:Form { Panel panel1 = new Panel(); ComboBox cb1 = new ComboBox(); Panel panel2 = new Panel(); Panel panel3 = new Panel(); Panel panel4 = new Panel(); Label Reg_name = new Label(); Form window1; TextBox s; bool isTrue; int scor =0 ; //initialize score board variable. String s7="null"; RadioButton radioButton1 = new RadioButton(); RadioButton radioButton2 = new RadioButton(); RadioButton radioButton3 = new RadioButton(); RadioButton radioButton21 = new RadioButton(); RadioButton radioButton22 = new RadioButton(); RadioButton radioButton23 = new RadioButton(); RadioButton radioButton31 = new RadioButton(); RadioButton radioButton32 = new RadioButton(); RadioButton radioButton33 = new RadioButton(); public User(Form window,TextBox logname1) { Console.WriteLine(" in side user text box : "+s); window1 = window; //initializing variable send to this Form s= logname1;//initializing variable send to this Form this.Text = "Select Category";// This will set Title for application window this.Size = new Size(600,500); this.MaximizeBox = false;//window can not be maximized by setting this property. this.BackColor = Color.Yellow; this.SuspendLayout(); this.StartPosition = FormStartPosition.CenterScreen;// set window in the middle of screen. this.FormBorderStyle = FormBorderStyle.Fixed3D;// setting this property window can not be resize. Reg_name.Text = "Hi Amit!"; Reg_name.Location = new Point(10,25); //Set label position Reg_name.Size = new Size (100,25); Reg_name.Font = new Font ("Verdana", 10,FontStyle.Bold); Reg_name.ForeColor = Color.Red; this.Controls.Add(Reg_name); panel1.Location = new Point(10,60);//crating panel on the From panel1.Size = new Size(575, 300); panel1.BackColor = Color.Green; this.Controls.Add(panel1);// Add the Panel control to the form. Label l_cb = new Label();//ComboBox label l_cb .Text = " Category :"; l_cb .Location = new Point(10,25); l_cb .Size = new Size (100,17); l_cb .Font = new Font ("Verdana", 10); l_cb .ForeColor = Color.Blue; cb1.Location = new Point(125,25); cb1 .Size = new Size (232,20); cb1 .Font = new Font ("Verdana", 10); cb1.Text= " Select One Option "; cb1.Items.Add(" Select One Option "); //Adding items in the ComboBox. cb1.Items.Add("Software"); cb1.Items.Add("Hardware"); cb1.Items.Add("General"); cb1.Items.Add("Sports"); cb1.Items.Add("Java"); cb1.Items.Add("C#(Sharp)"); cb1.Sorted=true;// setting sort property for ComboBox Button Reg1 = new Button(); Reg1.Text= "Submit"; Reg1.Location= new Point(250,250); Reg1.Size = new Size(75,25); Reg1.Font= new Font("verdana",10,FontStyle.Bold); Reg1.ForeColor=Color.Yellow; Reg1.BackColor=Color.Red; Reg1.Click+= new EventHandler(submit1_click); Button bac = new Button(); bac.Text= "Home Page"; bac.Location= new Point(250,425); bac.Size = new Size(125,25); bac.Font= new Font("verdana",10,FontStyle.Bold); bac.ForeColor=Color.Yellow; bac.BackColor=Color.Red; bac.Click+= new EventHandler(submit1_click);// when user clicked on the button mentioned event will call this.Controls.Add(bac); // Add the Label and TextBox controls to the Panel. panel1.Controls.Add(l_cb ); panel1.Controls.Add(cb1); panel1.Controls.Add(Reg1); } public void submit1_click( object sender, EventArgs e)// block of code will get call when user clicks on the submit { Boolean flag1 = true; String b1 = ((Button)sender).Text; s7 =cb1.Text; if(b1=="Home Page") { this.Dispose();//Category Form will get dispose window1.Show(); //Login form will get display, for this purpose i have passed (this) object to this constructor s.Focus(); scor = 0; Console.WriteLine("Score board :"+scor); } else //when user clicked on th submit button { if(s7!="General") { MessageBox.Show("For Demo Please select only General Category ","Confirmation Message",MessageBoxButtons.OK,MessageBoxIcon.Information); flag1 = false; cb1.Focus(); } if(flag1) //is flag is true { this.Controls.Remove(panel1); //this remove panel added previously on the Category Form this.Controls.Remove(Reg_name); // remove top label form the Category Form showdata(s7); } } } public void showdata(String str) { String b1 = str; this.Text = "View FAQS";//setting new title for Form Label wel = new Label(); wel.Text = "Wel Come to the Category : "+b1+" !! "; wel.Location = new Point(100,25); //Set label position wel.Size = new Size (350,25); wel.Font = new Font ("Verdana", 10,FontStyle.Bold); wel.ForeColor = Color.Red; this.Controls.Add(wel); showquestion(); } public void showquestion() { panel2.Location = new Point(50,60); panel2.Size = new Size(485, 320); panel2.BackColor = Color.Pink; this.Controls.Add(panel2);//newly created panel added to this Form Button Reg2 = new Button(); Reg2.Text= "Next2"; Reg2.Location= new Point(200,270); Reg2.Size = new Size(75,25); Reg2.Font= new Font("verdana",10,FontStyle.Bold); Reg2.ForeColor=Color.Yellow; Reg2.BackColor=Color.Red; Reg2.Click+= new EventHandler(submit2_click); Label ques1 = new Label(); ques1.Text = "Question 1: Who won the 1998 Soccer world Cup ?"; ques1.Location = new Point(10,20); //Set label position ques1.Size = new Size (425,25); ques1.Font = new Font ("Verdana", 10,FontStyle.Bold); ques1.ForeColor = Color.Red; GroupBox groupBox1 =new GroupBox();//creating Group box for radio buttons groupBox1.Text = "Answer"; groupBox1.Location= new Point(50,60); groupBox1.Size = new Size(400,175); radioButton1.Location= new Point(20,25); radioButton1.Size = new Size(50,25); radioButton1.CheckedChanged += new System.EventHandler(value);//Evwn will call when user select any check box Label ans1 = new Label(); ans1.Text = " France "; ans1.Location = new Point(75,25); //Set label position ans1.Size = new Size (100,25); ans1.Font = new Font ("Verdana", 10,FontStyle.Bold); ans1.ForeColor = Color.Blue; radioButton2.Location= new Point(20,75); radioButton2.Size = new Size(50,25); radioButton2.CheckedChanged += new System.EventHandler(value); Label ans2 = new Label(); ans2.Text = " Brazil "; ans2.Location = new Point(75,75); //Set label position ans2.Size = new Size (100,25); ans2.Font = new Font ("Verdana", 10,FontStyle.Bold); ans2.ForeColor = Color.Blue; radioButton3.Location= new Point(20,125); radioButton3.Size = new Size(50,25); radioButton3.CheckedChanged += new System.EventHandler(value); Label ans3 = new Label(); ans3.Text = " Italy "; ans3.Location = new Point(75,125); //Set label position ans3.Size = new Size (100,25); ans3.Font = new Font ("Verdana", 10,FontStyle.Bold); ans3.ForeColor = Color.Blue; // Add the RadioButtons to the GroupBox. groupBox1.Controls.Add(radioButton1); groupBox1.Controls.Add(radioButton2); groupBox1.Controls.Add(radioButton3); groupBox1.Controls.Add(ans1); groupBox1.Controls.Add(ans2); groupBox1.Controls.Add(ans3); // Add the GroupBox to the Form. Controls.Add(groupBox1); panel2.Controls.Add(Reg2); panel2.Controls.Add(ques1); panel2.Controls.Add(groupBox1); } public void submit2_click( object sender, EventArgs e) { if(isTrue) //if given answer is correct, flag will be true, and Score Board will be incremented { scor = scor + 1; } isTrue = false; Console.WriteLine(" value of score:"+ scor); showquestion1();// this function will remove current pannel and insert new panel } public void value(object sender, EventArgs e) { isTrue = radioButton1.Checked; //will set to true if correct answer checkbox is selected by user } public void showquestion1() { panel2.Dispose(); //dispose current panel panel3.Location = new Point(50,60);//create new panel panel3.Size = new Size(485, 320); //panel2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; panel3.BackColor = Color.Orange; this.Controls.Add(panel3);//newly created panel added to Category Form Button Reg2 = new Button(); Reg2.Text= "Next3"; Reg2.Location= new Point(200,270); Reg2.Size = new Size(75,25); Reg2.Font= new Font("verdana",10,FontStyle.Bold); Reg2.ForeColor=Color.Yellow; Reg2.BackColor=Color.Red; Reg2.Click+= new EventHandler(submit3_click); Label ques1 = new Label(); ques1.Text = "Question 2: Which Country won the 1999 Cricket world Cup?"; ques1.Location = new Point(10,20); //Set label position ques1.Size = new Size (425,40); ques1.Font = new Font ("Verdana", 10,FontStyle.Bold); ques1.ForeColor = Color.Red; GroupBox groupBox2 =new GroupBox(); groupBox2.Text = "Answer"; groupBox2.Location= new Point(50,60); groupBox2.Size = new Size(400,175); radioButton21.Location= new Point(20,25); radioButton21.Size = new Size(50,25); radioButton21.CheckedChanged += new System.EventHandler(value2); Label ans1 = new Label(); ans1.Text = " India "; ans1.Location = new Point(75,25); //Set label position ans1.Size = new Size (100,25); ans1.Font = new Font ("Verdana", 10,FontStyle.Bold); ans1.ForeColor = Color.Blue; radioButton22.Location= new Point(20,75); radioButton22.Size = new Size(50,25); radioButton22.CheckedChanged += new System.EventHandler(value2); Label ans2 = new Label(); ans2.Text = " Australia "; ans2.Location = new Point(75,75); //Set label position ans2.Size = new Size (100,25); ans2.Font = new Font ("Verdana", 10,FontStyle.Bold); ans2.ForeColor = Color.Blue; radioButton23.Location= new Point(20,125); radioButton23.Size = new Size(50,25); radioButton23.CheckedChanged += new System.EventHandler(value2); Label ans3 = new Label(); ans3.Text = " Pakistan "; ans3.Location = new Point(75,125); //Set label position ans3.Size = new Size (100,25); ans3.Font = new Font ("Verdana", 10,FontStyle.Bold); ans3.ForeColor = Color.Blue; // Add the RadioButtons to the GroupBox. groupBox2.Controls.Add(radioButton21); groupBox2.Controls.Add(radioButton22); groupBox2.Controls.Add(radioButton23); groupBox2.Controls.Add(ans1); groupBox2.Controls.Add(ans2); groupBox2.Controls.Add(ans3); // Add the GroupBox to the Form. Controls.Add(groupBox2); panel3.Controls.Add(Reg2); panel3.Controls.Add(ques1); panel3.Controls.Add(groupBox2); } public void submit3_click( object sender, EventArgs e) { if(isTrue) { scor = scor + 1; } isTrue = false; Console.WriteLine(" value of score:"+ scor); showquestion2(); } public void value2(object source, EventArgs e) { isTrue = radioButton22.Checked; } public void showquestion2() { panel3.Dispose(); Panel panel4 = new Panel(); panel4.SuspendLayout(); panel4.Location = new Point(50,60); panel4.Size = new Size(485, 320); panel4.BackColor = Color.Orange; this.Controls.Add(panel4); Button Reg2 = new Button(); Reg2.Text= "Next4"; Reg2.Location= new Point(200,270); Reg2.Size = new Size(75,25); Reg2.Font= new Font("verdana",10,FontStyle.Bold); Reg2.ForeColor=Color.Yellow; Reg2.BackColor=Color.Red; Reg2.Click+= new EventHandler(submit4_click); Label ques1 = new Label(); ques1.Text = "Question 3: Who won the 20001 Wimbledon Grand Slam?"; ques1.Location = new Point(10,20); //Set label position ques1.Size = new Size (475,25); ques1.Font = new Font ("Verdana", 10,FontStyle.Bold); ques1.ForeColor = Color.Red; GroupBox groupBox3 =new GroupBox(); groupBox3.Text = "Answer"; groupBox3.Location= new Point(50,60); groupBox3.Size = new Size(400,175); radioButton31.Location= new Point(20,25); radioButton31.Size = new Size(50,25); radioButton31.CheckedChanged += new System.EventHandler(value3); Label ans1 = new Label(); ans1.Text = " Andre Aggassi "; ans1.Location = new Point(75,25); //Set label position ans1.Size = new Size (250,25); ans1.Font = new Font ("Verdana", 10,FontStyle.Bold); ans1.ForeColor = Color.Blue; radioButton32.Location= new Point(20,75); radioButton32.Size = new Size(50,25); radioButton32.CheckedChanged += new System.EventHandler(value3); Label ans2 = new Label(); ans2.Text = " Peat Sampras"; ans2.Location = new Point(75,75); //Set label position ans2.Size = new Size (250,25); ans2.Font = new Font ("Verdana", 10,FontStyle.Bold); ans2.ForeColor = Color.Blue; radioButton33.Location= new Point(20,125); radioButton33.Size = new Size(50,25); radioButton33.CheckedChanged += new System.EventHandler(value3); Label ans3 = new Label(); ans3.Text = " Goran Ivansevich "; ans3.Location = new Point(75,125); //Set label position ans3.Size = new Size (250,25); ans3.Font = new Font ("Verdana", 10,FontStyle.Bold); ans3.ForeColor = Color.Blue; // Add the RadioButtons to the GroupBox. groupBox3.Controls.Add(radioButton31); groupBox3.Controls.Add(radioButton32); groupBox3.Controls.Add(radioButton33); groupBox3.Controls.Add(ans1); groupBox3.Controls.Add(ans2); groupBox3.Controls.Add(ans3); panel4.Controls.Add(Reg2); panel4.Controls.Add(ques1); panel4.Controls.Add(groupBox3); } public void submit4_click( object sender, EventArgs e) { if(isTrue) { scor = scor + 1; } isTrue = false; Console.WriteLine(" value of score:"+ scor); User1 f1= new User1(window1,s,scor,s7);//here new form is crated and passing some of the value to the constructor f1.Show(); this.Hide(); } public void value3(object source, EventArgs e) { isTrue = radioButton33.Checked; } } public class User1:Form { //initializing values Form dumwin; int scoreb; TextBox tb1; String s1; bool isTrue1; Panel panel1 = new Panel(); Panel panel2 = new Panel(); Panel panel3 = new Panel(); Panel panel4 = new Panel(); Panel panel5 = new Panel(); Panel panel6 = new Panel(); Panel panel7 = new Panel(); RadioButton radioButton11 = new RadioButton(); RadioButton radioButton12 = new RadioButton(); RadioButton radioButton13 = new RadioButton(); RadioButton radioButton21 = new RadioButton(); RadioButton radioButton22 = new RadioButton(); RadioButton radioButton23 = new RadioButton(); RadioButton radioButton31 = new RadioButton(); RadioButton radioButton32 = new RadioButton(); RadioButton radioButton33 = new RadioButton(); RadioButton radioButton41 = new RadioButton(); RadioButton radioButton42 = new RadioButton(); RadioButton radioButton43 = new RadioButton(); RadioButton radioButton51 = new RadioButton(); RadioButton radioButton52 = new RadioButton(); RadioButton radioButton53 = new RadioButton(); RadioButton radioButton61 = new RadioButton(); RadioButton radioButton62 = new RadioButton(); RadioButton radioButton63 = new RadioButton(); RadioButton radioButton71 = new RadioButton(); RadioButton radioButton72 = new RadioButton(); RadioButton radioButton73 = new RadioButton(); public User1(Form window2 ,TextBox tb,int sb,String s) { dumwin = window2;//assigning value to the variable on this New Form scoreb = sb; tb1 = tb; s1= s; this.Text = "View FAQS";// This will set Title for application window this.Size = new Size(600,500); this.MaximizeBox = false;//window can not be maximized by setting this property. this.BackColor = Color.Yellow; this.StartPosition = FormStartPosition.CenterScreen;// set window in the middle of screen. this.FormBorderStyle = FormBorderStyle.Fixed3D;// setting this property window can not be resize. Label wel = new Label(); wel.Text = "Wel Come to the Category : "+s1+" !! "; wel.Location = new Point(100,25); //Set label position wel.Size = new Size (350,25); wel.Font = new Font ("Verdana", 10,FontStyle.Bold); wel.ForeColor = Color.Red; this.Controls.Add(wel); Button bac = new Button(); bac.Text= "Home Page"; bac.Location= new Point(250,425); bac.Size = new Size(125,25); bac.Font= new Font("verdana",10,FontStyle.Bold); bac.ForeColor=Color.Yellow; bac.BackColor=Color.Red; bac.Click+= new EventHandler(submit1_click); this.Controls.Add(bac); showquest1(); } public void showquest1() { panel1.SuspendLayout(); panel1.Location = new Point(50,60); panel1.Size = new Size(485, 320); panel1.BackColor = Color.Cyan; this.Controls.Add(panel1); Button Reg2 = new Button(); Reg2.Text= "Next5"; Reg2.Location= new Point(200,270); Reg2.Size = new Size(75,25); Reg2.Font= new Font("verdana",10,FontStyle.Bold); Reg2.ForeColor=Color.Yellow; Reg2.BackColor=Color.Red; Reg2.Click+= new EventHandler(submit1_click); Label ques1 = new Label(); ques1.Text = "Question 4: Who won the 20001 F1 Championship ?"; ques1.Location = new Point(10,20); //Set label position ques1.Size = new Size (475,25); ques1.Font = new Font ("Verdana", 10,FontStyle.Bold); ques1.ForeColor = Color.Red; GroupBox groupBox =new GroupBox(); groupBox.Text = "Answer"; groupBox.Location= new Point(50,60); groupBox.Size = new Size(400,175); radioButton11.Location= new Point(20,25); radioButton11.Size = new Size(50,25); radioButton11.CheckedChanged += new System.EventHandler(value1); Label ans1 = new Label(); ans1.Text = " David Coulthad "; ans1.Location = new Point(75,25); //Set label position ans1.Size = new Size (250,25); ans1.Font = new Font ("Verdana", 10,FontStyle.Bold); ans1.ForeColor = Color.Blue; radioButton12.Location= new Point(20,75); radioButton12.Size = new Size(50,25); radioButton12.CheckedChanged += new System.EventHandler(value1); Label ans2 = new Label(); ans2.Text = " Michael Schumacher"; ans2.Location = new Point(75,75); //Set label position ans2.Size = new Size (250,25); ans2.Font = new Font ("Verdana", 10,FontStyle.Bold); ans2.ForeColor = Color.Blue; radioButton13.Location= new Point(20,125); radioButton13.Size = new Size(50,25); radioButton13.CheckedChanged += new System.EventHandler(value1); Label ans3 = new Label(); ans3.Text = " Ralf Schumacher "; ans3.Location = new Point(75,125); //Set label position ans3.Size = new Size (250,25); ans3.Font = new Font ("Verdana", 10,FontStyle.Bold); ans3.ForeColor = Color.Blue; // Add the RadioButtons to the GroupBox. groupBox.Controls.Add(radioButton11); groupBox.Controls.Add(radioButton12); groupBox.Controls.Add(radioButton13); groupBox.Controls.Add(ans1); groupBox.Controls.Add(ans2); groupBox.Controls.Add(ans3); panel1.Controls.Add(Reg2); panel1.Controls.Add(ques1); panel1.Controls.Add(groupBox); } public void submit1_click( object sender, EventArgs e) { String b1 = ((Button)sender).Text; //Console.WriteLine(" b1:"+b1); if(b1=="Home Page") { this.Dispose(); //Console.WriteLine("after disposing"); dumwin.Show(); tb1.Focus(); scoreb = 0; Console.WriteLine("Score board :"+scoreb); } if(isTrue1) scoreb = scoreb+1; Console.WriteLine("Score board :"+scoreb); showquest2(); } public void value1(object source, EventArgs e) { isTrue1 = radioButton12.Checked; } public void showquest2() { panel1.Dispose(); panel2.Location = new Point(50,60); panel2.Size = new Size(485, 320); panel2.BackColor = Color.Cyan; this.Controls.Add(panel2); Button Reg2 = new Button(); Reg2.Text= "Next6"; Reg2.Location= new Point(200,270); Reg2.Size = new Size(75,25); Reg2.Font= new Font("verdana",10,FontStyle.Bold); Reg2.ForeColor=Color.Yellow; Reg2.BackColor=Color.Red; Reg2.Click+= new EventHandler(submit2_click); Label ques1 = new Label(); ques1.Text = "Question 5: Select correct date for WTC Terrorist Attack?"; ques1.Location = new Point(10,20); //Set label position ques1.Size = new Size (475,25); ques1.Font = new Font ("Verdana", 10,FontStyle.Bold); ques1.ForeColor = Color.Red; GroupBox groupBox =new GroupBox(); groupBox.Text = "Answer"; groupBox.Location= new Point(50,60); groupBox.Size = new Size(400,175); radioButton21.Location= new Point(20,25); radioButton21.Size = new Size(50,25); radioButton21.CheckedChanged += new System.EventHandler(value2); Label ans1 = new Label(); ans1.Text = " 12 september"; ans1.Location = new Point(75,25); //Set label position ans1.Size = new Size (250,25); ans1.Font = new Font ("Verdana", 10,FontStyle.Bold); ans1.ForeColor = Color.Blue; radioButton22.Location= new Point(20,75); radioButton22.Size = new Size(50,25); radioButton22.CheckedChanged += new System.EventHandler(value2); Label ans2 = new Label(); ans2.Text = " 13 September"; ans2.Location = new Point(75,75); //Set label position ans2.Size = new Size (250,25); ans2.Font = new Font ("Verdana", 10,FontStyle.Bold); ans2.ForeColor = Color.Blue; radioButton23.Location= new Point(20,125); radioButton23.Size = new Size(50,25); radioButton23.CheckedChanged += new System.EventHandler(value2); Label ans3 = new Label(); ans3.Text = " 11 September "; ans3.Location = new Point(75,125); //Set label position ans3.Size = new Size (250,25); ans3.Font = new Font ("Verdana", 10,FontStyle.Bold); ans3.ForeColor = Color.Blue; // Add the RadioButtons to the GroupBox. groupBox.Controls.Add(radioButton21); groupBox.Controls.Add(radioButton22); groupBox.Controls.Add(radioButton23); groupBox.Controls.Add(ans1); groupBox.Controls.Add(ans2); groupBox.Controls.Add(ans3); panel2.Controls.Add(Reg2); panel2.Controls.Add(ques1); panel2.Controls.Add(groupBox); } public void submit2_click( object sender, EventArgs e) { String b1 = ((Button)sender).Text; if(isTrue1) scoreb = scoreb+1; isTrue1 = false; Console.WriteLine("Score board :"+scoreb); showquest3(); } public void value2(object source, EventArgs e) { isTrue1 = radioButton23.Checked; } public void showquest3() { panel2.Dispose(); panel3.Location = new Point(50,60); panel3.Size = new Size(485, 320); panel3.BackColor = Color.Cyan; this.Controls.Add(panel3); Button Reg2 = new Button(); Reg2.Text= "Next7"; Reg2.Location= new Point(200,270); Reg2.Size = new Size(75,25); Reg2.Font= new Font("verdana",10,FontStyle.Bold); Reg2.ForeColor=Color.Yellow; Reg2.BackColor=Color.Red; Reg2.Click+= new EventHandler(submit3_click); Label ques1 = new Label(); ques1.Text = "Question 6: Which is the Capital of New Zealand?"; ques1.Location = new Point(10,20); //Set label position ques1.Size = new Size (475,25); ques1.Font = new Font ("Verdana", 10,FontStyle.Bold); ques1.ForeColor = Color.Red; GroupBox groupBox =new GroupBox(); groupBox.Text = "Answer"; groupBox.Location= new Point(50,60); groupBox.Size = new Size(400,175); radioButton31.Location= new Point(20,25); radioButton31.Size = new Size(50,25); radioButton31.CheckedChanged += new System.EventHandler(value3); Label ans1 = new Label(); ans1.Text = " Wellignton"; ans1.Location = new Point(75,25); //Set label position ans1.Size = new Size (250,25); ans1.Font = new Font ("Verdana", 10,FontStyle.Bold); ans1.ForeColor = Color.Blue; radioButton32.Location= new Point(20,75); radioButton32.Size = new Size(50,25); radioButton32.CheckedChanged += new System.EventHandler(value3); Label ans2 = new Label(); ans2.Text = " Auckland"; ans2.Location = new Point(75,75); //Set label position ans2.Size = new Size (250,25); ans2.Font = new Font ("Verdana", 10,FontStyle.Bold); ans2.ForeColor = Color.Blue; radioButton33.Location= new Point(20,125); radioButton33.Size = new Size(50,25); radioButton33.CheckedChanged += new System.EventHandler(value3); Label ans3 = new Label(); ans3.Text = " Canbera "; ans3.Location = new Point(75,125); //Set label position ans3.Size = new Size (250,25); ans3.Font = new Font ("Verdana", 10,FontStyle.Bold); ans3.ForeColor = Color.Blue; // Add the RadioButtons to the GroupBox. groupBox.Controls.Add(radioButton31); groupBox.Controls.Add(radioButton32); groupBox.Controls.Add(radioButton33); groupBox.Controls.Add(ans1); groupBox.Controls.Add(ans2); groupBox.Controls.Add(ans3); panel3.Controls.Add(Reg2); panel3.Controls.Add(ques1); panel3.Controls.Add(groupBox); } public void submit3_click( object sender, EventArgs e) { String b1 = ((Button)sender).Text; if(isTrue1) scoreb = scoreb+1; isTrue1 = false; Console.WriteLine("Score board :"+scoreb); showquest4(); } public void value3(object source, EventArgs e) { isTrue1 = radioButton23.Checked; } public void showquest4() { panel3.Dispose(); panel4.Location = new Point(50,60); panel4.Size = new Size(485, 320); panel4.BackColor = Color.Cyan; this.Controls.Add(panel4); Button Reg2 = new Button(); Reg2.Text= "Next8"; Reg2.Location= new Point(200,270); Reg2.Size = new Size(75,25); Reg2.Font= new Font("verdana",10,FontStyle.Bold); Reg2.ForeColor=Color.Yellow; Reg2.BackColor=Color.Red; Reg2.Click+= new EventHandler(submit4_click); Label ques1 = new Label(); ques1.Text = "Question 7: Which is the Capital of India?"; ques1.Location = new Point(10,20); //Set label position ques1.Size = new Size (475,25); ques1.Font = new Font ("Verdana", 10,FontStyle.Bold); ques1.ForeColor = Color.Red; GroupBox groupBox =new GroupBox(); groupBox.Text = "Answer"; groupBox.Location= new Point(50,60); groupBox.Size = new Size(400,175); radioButton41.Location= new Point(20,25); radioButton41.Size = new Size(50,25); radioButton41.CheckedChanged += new System.EventHandler(value4); Label ans1 = new Label(); ans1.Text = " Delhi"; ans1.Location = new Point(75,25); //Set label position ans1.Size = new Size (250,25); ans1.Font = new Font ("Verdana", 10,FontStyle.Bold); ans1.ForeColor = Color.Blue; radioButton42.Location= new Point(20,75); radioButton42.Size = new Size(50,25); radioButton42.CheckedChanged += new System.EventHandler(value4); Label ans2 = new Label(); ans2.Text = " Mumbai"; ans2.Location = new Point(75,75); //Set label position ans2.Size = new Size (250,25); ans2.Font = new Font ("Verdana", 10,FontStyle.Bold); ans2.ForeColor = Color.Blue; radioButton43.Location= new Point(20,125); radioButton43.Size = new Size(50,25); radioButton43.CheckedChanged += new System.EventHandler(value4); Label ans3 = new Label(); ans3.Text = " Banglore "; ans3.Location = new Point(75,125); //Set label position ans3.Size = new Size (250,25); ans3.Font = new Font ("Verdana", 10,FontStyle.Bold); ans3.ForeColor = Color.Blue; // Add the RadioButtons to the GroupBox. groupBox.Controls.Add(radioButton41); groupBox.Controls.Add(radioButton42); groupBox.Controls.Add(radioButton43); groupBox.Controls.Add(ans1); groupBox.Controls.Add(ans2); groupBox.Controls.Add(ans3); panel4.Controls.Add(Reg2); panel4.Controls.Add(ques1); panel4.Controls.Add(groupBox); } public void submit4_click( object sender, EventArgs e) { String b1 = ((Button)sender).Text; if(isTrue1) scoreb = scoreb+1; isTrue1 = false; Console.WriteLine("Score board :"+scoreb); showquest5(); } public void value4(object source, EventArgs e) { isTrue1 = radioButton41.Checked; } public void showquest5() { panel4.Dispose(); panel5.Location = new Point(50,60); panel5.Size = new Size(485, 320); panel5.BackColor = Color.Cyan; this.Controls.Add(panel5); Button Reg2 = new Button(); Reg2.Text= "Next9"; Reg2.Location= new Point(200,270); Reg2.Size = new Size(75,25); Reg2.Font= new Font("verdana",10,FontStyle.Bold); Reg2.ForeColor=Color.Yellow; Reg2.BackColor=Color.Red; Reg2.Click+= new EventHandler(submit5_click); Label ques1 = new Label(); ques1.Text = "Question 8: What is the date of Indian Parliament Attack?"; ques1.Location = new Point(10,20); //Set label position ques1.Size = new Size (475,25); ques1.Font = new Font ("Verdana", 10,FontStyle.Bold); ques1.ForeColor = Color.Red; GroupBox groupBox =new GroupBox(); groupBox.Text = "Answer"; groupBox.Location= new Point(50,60); groupBox.Size = new Size(400,175); radioButton51.Location= new Point(20,25); radioButton51.Size = new Size(50,25); radioButton51.CheckedChanged += new System.EventHandler(value5); Label ans1 = new Label(); ans1.Text = " 9 December "; ans1.Location = new Point(75,25); //Set label position ans1.Size = new Size (250,25); ans1.Font = new Font ("Verdana", 10,FontStyle.Bold); ans1.ForeColor = Color.Blue; radioButton52.Location= new Point(20,75); radioButton52.Size = new Size(50,25); radioButton52.CheckedChanged += new System.EventHandler(value5); Label ans2 = new Label(); ans2.Text = " 11 December"; ans2.Location = new Point(75,75); //Set label position ans2.Size = new Size (250,25); ans2.Font = new Font ("Verdana", 10,FontStyle.Bold); ans2.ForeColor = Color.Blue; radioButton53.Location= new Point(20,125); radioButton53.Size = new Size(50,25); radioButton53.CheckedChanged += new System.EventHandler(value5); Label ans3 = new Label(); ans3.Text = " 13 December "; ans3.Location = new Point(75,125); //Set label position ans3.Size = new Size (250,25); ans3.Font = new Font ("Verdana", 10,FontStyle.Bold); ans3.ForeColor = Color.Blue; // Add the RadioButtons to the GroupBox. groupBox.Controls.Add(radioButton51); groupBox.Controls.Add(radioButton52); groupBox.Controls.Add(radioButton53); groupBox.Controls.Add(ans1); groupBox.Controls.Add(ans2); groupBox.Controls.Add(ans3); panel5.Controls.Add(Reg2); panel5.Controls.Add(ques1); panel5.Controls.Add(groupBox); } public void submit5_click( object sender, EventArgs e) { String b1 = ((Button)sender).Text; if(isTrue1) scoreb = scoreb+1; isTrue1 = false; Console.WriteLine("Score board :"+scoreb); showquest6(); } public void value5(object source, EventArgs e) { isTrue1 = radioButton53.Checked; } public void showquest6() { panel5.Dispose(); panel6.Location = new Point(50,60); panel6.Size = new Size(485, 320); panel6.BackColor = Color.Cyan; this.Controls.Add(panel6); Button Reg2 = new Button(); Reg2.Text= "Next10"; Reg2.Location= new Point(200,270); Reg2.Size = new Size(75,25); Reg2.Font= new Font("verdana",10,FontStyle.Bold); Reg2.ForeColor=Color.Yellow; Reg2.BackColor=Color.Red; Reg2.Click+= new EventHandler(submit6_click); Label ques1 = new Label(); ques1.Text = "Question 9:Who is Oscar winner this Year for the Best Actress?"; ques1.Location = new Point(10,20); //Set label position ques1.Size = new Size (475,40); ques1.Font = new Font ("Verdana", 10,FontStyle.Bold); ques1.ForeColor = Color.Red; GroupBox groupBox =new GroupBox(); groupBox.Text = "Answer"; groupBox.Location= new Point(50,60); groupBox.Size = new Size(400,175); radioButton61.Location= new Point(20,25); radioButton61.Size = new Size(50,25); radioButton61.CheckedChanged += new System.EventHandler(value6); Label ans1 = new Label(); ans1.Text = " Catherina Zeta Jones"; ans1.Location = new Point(75,25); //Set label position ans1.Size = new Size (250,25); ans1.Font = new Font ("Verdana", 10,FontStyle.Bold); ans1.ForeColor = Color.Blue; radioButton62.Location= new Point(20,75); radioButton62.Size = new Size(50,25); radioButton62.CheckedChanged += new System.EventHandler(value6); Label ans2 = new Label(); ans2.Text = " Judie Foster"; ans2.Location = new Point(75,75); //Set label position ans2.Size = new Size (250,25); ans2.Font = new Font ("Verdana", 10,FontStyle.Bold); ans2.ForeColor = Color.Blue; radioButton63.Location= new Point(20,125); radioButton63.Size = new Size(50,25); radioButton63.CheckedChanged += new System.EventHandler(value6); Label ans3 = new Label(); ans3.Text = " Julie Robert "; ans3.Location = new Point(75,125); //Set label position ans3.Size = new Size (250,25); ans3.Font = new Font ("Verdana", 10,FontStyle.Bold); ans3.ForeColor = Color.Blue; // Add the RadioButtons to the GroupBox. groupBox.Controls.Add(radioButton61); groupBox.Controls.Add(radioButton62); groupBox.Controls.Add(radioButton63); groupBox.Controls.Add(ans1); groupBox.Controls.Add(ans2); groupBox.Controls.Add(ans3); panel6.Controls.Add(Reg2); panel6.Controls.Add(ques1); panel6.Controls.Add(groupBox); } public void submit6_click( object sender, EventArgs e) { String b1 = ((Button)sender).Text; if(isTrue1) scoreb = scoreb+1; isTrue1 = false; Console.WriteLine("Score board :"+scoreb); showquest7(); } public void value6(object source, EventArgs e) { isTrue1 = radioButton63.Checked; } public void showquest7() { panel6.Dispose(); panel7.Location = new Point(50,60); panel7.Size = new Size(485, 320); panel7.BackColor = Color.Cyan; this.Controls.Add(panel7); Button Reg2 = new Button(); Reg2.Text= "Score Board"; Reg2.Location= new Point(200,270); Reg2.Size = new Size(125,25); Reg2.Font= new Font("verdana",10,FontStyle.Bold); Reg2.BackColor=Color.Red; Reg2.ForeColor=Color.Cyan; Reg2.Click+= new EventHandler(submit7_click); Label ques1 = new Label(); ques1.Text = "Question 10: Who is Oscar winner this Year for the Best Actor?"; ques1.Location = new Point(10,20); //Set label position ques1.Size = new Size (475,40); ques1.ForeColor = Color.Red; ques1.Font = new Font ("Verdana", 10,FontStyle.Bold); GroupBox groupBox =new GroupBox(); groupBox.Text = "Answer"; groupBox.Location= new Point(50,60); groupBox.Size = new Size(400,175); radioButton71.Location= new Point(20,25); radioButton71.Size = new Size(50,25); radioButton71.CheckedChanged += new System.EventHandler(value7); Label ans1 = new Label(); ans1.Text = " Tom Hank"; ans1.Location = new Point(75,25); //Set label position ans1.Size = new Size (250,25); ans1.Font = new Font ("Verdana", 10,FontStyle.Bold); ans1.ForeColor = Color.Blue; radioButton72.Location= new Point(20,75); radioButton72.Size = new Size(50,25); radioButton72.CheckedChanged += new System.EventHandler(value7); Label ans2 = new Label(); ans2.Text = " Harison Ford "; ans2.Location = new Point(75,75); //Set label position ans2.Size = new Size (250,25); ans2.Font = new Font ("Verdana", 10,FontStyle.Bold); ans2.ForeColor = Color.Blue; radioButton73.Location= new Point(20,125); radioButton73.Size = new Size(50,25); radioButton73.CheckedChanged += new System.EventHandler(value7); Label ans3 = new Label(); ans3.Text = "Russel Crow"; ans3.Location = new Point(75,125); //Set label position ans3.Size = new Size (250,25); ans3.Font = new Font ("Verdana", 10,FontStyle.Bold); ans3.ForeColor = Color.Blue; // Add the RadioButtons to the GroupBox. groupBox.Controls.Add(radioButton71); groupBox.Controls.Add(radioButton72); groupBox.Controls.Add(radioButton73); groupBox.Controls.Add(ans1); groupBox.Controls.Add(ans2); groupBox.Controls.Add(ans3); panel7.Controls.Add(Reg2); panel7.Controls.Add(ques1); panel7.Controls.Add(groupBox); } public void submit7_click( object sender, EventArgs e) { String b1 = ((Button)sender).Text; if(isTrue1) scoreb = scoreb+1; isTrue1 = false; Console.WriteLine("Score board :"+scoreb); scoreboard(); } public void value7(object source, EventArgs e) { isTrue1 = radioButton73.Checked; } public void scoreboard () { panel7.Dispose(); Label ans1 = new Label(); ans1.Text = "Your Score is : "+ scoreb; ans1.Location = new Point(200,75); //Set label position ans1.Size = new Size (250,25); ans1.Font = new Font ("Verdana", 10,FontStyle.Bold); ans1.ForeColor = Color.Blue; this.Text= "Score Board"; this.Controls.Add(ans1); } } public class Client:Form { Form app1; TextBox Ft_name = new TextBox(); TextBox Lt_name = new TextBox(); TextBox emait = new TextBox(); TextBox t_add = new TextBox(); TextBox t_pass = new TextBox(); TextBox t_pho = new TextBox(); ComboBox cb = new ComboBox(); Button submit = new Button(); public Client(Form app) { app1 = app; this.Text = "Client GUI";// This will set Title for application window this.Size = new Size(400,500); this.MaximizeBox = false;//window can not be maximized by setting this property. this.BackColor = Color.Pink; this.StartPosition = FormStartPosition.CenterScreen;// set window in the middle of screen. this.FormBorderStyle = FormBorderStyle.Fixed3D;// setting this property window can not be resize. this.AutoScroll = true; this.AcceptButton=submit; textBox() ; } public void textBox() { Console.WriteLine("Inside textBox function"); Label Fl_name = new Label(); Fl_name.Text = "First Name :"; Fl_name.Location = new Point(10,10); //Set label position Fl_name.Size = new Size (112,23); Fl_name.Font = new Font ("Verdana", 10,FontStyle.Italic); Fl_name.ForeColor = Color.Red; this.Controls.Add(Fl_name);//Adding label into the form Ft_name.TabIndex = 0; Ft_name.Location = new Point(125,10); Ft_name.Size = new Size (232,20); Ft_name.Font = new Font ("Arial", 10,FontStyle.Bold); Ft_name.MaxLength = 15;// Only 15 or less than 15 characyters can be enter in the textbox. Ft_name.BorderStyle= System.Windows.Forms.BorderStyle.Fixed3D; this.Controls.Add( Ft_name); Label Ll_name = new Label();//Last name Ll_name.Text = "Last Name :"; Ll_name.Location = new Point(10,45); Ll_name.Size = new Size (112,23); Ll_name.Font = new Font ("Verdana", 10); Ll_name.ForeColor = Color.Red; this.Controls.Add(Ll_name); Lt_name.TabIndex = 1; Lt_name.Location = new Point(125,45); Lt_name.Size = new Size (232,20); Lt_name.Font = new Font ("Arial", 10,FontStyle.Bold); Lt_name.BorderStyle= System.Windows.Forms.BorderStyle.Fixed3D; Lt_name.MaxLength = 15; this.Controls.Add(Lt_name); Label email = new Label();//Email email.Text = "Email ID :"; email.Location = new Point(10,75); email.Size = new Size (112,23); email.Font = new Font ("Verdana", 10); email.ForeColor = Color.Red; this.Controls.Add(email); emait.TabIndex = 2; emait.Location = new Point(125,75); emait.Size = new Size (232,20); emait.Font = new Font ("Arial", 10,FontStyle.Bold); emait.BorderStyle= System.Windows.Forms.BorderStyle.Fixed3D; emait.MaxLength = 40; this.Controls.Add(emait); Label l_add = new Label();//Address l_add.Text = "Address :"; l_add.Location = new Point(10,110); l_add.Size = new Size (112,23); l_add.Font = new Font ("Verdana", 10); l_add.ForeColor = Color.Red; this.Controls.Add(l_add); t_add.TabIndex = 2; t_add.Location = new Point(125,110); t_add.Size = new Size (232,20); t_add.Font = new Font ("Arial", 10,FontStyle.Bold); t_add.BorderStyle= System.Windows.Forms.BorderStyle.Fixed3D; t_add.MaxLength = 50; this.Controls.Add(t_add); Label l_pass = new Label();//Password l_pass.Text = "Password :"; l_pass.Location = new Point(10,145); l_pass.Size = new Size (112,23); l_pass.Font = new Font ("Verdana", 10); l_pass.ForeColor = Color.Red; this.Controls.Add(l_pass); t_pass.TabIndex = 3; t_pass.Location = new Point(125,145); t_pass.Size = new Size (232,20); t_pass.Font = new Font ("Arial", 10,FontStyle.Bold); t_pass.PasswordChar = '*';// setting "*" to display for entered data in the text box. t_pass.BorderStyle= System.Windows.Forms.BorderStyle.Fixed3D; t_pass.MaxLength = 12; this.Controls.Add(t_pass); Label l_pho = new Label();//Phone l_pho.Text = "Phone Number :"; l_pho.Location = new Point(10,180); l_pho.Size = new Size (118,23); l_pho.Font = new Font ("Verdana", 10); l_pho.ForeColor = Color.Red; this.Controls.Add(l_pho); t_pho.TabIndex = 4; t_pho.Location = new Point(125,180); t_pho.Size = new Size (232,20); t_pho.Font = new Font ("Arial", 10,FontStyle.Bold); t_pho.BorderStyle= System.Windows.Forms.BorderStyle.Fixed3D; t_pho.MaxLength = 12; this.Controls.Add(t_pho); Label l_sub = new Label();//Check box label l_sub.Text = "Subcription :"; l_sub.Location = new Point(10,215); l_sub.Size = new Size (112,23); l_sub.Font = new Font ("Verdana", 10); l_sub.ForeColor = Color.Red; this.Controls.Add(l_sub); Label l_rb1 = new Label();// RadioButton 1 label l_rb1.Text = " Weekly "; l_rb1.Location = new Point(140,230); l_rb1.Size = new Size (60,17); l_rb1.Font = new Font ("Verdana", 10); l_rb1.ForeColor = Color.Blue; this.Controls.Add(l_rb1); RadioButton rb1=new RadioButton(); rb1.TabIndex =5; rb1.Location = new Point(205,228); rb1.Checked = true; this.Controls.Add(rb1); Label l_rb2 = new Label();// RadioButton 2 l_rb2.Text = " Monthly "; l_rb2.Location = new Point(140,255); l_rb2.Size = new Size (66,17); l_rb2.Font = new Font ("Verdana", 10); l_rb2.ForeColor = Color.Blue; this.Controls.Add(l_rb2); RadioButton rb2=new RadioButton(); rb2.TabIndex =6; rb2.Location = new Point(205,255); this.Controls.Add(rb2); Label l_rb3 = new Label();// RadioButton 3 l_rb3.Text = " Yearly "; l_rb3.Location = new Point(140,285); l_rb3.Size = new Size (66,17); l_rb3.Font = new Font ("Verdana", 10); l_rb3.ForeColor = Color.Blue; this.Controls.Add(l_rb3); RadioButton rb3 = new RadioButton(); rb3.TabIndex =7; rb3.Location = new Point(205,282); this.Controls.Add(rb3); Label l_tech = new Label(); l_tech.Text = "Technology :"; l_tech.Location = new Point(10,310); l_tech.Size = new Size(112,23); l_tech.Font = new Font ("Verdana", 10); l_tech.ForeColor = Color.Red; this.Controls.Add(l_tech); Label l_chk1 = new Label();//Check box label l_chk1.Text = "Java "; l_chk1.Location = new Point(125,325); l_chk1.Size = new Size (50,17); l_chk1.Font = new Font ("Verdana", 10); l_chk1.ForeColor = Color.Blue; this.Controls.Add(l_chk1); CheckBox chk1=new CheckBox(); chk1.TabIndex = 8; chk1.Location = new Point(185,322); chk1.Width = 35 ; chk1.Checked= true; this.Controls.Add(chk1); Label l_chk2 = new Label();//Check box label--vertical l_chk2.Text = "C Sharp(C#) "; l_chk2.Location = new Point(125,355); l_chk2.Size = new Size (60,17); l_chk2.Font = new Font ("Verdana", 10); l_chk2.ForeColor = Color.Blue; this.Controls.Add(l_chk2); CheckBox chk2=new CheckBox(); chk2.TabIndex = 9; chk2.Location = new Point(185,352); chk2.Width = 35 ; this.Controls.Add(chk2); Label hl_chk1 = new Label();//Check box label--horizontal hl_chk1.Text = " ASP "; hl_chk1.Location = new Point(250,325); hl_chk1.Size = new Size (60,17); hl_chk1.Font = new Font ("Verdana", 10); hl_chk1.ForeColor = Color.Blue; this.Controls.Add(hl_chk1); CheckBox chkh1=new CheckBox(); chkh1.TabIndex = 9; chkh1.Location = new Point(310,322); chkh1.Width = 35 ; this.Controls.Add(chkh1); Label hl_chk2 = new Label();//Check box label--horizontal hl_chk2.Text = " VB "; hl_chk2.Location = new Point(250,355); hl_chk2.Size = new Size (60,17); hl_chk2.Font = new Font ("Verdana", 10); hl_chk2.ForeColor = Color.Blue; this.Controls.Add(hl_chk2); CheckBox chkh2=new CheckBox(); chkh2.TabIndex = 10; chkh2.Location = new Point(310,352); chkh2.Width = 35 ; this.Controls.Add(chkh2); Label l_cb = new Label();//ComboBox label l_cb .Text = " Experience :"; l_cb .Location = new Point(10,380); l_cb .Size = new Size (100,17); l_cb .Font = new Font ("Verdana", 10); l_cb .ForeColor = Color.Red; this.Controls.Add(l_cb ); cb.Location = new Point(125,380); cb .Size = new Size (232,20); cb .Font = new Font ("Verdana", 10); cb.Text= " Select One Option "; cb.Items.Add("Freshers"); cb.Items.Add("Less than 6 Months"); cb.Items.Add("From 6 Months To 2 Years"); cb.Items.Add("From 2 Months To 5 Years"); cb.Items.Add("From 5 Months To 10 Years"); cb.Items.Add("Above 10 Years"); this.Controls.Add(cb ); submit.Text= "Submit"; submit.Location= new Point(100,430); submit.Size = new Size(75,25); submit.Font= new Font("verdana",10,FontStyle.Bold); submit.BackColor=Color.Red; submit.ForeColor=Color.Yellow; submit.Click+= new EventHandler(submit_click); this.Controls.Add(submit); Button reset = new Button(); reset.Text= " Reset"; reset.Location= new Point(200,430); reset.Size = new Size(75,25); reset.Font= new Font("verdana",10,FontStyle.Bold); reset.ForeColor=Color.Yellow; reset.BackColor=Color.Red; reset.Click+= new EventHandler(reset_click); this.Controls.Add(reset); } public void submit_click( object sender, EventArgs e) { Boolean flag = true; String s =Ft_name.Text; if(s=="") { MessageBox.Show("Please enter First Name ","Confirmation Message",MessageBoxButtons.OK,MessageBoxIcon.Information); flag = false; Ft_name.Focus(); } else { String s1 =Lt_name.Text; if(s1=="") { MessageBox.Show("Please enter Last Name ","Confirmation Message",MessageBoxButtons.OK,MessageBoxIcon.Information); flag = false; Lt_name.Focus(); } else { String s2 =t_add.Text; if(s2=="") { MessageBox.Show("Please enter Address ","Confirmation Message",MessageBoxButtons.OK,MessageBoxIcon.Information); flag = false; t_add.Focus(); } else { String s3 =t_pass.Text; if(s3=="") { MessageBox.Show("Please enter Password ","Confirmation Message",MessageBoxButtons.OK,MessageBoxIcon.Information); flag = false; t_pass.Focus(); } else { String s4 =t_pho.Text; if(s4=="") { MessageBox.Show("Please enter Phone Number ","Confirmation Message",MessageBoxButtons.OK,MessageBoxIcon.Information); flag = false; t_pho.Focus(); } else { String s5 =emait.Text; if(s5=="") { MessageBox.Show("Please enter Email Id ","Confirmation Message",MessageBoxButtons.OK,MessageBoxIcon.Information); flag = false; emait.Focus(); } else if(emait.Text.IndexOf(".") == -1 || emait.Text.IndexOf("@") == -1) { MessageBox.Show("Please check for . and @ in the Email Id ","Confirmation Message",MessageBoxButtons.OK,MessageBoxIcon.Information); flag = false; emait.Focus(); } else { String s6 =cb.Text; if(s6==" Select One Option ") { MessageBox.Show("Please select proper Experience ","Confirmation Message",MessageBoxButtons.OK,MessageBoxIcon.Information); flag = false; cb.Focus(); } } } } } } } if(flag) { this.Dispose(); Console.WriteLine("after disposing"); app1.Show(); Label message = new Label(); message.Text = "Form has been submited Successfully :"; message.Location = new Point(110,5); //Set label position message.Size = new Size (300,20); message.Font = new Font ("Verdana", 10,FontStyle.Italic); message.ForeColor = Color.Green; app1.Controls.Add(message);//Adding label into the form } } public void reset_click( object sender, EventArgs e) { Console.WriteLine(" In side Reset"); Ft_name.Text =""; Lt_name.Text =""; emait.Text =""; t_add.Text =""; t_add.Text =""; t_pass.Text =""; t_pho.Text =""; cb.Text= "Select One Option"; Ft_name.Focus(); } }
Wednesday, March 24, 2010
Dropdown Problem with Firefox
Basically, what Firefox did was keep the
right div floating to the right, but underneath the middle div. This
wasn’t what I wanted at all, and after about half an hour of fiddling
with the code I managed to fix it.
The Solution
All I did was cut the right div out of the code and placed it before
the middle div. It still had the same style attributes, all that was
needed was a quick swap. And hey presto! It worked!
The CSS
#left {float: left;}
#middle {float: left;}
#right {float: right;}
improving image quality
As you can see, the image is smaller again, but the quality has
improved. First I have to remove part of the coding which makes that
icon and link to Picasa. In order to change the image size, this time I
don't have the width and height parameters showing in the code. Yeah.
But what I can do is turn the s400 number, which does show in there,
into s800.
So from this:
We change to this:
So from this:
<div
style='text-align:center;margin:0px auto 10px;'><a
href='http://1.bp.blogspot.com/_kwcZ6o5jNCw/SqY2LNT8H9I/AAAAAAAAEjQ/2VsHtsfcuiY/s1600-h/indian+child-1.jpg'><img
src='http://1.bp.blogspot.com/_kwcZ6o5jNCw/SqY2LNT8H9I/AAAAAAAAEjQ/2VsHtsfcuiY/s400/indian+child-1.jpg' border='0'
alt='' /></a> </div><div style='clear:both;
text-align:CENTER'><a
href='http://picasa.google.com/blogger/' target='ext'><img
src='http://photos1.blogger.com/pbp.gif' alt='Posted by Picasa'
style='border: 0px none ; padding: 0px; background: transparent none
repeat scroll 0% 50%; -moz-background-clip: initial;
-moz-background-origin: initial; -moz-background-inline-policy:
initial;' align='middle' border='0' /></a></div>
We change to this:
<div
style='text-align:center;margin:0px auto 10px;'><a
href='http://1.bp.blogspot.com/_kwcZ6o5jNCw/SqY2LNT8H9I/AAAAAAAAEjQ/2VsHtsfcuiY/s1600-h/indian+child-1.jpg'><img
src='http://1.bp.blogspot.com/_kwcZ6o5jNCw/SqY2LNT8H9I/AAAAAAAAEjQ/2VsHtsfcuiY/s800/indian+child-1.jpg' border='0'
alt='' /></a> </div><div style='clear:both;
text-align:CENTER'></div>
WPF - Overwrite the Main method
Sometimes we need larger control over our program. In WPF the
framework hide the Main method from us (altough it can be found in the
obj/debug(or release) directory in the App.g.cs file). Fortunately it's
possible to overwrite this method:
Step 1: open the Visual Studio with the WPF project
Step 2: in Solution Explorer right click on the App.xaml select Properties and set the Build Action to Page (so we can define resources here, as normal)
Step3: add a new class file to the project:
Step 1: open the Visual Studio with the WPF project
Step 2: in Solution Explorer right click on the App.xaml select Properties and set the Build Action to Page (so we can define resources here, as normal)
Step3: add a new class file to the project:
public class MyApplication { [STAThread] static public void Main(string[] args) { App app = new App(); //this is the default classname and not our application's
name app.InitializeComponent(); app.Run(); } }
WPF Combobox Databind to a Dataset
Often there is a question on "how to bind ComboBox to a
Dataset?". A short and simple answer, You cannot. The more specific
answer is, you bind to a DataTable and not a DataSet.
There are two ways to accomplish this,
1. Setting the Path of the ItemsSource in XAML and then setting the DataSet as the DataContext.
XAML,
Code behind,
2. Setting the ItemsSource of the ComboBox to the DataTable from the DataSet.
XAML,
Code behind,
There are two ways to accomplish this,
1. Setting the Path of the ItemsSource in XAML and then setting the DataSet as the DataContext.
XAML,
< ComboBox x:Name="myComboBox" ItemsSource="{Binding Path=yourTableName}"> </ComboBox>
Code behind,
DataSet ds; //ASSUMPTION: This DataSet contains a Table named yourTableName myComboBox.DataContext = ds;
2. Setting the ItemsSource of the ComboBox to the DataTable from the DataSet.
XAML,
< ComboBox x:Name="myComboBox"> </ComboBox>
Code behind,
DataSet ds; //ASSUMPTION: This DataSet contains a Table named yourTableName myComboBox.ItemsSource = ds["yourTableName"];
Background Image of Window or Page in WPF
For Window
< Window x:Class="MyXbap.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300" > < !-- define Background Property -- > < Window.Background > < !-- Define Image Bruch and Set Image Source to Image Path-- > < ImageBrush ImageSource="Beach.jpg" >< /ImageBrush > < /Window.Background > < /Window >
For Page
< Page x:Class="MyXbap.Page2" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:triple="http://thriple.codeplex.com/" Title="Page2" Width="1000" Height="1000" > < !-- define Backgroudn Property of Page- > < Page.Background > < !-- define Image Brush and Set ImageSource Property -- > < ImageBrush ImageSource="Beach.jpg" >< /ImageBrush > < /Page.Background > < /Page >
Menu Control in WPF
In this article i will be exploring the different aspects of
Menu Control in WPF. I assume that the audience are familier with WPF
and Declarative programming using XAML. In brief WPF is the next
generation of User Interface designing. XAML helps in making clear
separation between Design and code behing logic for design. Explaining
about these is out of scope of this article, i go ahead with the
assumption that you know basics of XAML and WPF.
Menus has been an integral part of any Windows based application. In WPF there are two classes that are used for making Menu; Menu and MenuItem. A Menu is a collection of one or more MenuItem. Each MenuItem can have a Command associated with it. A MenuItem again may further have MenuItem associated with it to make submenus. In this article i will show how to work with menus.
Lets start by looking how to create a Menu (XAML Code).
This tag creates a Menu Control. Now lets look at some major properties of Menu Control.
Name --> Defines the name of the menu.
Height --> Defines the Height of the menu
Width --> Defines the width of the menu
Margin --> Defines the position of the menu
Background--> Defines backcolor of the menu
HorizontalAlignment --> Defines the horizontal alignment of the menu inside the parent control.
HorizontalContentAlignment --> Defines the horizontal alignment for the menu content.
VerticalAlignment --> Defines the vertical alignment of the menu inside the parent control.
VerticalContentAlignment --> Defines the content alignment for the menu content.
ToolTip --> defines the tooltip for the menu.
Now that we have seen the major properties its time to see them in action. A Menu tag with all its properties set may look like:
In this sample we have set properties of the menu in XAML, but this is not the only way of doing so. Properties of a menu can be set in three different ways:
In XAML manually as done above
Using Property Window
using codebeind in C# at runtime.
Now that our menu control is ready lets add some Menu items to it.
As said earlier MenuItem tag is used to add menu item to a menu.
< MenuItem Header="_File"/>
Here we create a Menu Item with name as File.
A menu item can in turn have other menu items within it as sub menus. Now lest add some more menu items to our File menu.
Here we have added 4 menu items and a separator to the File Menu
A separator is used to categorize similer menu items. It is added with < Separator/> tag.
We can also add sub menu items to sub menus.
The xaml will look like
Now lets add a tool tip to our menu item.
Now lets look at adding and event handler to menu item.
The Click event is used to associate an event handler to the menu item.
And in the code behind
Menus has been an integral part of any Windows based application. In WPF there are two classes that are used for making Menu; Menu and MenuItem. A Menu is a collection of one or more MenuItem. Each MenuItem can have a Command associated with it. A MenuItem again may further have MenuItem associated with it to make submenus. In this article i will show how to work with menus.
Lets start by looking how to create a Menu (XAML Code).
< Menu Name="Menu1" HorizontalAlignment="Left" VerticalAlignment="Top"/ >
This tag creates a Menu Control. Now lets look at some major properties of Menu Control.
Name --> Defines the name of the menu.
Height --> Defines the Height of the menu
Width --> Defines the width of the menu
Margin --> Defines the position of the menu
Background--> Defines backcolor of the menu
HorizontalAlignment --> Defines the horizontal alignment of the menu inside the parent control.
HorizontalContentAlignment --> Defines the horizontal alignment for the menu content.
VerticalAlignment --> Defines the vertical alignment of the menu inside the parent control.
VerticalContentAlignment --> Defines the content alignment for the menu content.
ToolTip --> defines the tooltip for the menu.
Now that we have seen the major properties its time to see them in action. A Menu tag with all its properties set may look like:
< Menu Height="20" Width="200" Name="Menu1" HorizontalAlignment="Left" HorizontalContentAlignment="Left" VerticalAlignment="Top" VerticalContentAlignment="Top" Background="BlanchedAlmond">
In this sample we have set properties of the menu in XAML, but this is not the only way of doing so. Properties of a menu can be set in three different ways:
In XAML manually as done above
Using Property Window
using codebeind in C# at runtime.
Now that our menu control is ready lets add some Menu items to it.
As said earlier MenuItem tag is used to add menu item to a menu.
< MenuItem Header="_File"/>
Here we create a Menu Item with name as File.
A menu item can in turn have other menu items within it as sub menus. Now lest add some more menu items to our File menu.
< MenuItem Header="_File"> < MenuItem Header="_New"/> < MenuItem Header="_Open"/> < Separator/> < MenuItem Header="_Save"/> < MenuItem Header="Save _All"/> < /MenuItem>
Here we have added 4 menu items and a separator to the File Menu
A separator is used to categorize similer menu items. It is added with < Separator/> tag.
We can also add sub menu items to sub menus.
The xaml will look like
< MenuItem Header="_File"> < MenuItem Header="_New"/> < MenuItem Header="_Open"/> < Separator/> < MenuItem Header="_Save"> < MenuItem Header="Save _File"/> < MenuItem Header="Save A_ll"/> < MenuItem Header="Save _As"/> < /MenuItem> < /MenuItem>
Now lets add a tool tip to our menu item.
< MenuItem Header="_New"> < MenuItem.ToolTip> < ToolTip> Create new File < /ToolTip> < /MenuItem.ToolTip> < /MenuItem>
Now lets look at adding and event handler to menu item.
< MenuItem Header="_New" Click="MenuItem_Click"/>
The Click event is used to associate an event handler to the menu item.
And in the code behind
private void MenuItem_Click(object sender, RoutedEventArgs e) { MessageBox.Show("Menu item Clicked"); }
WPF Command bindings - Mouse clicks
The following article discusses the WPF command binding
feature with relation to Mouse clicks.
One of WPF powerful features is the binding of commands to controls. If you have used the MVVM design pattern, you will know how useful, powerful, easy, efficient and robust the application gets with WPF commands.
A simple scenario of a command binding would be,
When the button is clicked, first TestCommandCanExecute will be invoked and then as usual TestCommandExecute.
In addition to the simple scenarios of binding a command to a click event we also need to bind specific commands to specific events. For e.g., how about binding one command to Right Click and other to Left click ?
No problems at all. We can use the Mouse bindings to bind a specific command to a specific Mouse event.
Something like this,
The other Mouse actions supported for Mouse bindings are - LeftClick, RightClick, MiddleClick, WheelClick, LeftDoubleClick, RightDoubleClick, MiddleDoubleClick
I have attached an application demonstrating this behavior.
Here is the XAML,
This is the code behind,
One of WPF powerful features is the binding of commands to controls. If you have used the MVVM design pattern, you will know how useful, powerful, easy, efficient and robust the application gets with WPF commands.
A simple scenario of a command binding would be,
<Window>
<Window.CommandBindings>
<CommandBinding Command="Help"
CanExecute="TestCommandCanExecute"
Executed="TestCommandExecute" />
</Window.CommandBindings>
<Grid>
<Button Command="Help" Content="Click me" />
</Grid>
</Window>
When the button is clicked, first TestCommandCanExecute will be invoked and then as usual TestCommandExecute.
In addition to the simple scenarios of binding a command to a click event we also need to bind specific commands to specific events. For e.g., how about binding one command to Right Click and other to Left click ?
No problems at all. We can use the Mouse bindings to bind a specific command to a specific Mouse event.
Something like this,
<Button Content="Click Me">
<Button.InputBindings>
<MouseBinding Command="TestCommandLeft" MouseAction="RightClick" />
<MouseBinding Command="TestCommandRight" MouseAction="RightClick" />
</Button.InputBindings>
</Button>
The other Mouse actions supported for Mouse bindings are - LeftClick, RightClick, MiddleClick, WheelClick, LeftDoubleClick, RightDoubleClick, MiddleDoubleClick
I have attached an application demonstrating this behavior.
Here is the XAML,
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:custom="clr-namespace:WpfApplication1"
Title="Window1" Height="300" Width="300">
<Window.CommandBindings>
<CommandBinding Command="{x:Static custom:Window1.SimpleCommand}" CanExecute="SimpleCanExecute" Executed="SimpleExecute" />
<CommandBinding Command="{x:Static custom:Window1.LeftClickCommand}" CanExecute="LeftClickCanExecute" Executed="LeftClickExecute" />
<CommandBinding Command="{x:Static custom:Window1.RightClickCommand}" CanExecute="RightClickCanExecute" Executed="RightClickExecute" />
</Window.CommandBindings>
<StackPanel Orientation="Vertical" >
<Button Content="Simple command" Command="{x:Static custom:Window1.SimpleCommand}">
</Button>
<Button Content="Complex command">
<Button.InputBindings>
<MouseBinding Command="{x:Static custom:Window1.LeftClickCommand}" MouseAction="LeftClick" />
<MouseBinding Command="{x:Static custom:Window1.RightClickCommand}" MouseAction="RightClick" />
</Button.InputBindings>
</Button>
</StackPanel>
</Window>
This is the code behind,
using System.Windows;
using System.Windows.Input;
namespace WpfApplication1
{
///
/// Interaction logic for Window1.xaml
///
public partial class Window1 : Window
{
public static RoutedCommand SimpleCommand = new RoutedCommand();
public static RoutedCommand LeftClickCommand = new RoutedCommand();
public static RoutedCommand RightClickCommand = new RoutedCommand();
public Window1()
{
InitializeComponent();
}
private void SimpleCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
e.Handled = true;
}
private void SimpleExecute(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("Simple Command");
e.Handled = true;
}
private void LeftClickCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
e.Handled = true;
}
private void LeftClickExecute(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("Left click");
e.Handled = true;
}
private void RightClickCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
e.Handled = true;
}
private void RightClickExecute(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("Right click");
e.Handled = true;
}
}
}
WPF BorderBrush
This article demonstrates the way to work with WPF
BorderBrush.
The Border control in WPF is used to draw a border along the edges of a control. The Border control has a property named BorderBrush which is the drawing brush for the border.
The way we declare the BorderBrush in XAML is different than through code. As we know, XAML automatically converts string to a specific type.
The following code demonstrates use of the Border element,
The above code has a StackPanel containing a Border for a TextBox. The BorderBursh is assigned a Blue color. This means, the Border will be a SolidColorBrush of Blue color. SolidColorBrush is the default WPF Brush.
Now, we will see how to interact with the BorderBrush in code-behind. The scenario we will look at is, finding the current color of the BorderBrush and if Blue then changing it to Red. The following code demonstrates the same,
The Border control in WPF is used to draw a border along the edges of a control. The Border control has a property named BorderBrush which is the drawing brush for the border.
The way we declare the BorderBrush in XAML is different than through code. As we know, XAML automatically converts string to a specific type.
The following code demonstrates use of the Border element,
<StackPanel> <Border BorderBrush="Blue"> <TextBox>Dummy text</TextBox> </Border> </StackPanel>
The above code has a StackPanel containing a Border for a TextBox. The BorderBursh is assigned a Blue color. This means, the Border will be a SolidColorBrush of Blue color. SolidColorBrush is the default WPF Brush.
Now, we will see how to interact with the BorderBrush in code-behind. The scenario we will look at is, finding the current color of the BorderBrush and if Blue then changing it to Red. The following code demonstrates the same,
Color clr = (brdr.BorderBrush as SolidColorBrush).Color; if (clr.Equals(Colors.Blue)) brdr.BorderBrush = new SolidColorBrush(Color.Red);
WPF Menu Shortcut and Hot keys
We often use Menu items in our forms having Hot keys and
shortcut keys. This is a little bit tricky to use in WPF.
In Windows forms we had an options of adding "&" before the letter we wanted to be the Hot Key. In WPF this is changed to "_".
Running this will display a "_", indicatin a hot key, for "F"
For providing shortcut keys you need to use the InputGestureText property,
I have added a submenu item which will be invoked using the Ctrl+X key. Just adding the InputGesture won't actually fire the menu event. We need to also handling the event. This can be done in two ways, either hook to the key strokes to see if "Ctrl+X" is pressed or bind the Command using a Command binding.
This can be done as follows,
In Windows forms we had an options of adding "&" before the letter we wanted to be the Hot Key. In WPF this is changed to "_".
<Menu DockPanel.Dock="Top"> <MenuItem Header="_File"> </MenuItem> </Menu>
Running this will display a "_", indicatin a hot key, for "F"
For providing shortcut keys you need to use the InputGestureText property,
<Menu DockPanel.Dock="Top"> <MenuItem Header="_File"> <MenuItem Header="Exit" InputGestureText="Ctrl+X"> </MenuItem> </MenuItem> </Menu>
I have added a submenu item which will be invoked using the Ctrl+X key. Just adding the InputGesture won't actually fire the menu event. We need to also handling the event. This can be done in two ways, either hook to the key strokes to see if "Ctrl+X" is pressed or bind the Command using a Command binding.
This can be done as follows,
<Window.CommandBindings> <CommandBinding Command="{x:Static custom:Window1.MenuRoutedCommand}" Executed="ExecutedCustomCommand" CanExecute="CanExecuteCustomCommand" /> </Window.CommandBindings> <Menu DockPanel.Dock="Top"> <MenuItem Header="_File"> <MenuItem Header="Exit" Command="{x:Static custom:Window1.MenuRoutedCommand}" </MenuItem> </MenuItem> </Menu> code behind, public static RoutedCommand MenuRoutedCommand = new RoutedCommand(); public void ExecutedCustomCommand(object sender, ExecutedRoutedEventArgs e) { MessageBox.Show("Executed"); } public void CanExecuteCustomCommand(object sender, CanExecuteRoutedEventArgs e) { Control target = e.Source as Control; if (target == null) { e.CanExecute = false; return; } e.CanExecute = true; } public Window1() { InitializeComponent(); CommandBinding customBinding = new CommandBinding( MenuRoutedCommand, ExecutedCustomCommand, CanExecuteCustomCommand); KeyGesture CloseCmdKeyGesture = new KeyGesture( Key.X, ModifierKeys.Control); CustomRoutedCommand.InputGestures.Add(CloseCmdKeyGesture); }
Subscribe to:
Posts (Atom)
-
The following article discusses the WPF command binding feature with relation to Mouse clicks. One of WPF powerful features is the bindin...
-
SQL Server has never been short of ways to read from and write to files and it is always better to use the standard techniques provided b...
-
In this article we will look at the basics of Angular.Js. This is the first part of an article series. The main objective of this series i...