Tuesday, February 25, 2014

Multi Document Interface


Multi Document interfaceu 

While designing an application with multiple forms init, we are setting the startup form under the program class, but as we don’t provide source code to client, When we develop a application, he doesn’t have chance to edit the class program and specify his startup Form. In that case we use Mdi Approach.

- In this approach we will be having only one form as a startup form, so client doesn’t required to change it each time. Rest of the form in application will be under the control of Main Form or start up Form.

- Here we will be having two parts:
                        - MDI Parent
                        - MDI Child

- The form which is going to be the startup form is MDI parent and to make it as Mdi parent we need to set the IsMdiProperty of form as true.

Note – An application can have only one MdiParent, all the other forms should be child of parent, which comes and sits under parent;

Eg. Visual Studio is an MdiParent which launches all the remaining form under it as Childs.

- To launch a form as child of parent, create object of form class we want to launch, set its MdiParent property with parent forms reference and then call show method.

Layout: - 

When we have more than one child form to be launched under parent the way how child form gets arranged in parent can be set with a layout, which has 4 options like:
        i.              
                 Cascade (D): - child forms are arranged one on the top of the other
      ii.            Tile Vertical: - child forms are arranged one beside the other
    iii.            Tile Horizontal: - child forms are arranged one blow the other
     iv.            ArrangeIcons: - all child icone are arranged with in the parent.

 Way to define a project with one MdiParent and its child: -

- Take a new form on the project and sets its IsMdiContainer property as true, so that it becomes as MdiParent and also set the windowstate property as maximize, so that the form gets launched to the full size of the screen.
- Place a menu strip control on a form and add two menus on it.
            1. Forms,
            2. Arrange

- Under Form menu add separator menu item for each form we want to launch
E.g. Form1, Form2, Form3 etc.

- Under Arrange menu add the menu items – ArrangeIcons, Cascade, vertical and Horizontal.

- Now write the following code:

Under Each Forms Menu Item:
Form1 f = new Form1();
f.MdiParent = this;
f.Show();


Under arrange icons menu Strip:
this.LayoutMdi(MdiLayout.ArrangeIcons);

Under Cascade menu Strip:
this.LayoutMdi(MdiLayout.Casecade);

Under Vertical menu Strip:
this.LayoutMdi(MdiLayout.TileVertical);

Under Horizontal menu Strip:
this.LayoutMdi(MdiLayout.TileHorizontal);

User Control

User Control  These are controls that are created or developed by the programmers. To consume under your application there are of two types –
  1. Creating a new control from existing controls.
  2. Inherited or Extended Controls.


- In the first case, we create new controls making use of existing controls, so to design the control first we need to define a class inheriting from the predefined class i.e. user control, which provide the container required for designing.
** Image con not be displayed
Syntax
public class StopClock : UserControl
{
  // Design the control
  // write behavior for the control
}

- In the second case without designing any control we only copy the design of an existing control and add functionalities or behavior to the control.

In this case, we define a class inheriting from the control class and we have to add new behavior.
public class NumericTextBox : TextBox
{
   // write the code for accepting only numeric’s
}

Note – To create a control in the first process we need to add an item template of the type user control, which provide a class inheriting from UserControl class.
            Where as in the second case we need to add a item template of type class and than
Inherit from the control class we want to extend. 

Developing a Control

- People working on controls or classified into two Categories:
1. Component Developer
2. Component Consumer

- The person who develops a control is a component developer and who consume the controls is a component consumer.
While developing component the developer of the control should define all the behavior required for the control and also defined any required properties, methods and event to the control.

- Properties are defined to provide access for any value of the control to consumers.
Eg: Text property for of TextBox, Checked property of CheckBox etc.

- Method is defined so that the controls can perform actions whenever required.
Eg: Clear(), Focus(), Methods of textbox
Close() method of Form etc.

- Events

While developing a control the developer of the control may not know what action has to be performed at the specific time period.

Eg: the developer of the control is not aware what should happen when button is clicked, what happen when button is clicked will be decided by component consumer even if decided by consumer, it is the responsibility of developer to execute the code written by consumer even if these two persons never come together to work.

To resolve the above problem developer must first define an event under their controls and then asked the consumer to write code under an event procedure which should be bound with the event, So whenever the event occur, event procedure gets executed.

Define an event:
[ < modifier >] event < delegate > <name>
As we know that events takes the help of delegates to execute an event procedure, so while defining an events we must also specified which delegate will execute the event.

So first we need to define a delegate and then the event

public delegate void EventHandler(object sender EventArgs e);

public event EventHandler click;

- All delegates that are predefined in BCL are defined with two parameters:


1. Object sender
2. EventArgs e

