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.