Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

CSharp.NET Tutorial – Day 19

In previous article, we have discussed what is PictureBox Control and its 'Size Mode' Property, what are RadioButton and CheckBox and its common Boolean property Checked, some events like CheckedChanged, what is Tag Property, what are ComboBox, ListBox, and CheckedListBox, How to add values to the controls like ComboBox, ListBox and CheckedListBox, Multiple selection for ListBox and its Selection Mode, How to find the Items selected under the controls, SelectedIndexChanged along with some examples.

Please find below the link for accessing the article

CSharp.NETTutorial - Day 18

Now, in this article we will discuss what is Timer, what are user defined intervals for Timer Control like Interval, Tick, Start and Stop, What is the concept of Multi Document Interfaces, what are types of arrangements like Cascade (Default), TileVertical, TileHorizontal and ArrangeIcons along with some examples.

Timer

Timer is a control which raises an event at definite user defined intervals.  Below is the list of Timer control members.

1) Interval:  Interval is a property, using which we can set the frequency of execution for Timer and it is in milliseconds.

2) Tick:  Tick is an event which will occur whenever the specified interval is elapsed.

3) Start:  Start is a method which basically starts the execution of Timer.

4) Stop:  Stop is a method which basically stops the execution of Timer.

Let's see an example now.  Design a form like below


(<<) Prev-> btnPrev
(>>) Next->btnNext
-> Place a 'FolderBrowserDialog' and a 'Timer' Control on the form

Write the below code

using System.IO;

Declarations:

int imageIndex=0;
string dirpath;

Under form Load:

for(int i=1;i<=10;i++)
comboBox1.Items.Add (i);
comboBox1.SelectedIndex=2;

Under Load Button Click

listBox1.Items.Clear();
folderBrowserDialog1.ShowDialog();
dirpath=folderBrowserDialog1.SelectedPath;
string[ ] files=Directory.GetFiles(dirpath.".jg");
foreach(string file in files)
{
int pos=file.LastIndexOf("\\");
listBox1.Items.Add(file.Substring(pos+1);
}
listBox1.SelectedIndex=0;
imgIndex=0;
btnPrev.Enabled=false;

Under ListBox SelectedIndexChanged Event

pictureBox1.ImageLocation = dirpath +"\\"+listBox1.SelectedItem;

Under << button Click

if(imageIndex > 0)
{
imageIndex -=1;
if(imageIndex ==0)
btnPrev.Enabled=false;
if(imageIndex <listBox1.Items.Count -1)
btnNext.Enabled=true;
listBox1.SelectedIndex=imageIndex;
}

Under >> Button Click

if(imgIndex<listBox1.Items.Count -1)
{
imgIndex +=1;
if(imgIndex > 0)
btnPrev.Enabled=true;
if(imgIndex == listBox1.Items.Count -1)
btnNext.Enabled=true;
listBox1.SelectedIndex=imgIndex;
}

Under Timer Tick Event
btnNext.PerformClick();

Under Slide Show Button Click

if(btnSlide.Text=="Slide Show")
{
btnSlide.Text="Stop Show";
timer1.Interval=int.Parse (comboBox1.Text)*1000;
timer1.Start ();
}
else
{
btnSlide.Text="Slide Show";
timer.Stop ();
}

Multi Document Interfaces

"Multi Document Interfaces", is an approach that we use to work with multiple forms in a project.  At present, when we need to deal with multiple forms in a project, we are executing the required form by specifying the form name under 'Program' class.  But when the application is installed on the client system, client does not have the source code with him to specify his startup form under Program class.  Now, we need to focus on how to deal with this kind of problem.  To overcome this problem, we need to design the application as below.

-> An application should have only one startup form in it and which we can call it as 'MDIParent', where all the remaining forms will be the Child Forms of the Parent.  So it is clear that execution of application shall start from the parent and it will provide the different links to launch all the remaining forms in the project and it is parent's responsibility.

-> To make a form as an 'MDIParent', we need to set the 'IsMDIContainer' property of the form as 'true' and under this we need to create objects of all the child forms which are present in the project and we can launch them whenever required.

-> There is another scenario like when we need to open multiple child forms in a project at the same time the way how the child forms get Arranged within the parent is controlled by the 'Layout' of Parent which will allow four types of arrangements.

1) Cascade (Default):  Here, all the forms are arranged one on top of the other.

2) TileVertical:  Here, all the forms are arranged one beside the other.

3) TileHorizontal:  Here, all the forms are arranged on below the other.

4) ArrangeIcons:  Here, all the minimized icons get arranged in the bottom of the Parent.

We will see the steps one by one below...

-> Take a new Windows form in the Project, named it as 'MdiParent.cs’

-> Set 'IsMDIContainer' property of the form as 'True' to make it 'MDIParent' and also set the 'WindowState' property' of form as 'Maximized' so that the form gets launched to the full size of the screen.

-> Place a MenuStrip control on the form and add two menus in it.
1) Forms
2) Layout

-> Under Forms 'Menu', add different menu items which required one for each child form to launch.

Eg:  Form1, Form2, Form3 etc

-> Under 'Layout' menu, add the menu items 'Cascade', 'Vertical', 'Horizontal' and 'ArrangeIcons'.

-> Now under each form menu item, write the below code.

Form1 f=new Form1 ();
f.MdiParent=this;
f.Show ();

Under Cascade Menu item

this.LayoutMdi (MdiParent. Cascade);

Under Vertical MenuItem

this.LayoutMdi(MdiLayout.Vertical);

Under Horizontal MenuItem

this.LayoutMdi(MdiLayout.Horizontal);

Under ArrangeIcons MenuItem
this.LayoutMdi(MdiLayout.ArrangeIcons);

Happy Learning….!!!!!


This post first appeared on Dot Net Programming (C#, Asp.Net, ADO.Net, WCF, WPF, Ajax, LINQ), please read the originial post: here

Share the post

CSharp.NET Tutorial – Day 19

×

Subscribe to Dot Net Programming (c#, Asp.net, Ado.net, Wcf, Wpf, Ajax, Linq)

Get updates delivered right to your inbox!

Thank you for your subscription

×