- That is the reason why all our event procedure are also taking the same two parameters ( we are already aware that I/O parameter of delegate should be same ass the I/O prams of method it has to call)

Note à While defining our own event to our controls we can define delegates with or without parameters so that the event procedure that are generated will also come with or without parameters respectively.

Create a StopClock control:

à Open a new project of type windows Forms Control library and name it as controls project.
à By default the project comes with a class userControl1 that is inherited from user control
à Open the Solution explorer and rename the file UserControl1.cs as StopClock.cs and design it as following.

Now goto the properties of Mask TextBox à select Mask property à click on the button beside it  à select time à click ok à and also set text as “0000”

Place a timer control and set its interval property as 1000

Now write the following code in code view
** Code Can not be displayed..
Now open the solution explorer à right click on the project à select built, which compile the project and generated an assembly controlsProject.dll

Consuming the Controls è To consume the controls à goto our old project à open the toolbox à right click on it à and select add tab, which adds a new tab à enter the name as csharp4  controls.
Now right click on the new tab and select choose items, which opens a dialog box à click on the browser button and select controlsProject.dll from its physical location.
c:\csharp4\controlsProject\Bin\Debug\controlsProject.dll
After selecting the assembly à click on the OK button, which adds StopClock control under the new tab we have added, which can be place on any Form and consume it.

PictureBox Control & Timer

ComboBox

Goto properties of ComboBox: - Select items property used under it and enter the list of countries 

SelectedIndexChanged It is the default event of all the above three controls, which gets raised immediately once a value is selected.

PictureBox Control: - It is used for displaying images in an application.
To display an image, the image has to be bound with the control using any of these properties:  

PictureBox

  • ImageLocation = < Path of the image>
  • Image = Image.FromFile (< Path of the image >)
  • Image = Image.FromStream ( stream stream)
Step1 – By default pictureBox control doesn’t have any border. If we want to give a border to the control, we need to set the BorderStyle Property either as a fixed single or fixed 3D.

Step2 – By default the image is bigger than the pictureBox so it will not display the complete image. To place or handle the image placement under the control we use the size mode property, which can be set with values like- Starched image, outsize and center image, default is normal.

Timer: - 

This control is used for execution of code for specified interval regularly i.e. once the specified frequency gets allows.
Timer provided Four members init that are-
  1. Interval Property: - Used for specifying the frequency for execution of the code, which should be   given in millisecond.
  2. Tick Event: - The code which should be executed by the timer should be written under his event
  3. Start : - It’s a method which starts the execution of timer.
  4. Stop : - It’s a method which stops the execution of timer.

ComboBox, ListBox and CheckListBox

** Image con not be displayed

-Goto property of fees Text box and change its name as TxtFees and set readonly  property as true, so that it becomes non editable and also set Text as zero(0).

- For every CheckBox and radio button provide a tag value, which should be the fees amount corresponding to course. Now for normal radio button use tag as zero.

Note – Tag property is also similar to text property, whereas, Tag is not visible to the end user, but both text and tag can be accessed in code by a programmer using text and tag properties.

- Now define a checked change event procedure for one CheckBox and bind this event procedure with all the other checkboxes.

- In the same way do it for radio button control also.

- Now write the following code under code view:
** Text con not be displayed

ComboBox, ListBox and CheckListBox: - we are using this control also for users to select from a list of available values.

ComboBox

it is allowing only single selection, but also allow the user to enter a new value in Run time.

ListBox

It is by default allows single selection but can be customized into multi selection.

CheckListBox

Is always by-default allows multi selection.

Adding values to the control: - 

If we want to add values to the control, we have various mechanisms:
  • Under the properties of the control  select items property and click on the button beside it, which opens a window and under it enter your required values.
  • Using Items.Add method a value can be added to the control at any time.
    < Control >. Items.Add ( object values )

  •       Using the Items.AddRange method of thee control, an array of values can be at a time bound to them.
    Eg: 
    < control > . Items.AddRange ( object [] values )
  • .      Using the data source property of the control, a data table can be bounded to the control, but again using display member property, we need to specify which column of the table has to be display.

    Eg:
    < Control > .DataSource = < data table >
    <control > DisplayMember = < Col name >

Multiple selection under ListBox

As discussed earlier a ListBox control allows only single selection, If we want to go for multi selection, we need to set the selection mode property of the control either as MultiSimple (only mouse click) or MultiExtended (ctrl + Mouse click).
Identify selected values from the controls: -

ComboBox:
- Text
String
- SelectedItem
object
SelectedIndex
int
ListBox
- SelectedItem
object
- SelectedItems
object []
-SelectedIndex
int
- SelectedIndices
int []

