Thursday 4 April 2013

Example: Multi Document Interface







using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WinForms7
{
    public partial class MidParent : Form
    {
        public MidParent()
        {
            InitializeComponent();
        }

        private void form1ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form1 f = new Form1();
            f.MdiParent = this;
            f.Show();
        }

        private void arrangeIconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.LayoutMdi(MdiLayout.ArrangeIcons);
        }

        private void form2ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form2 f = new Form2();
            f.MdiParent = this;
            f.Show();
        }

        private void form3ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form3 f = new Form3();
            f.MdiParent = this;
            f.Show();
        }

        private void cascadeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.LayoutMdi(MdiLayout.Cascade);
        }

        private void horizontalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void verticalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.LayoutMdi(MdiLayout.TileVertical);
        }
    }
}

Multi Document Interface

  • While desdignng an application it can contain any no. of forms in it, right now to run the desired form we are explicitly specifying the form class name under program class. But when we provide the app to client we only give him the assembies of the project(ie IL code). So client doesnot have a chance to edit the client program and specify the form class name he wants to run.
  • To overcome the above problem we are provided with an approach known as multi document interfaces where in this approach an application will be having on form as a startup form which is refereed to as mdiparent or mdicontainer, so that clients dont require to change the form name under program class at any time. Rest of the forms in app will be under the control of mdiparent form and refered to as mdichilds. To make a form as mdiparent set the property ismdicontainer as true. . Note:An application can have only one mdiparent and all other fprms must be childs of parent which should come and sit under parent. Eg: VS is an mdiparent and launches all other forms like "New Project", "Add New Item" as its childs.
  • To launch a form as child of parent create object of child from class, set its mdiparent property with parent forms reference and then class show() method.
Layout:
  • When we have more than one child forms opened at a time under parent, child forms will be arranged inside the parent by using a layout for arrangement which can be any of the four options like
               - Cascade: Child forms are arranged one on top of the other.
               - TileVertical:  Child forms are arranged one beside the other.
               - TileHorizontal: Child forms are arranged one on top of the other.
               - ArrangeIcons: All child forms are arranged with in the bottom of the parent.

Example:ComboBox, ListBox and CheckBox List control




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WinForms7
{
    public partial class Form5 : Form
    {
        public Form5()
        {
            InitializeComponent();
        }

        private void Form5_Load(object sender, EventArgs e)
        {
            listBox1.Items.Add("AP");
            listBox1.Items.Add("Orrisa");
            listBox1.Items.Add("Maharashtra");
            listBox1.Items.Add("MP");
            listBox1.Items.Add("UP");
            string[]cities={"Bengaluru","Chennai","Delhi","Hyderabad","Kolkata","Mumbai"};
            checkedListBox1.Items.AddRange(cities);
        }

        private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Convert.ToInt32(e.KeyChar) == 13)
                if (comboBox1.FindStringExact(comboBox1.Text) == -1)
                    comboBox1.Items.Add(comboBox1.Text);
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(comboBox1.Text);
            MessageBox.Show(comboBox1.SelectedItem.ToString());
            MessageBox.Show(comboBox1.SelectedIndex.ToString());
        }

        private void button2_Click(object sender, EventArgs e)
        {
            foreach (object obj in listBox1.SelectedItems)
                MessageBox.Show(obj.ToString());
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string str = null;
            foreach (object obj in checkedListBox1.CheckedItems)
                str += obj.ToString() + ",";
            str=str.Substring(0,str.Length-2);
            int pos=str.LastIndexOf(",");
            if(pos!=-1)
            {
                str=str.Remove(pos,1);
                str=str.Insert(pos,"and");
            }
            MessageBox.Show(str);
        }
       
    }
}

Example of RadioButton and CheckBox Control






using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WinForms7
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

          private void button1_Click(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
                MessageBox.Show("Radiobutton1 is selected");
            else if
                (radioButton2.Checked)
                MessageBox.Show("Radiobutton2 is selected");
            else if
                (radioButton3.Checked)
                MessageBox.Show("Radiobutton3 is selected");
          }

        private void button2_Click(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
                MessageBox.Show("Checkbox1 is selected");
            if (checkBox2.Checked)
                MessageBox.Show("Checkbox2 is selected");
            if (checkBox3.Checked)
                MessageBox.Show("Checkbox3 is selected");
        }
       
    }
}

Example showing an application using RadioButton and CheckBox




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WinForms7
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }
        int count = 0;

        private void checkBox1_CheckedChanged_1(object sender, EventArgs e)
        {
            {
                radioButton1.Checked = true;
                int amt = int.Parse(txtFees.Text);
                CheckBox cb = sender as CheckBox;
                if (cb.Checked)
                {
                    count += 1;
                    amt += Convert.ToInt32(cb.Tag);
                }
                else
                {
                    count -= 1;
                    amt -= Convert.ToInt32(cb.Tag);
                }
                txtFees.Text = amt.ToString();
            }
        }
      
        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            int amt=int.Parse (txtFees .Text);
            RadioButton rb = sender as RadioButton;
            if(rb.Checked )
                amt+=(Convert .ToInt32 (rb.Tag )*count );
            else
            amt-=(Convert .ToInt32 (rb.Tag )*count );
            txtFees.Text = amt.ToString();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            foreach (Control ctrl in groupBox1.Controls)
            {
                CheckBox cb = ctrl as CheckBox;
                cb.Checked = false;
            }
            foreach (Control ctrl in this.Controls)
            {
                if (ctrl.GetType().Name == "TextBox")
                {
                    TextBox tb = ctrl as TextBox;
                    tb.Clear();
                }
                txtFees.Text = "0";
                txtName.Focus();
                    
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        }
    }

Creating "Registration Form"using Windows Application

Example:  Creating registration Form in Windows Application using CSharp

Design as following, define a validating event procedure for user name textbox and bind that event procedure with both password textbox's. Now write  the following code under the Validating Event Procedure:




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WinForms7
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
        }

        private void txtname_Validating(object sender, CancelEventArgs e)
        {
            TextBox tb = sender as TextBox;
            if (tb.Text.Trim().Length == 0)
            {
                MessageBox.Show("Cannot leave empty");
                e.Cancel = true;
                return;
            }
            if (tb.Name =="txtpass")
            {
                if (tb.Text.Trim().Length < 8)
                {
                    MessageBox.Show("Pwd should be between 8 to 16 chars");
                    e.Cancel = true;
                    return;
                }
                if(tb.Name=="txtCPass")
                {
                    if(txtPass.Text .Trim()!=txtPass.Text.Trim())
                    {
                        MessageBox.Show("Conform pwd should match with pwd");
                        return;
                    }
                    btnSave.Enabled=true;
                }
            }

            }

        private void btnClose_Click(object sender, EventArgs e)
        {
            txtname.CausesValidation = false;
            txtPass.CausesValidation = false;
            txtCPass.CausesValidation = false;
            this.Close();
        }

        private void txtPhone_KeyPress(object sender, KeyPressEventArgs e)
        {
            //MessageBox.Show(Convert.ToInt32(e.KeyChar.ToString()));
            if (char.IsDigit(e.KeyChar) == false && Convert.ToInt32(e.KeyChar) != 8)
                MessageBox.Show("Enter numeric only");
            e.Handled = true;
   
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
         //need to write code for saving data to database here
            MessageBox.Show("Data saved to database");
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            foreach (Control ctrl in this.Controls)
            {
                if (ctrl.GetType().Name == "TextBox")
                {
                    TextBox tb = ctrl as TextBox;
                    tb.Clear();
                }
                btnSave.Enabled = false;
                txtname.Focus();
            }
        }
                 
      }
    }