CheckListBox
- CheckedItems
object []
- CheckedIndices
int []

Radio Button and CheckBox

Radio Button and CheckBox

Radio Button and CheckBox: - These two controls are used when we want, the user select the values from a list of avilabel values.

Radio Button allows single selection and CheckBox allows multiple selections.

Note – Because radio button allows only single selection, when we want to use them under multiple questions, we require to group the radio buttons, that are used under the particular question.

To group them we need to place them on different container controls like- Panel and GroupBox etc.

- Both radio button and CheckBox controls has a property checked, to identify whether the control has been selected or not, which returns true if selected or else return false.


- Under Button1 click:
 {
    if (radioButton1.Checked)
       MessageBox.Show("Radio Button 1 is Selected");
    else if (radioButton2.Checked)
       MessageBox.Show("Radio Button 2 is Selected");
    else if (radioButton3.Checked)
       MessageBox.Show("Radio Button 3 is Selected");
           
  }


- Under Button2 click:
        {
            if (checkBox1.Checked)
            MessageBox.Show("Check Box1 is selected");
            if (checkBox2.Checked)
            MessageBox.Show("Check Box2 is selected");
            if (checkBox3.Checked)
            MessageBox.Show("Check Box3 is selected");
        } 

Checked Change event: - It is the default event of both the above two controls, which gets raised when the control is selected as well as deselected also.

Creating Menu in Windows Application

Creating Menu in Windows Application

- Not all events but some events have properties under them. If we want to access the propertied of an event, under its event procedure we can make use of the parameter e, which refers to properties of current executing event.

- In the above case, cancel is a property of validation event when set as true will not allow the focus to leave the control.

- If we want to close the Form even from mandatory field --> Goto properties of cancel button and set causes validation property as false, So that when we click on the button, the code under its click event gets executed before execution of textbox’s validation event.

- Now under the cancel button write the following code
  {
        txtUser.causesValidation = false;
        txtPwd.causesValidation = false;

        this.close();
  }

- When we set the causes validation property of txtBox as false, validation event of txtBox will not occur, so the form gets closed.

- Now, under the login button write the following code:
if(txtUser.Text.Trim() == “Admin” && txtPwd.Text.Trim() == “Admin”)

        MessageBox.Show(“Login successful”);
else
        MessageBox.Show(“Login failed”);

- Now under reset button:
  txtUser.Text = “”;
  txtPwd.Clear();
  txtUser.focus();

Creating Menu in Windows application

To create a menu  add menu strip control to form, which sets on top of it.

- To add a menu on menu strip  click on Left hand site corner of it, which opens a textbox to type here  Enter a name in it  which adds a menu, repeat the same process for adding multiple menus.

- To add a menu item, under a menu  click on the menu  which opens the text box blow assigning to type here  enter a name init, which adds a menu item, repeat the same process for adding multiple menu items.

Note – A menu and menu item are object of the class toolStripMenu item.
 If we want menus to be responding for alt keys of key board, prefix with “&” before the character which should response for alt.

Eg:     &File, &Edit, Format etc..

- To create a short cut for menu item so that it response to key board actions  Goto properties of menu item  Select short cut keys property   click on drop down beside it that displays a window. In it choose a modifier ctrl or alt or shift and then choose a key for ComboBox blow.

- TO group related menu item, under a menu we can add separators between menu item, to do it  Right click on menu item and select insert separator, which adds a separator on top of the menu item.

Note - same as we inserted separator, we can also insert a menu item if required in the middle.

- If we want to display an Image, beside a menu item  Right click on it and select the set image, Which opens a window  select local resource  and click on import button  which opens a DilogBlox , Using it select an image from your hard disk.

- Some times, we find check marks beside menu item to identify a property is on or off. Eg: Word Wrap under notepad. To create check mark beside a menu item --> right click on it and select checked.

Note – To check or uncheck the item in run time we need to write code explicitly under its click event as following
if ( < Control >.Checked == true)           
      < Control >.Checked = false;
else
      < Control >.Checked = true;                   

Dialog Control

These are used for displaying a list of system values from which the users can select; we have five different dialog controls like:
ColorDialog, FolderBrowserDialog, FontDialog, OpenFileDialog and SaveFileDialog

- None of the dialog controls are visible on a form even after click in them. TO make them visible we need to call method ShowDialog explicitly, which is common for all the five controls. 

- Dialog controls never perform any actions; they are only responsible for returning the values that has been chosen by the users. It was the programmer responsibility to capture those values and perform a necessary action.

To capture the values chosen use following properties 
ColorDialog
Color
FolderBrowserDialog
Select Path
FontDialog
Font
OpenFileDialog
FileName
SaveFileDialog.
File name