Untitled

0 downloads 88 Views 369KB Size Report
3000, this means that every 3 seconds all the code inside our Timer1_Tick() will be executed. Once you double click the
Task 1 Creating a new Visual Studio Solution

Create a new Visual Studio Windows Form Application

Make sure that you have selected ‘Visual C#’ from the left-hand side. Then select ‘Windows Form Application’ from the right-hand side.

Give your new solution a meaningful name, then click on the ‘Browse’ button to select the desired folder to save the new solution to. Click the ‘OK’ button when you are ready to begin. REMEMBER C2k Users – the programming environment does not grant access to your Documents folder, all work must be saved to your USB device otherwise it will be lost.

www.CodeNI.co.uk [email protected]

My first form You will then see your default form. It is important to consider the required elements that the form is required to hold when sizing your form. To resize the form you can click and drag one of the white anchors on the edges.

You can also use the properties tab to input the exact height and width of the form. To view the properties of any control (Textbox, Picturebox etc) simply click that element and go to the properties tab.

www.CodeNI.co.uk [email protected]

Toolbox Menu To add elements to your form you will need to use the toolbox menu on the left-hand side of the screen. -

Common Controls This tab contains common elements such as textboxes, pictureboxes buttons etc.

-

Containers This tab contains containers such as groupboxes and tabviews.Useful for grouping common controls togther.

-

Compenents This tab holds useful componenets such as a Timer. We will now look into using the timer component.

www.CodeNI.co.uk [email protected]

Task 2 Add a timer component to your form

Why a timer is important Within the Unit 5 task pupils have been asked to create a ‘Reaction Test’, to facilitate this they will need to work with the Timer component. Creating a splash screen will allow them to see how the timer works (the intervals and accosiated timer_Tick event). In our first form we will use the timer to create a ‘splashscreen’. This form will be diaplyed for a number of seconds beofre automatically loading our next form.

Add a Timer to your form From the components menu, click and drag the timer component onto your form. Drop the timer onto your form and you will see the following appear below your form. Timer is automatically named ‘timer1’, this name can be changed by clicking on the timer and modifying its name in the properties tab. You can also set the ‘interval’ of the timer in the properties.

What is an Interval? The interval is the time between each timer tick event. This is measured in MS – so 1000 = 1 second. Set the Interval to 3000.

www.CodeNI.co.uk [email protected]

Task 3 Add a new Form to your solution

Create a Login Form Right click on the name of your solution – This is located on the right-hand side of the screen inside the ‘Solution Explorer’.

www.CodeNI.co.uk [email protected]

When the menu appears scroll down to the Add menu and select ‘Windows Form..’

In the new window give your new form a meaningful name. We will name this form frmLogin and click the ‘Add’ button.

www.CodeNI.co.uk [email protected]

Task 4 Create a timerTick event for timer1

\\ Now we have two forms, we will now make our first form a ‘Splash Screen’. This screen will only be displayed for a short time before loading the frmLogin form. To do this we need to add a two events to our first form.

Create a Timer1_Tick() event The first event we want to add is a Tick event for our timer – remember we set our timer interval to 3000, this means that every 3 seconds all the code inside our Timer1_Tick() will be executed. To add the Tick event to our form we need to double click on the Timer1 icon below the form.

Once you double click the Timer1 icon you will see the following code (it will open in a new Tab, you can move between Tabs at the top of the screen) using using using using

System; System.Collections.Generic; System.ComponentModel; System.Data;

using using using using using

System.Drawing; System.Linq; System.Text; System.Threading.Tasks; System.Windows.Forms;

namespace PracticalTask1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); }

}

}

private void timer1_Tick(object sender, EventArgs e) { ADD CODE HERE }

www.CodeNI.co.uk [email protected]

We now need to add code to the Timer Tick method that we have just created. This must go inside the brackets for the method { } (Where it says ADD CODE HERE) . First we want the timer to stop. We want the timer to stop otherwise it will run this code again in another 3 seconds time, this code will continue to be executed until the timer is stopped. timer1.Stop();

when typing the above line of code intellisense will pop up, this is similar to auto complete – it will show you all the available commands, variables, methods etc. We want to use the Stop( ) method.

After we stop our timer1_Tick() event from running again we want to create an instance of our next form. (Our next form in this example is frmLogin) frmLogin login = new frmLogin();

The line of code above is creating an instance of the frmLogin form, this instance is called login. Now we want to tell our new instance (login) to appear by using the following code. login.Show()

Finally we want to hide the current form, the following code will hide the current form. this.Hide();

Our completed timer1_Tick() event should look like the code below private void timer1_Tick(object sender, EventArgs e) { timer1.Stop(); frmLogin login = new frmLogin(); login.Show(); this.Hide(); }

www.CodeNI.co.uk [email protected]

Task 5 Create a Form_Load( ) event

Create a Form_Load() event Now we need to create our second event/method (this is our Load_Event( )) for our first form. The Form load event only runs once when the form first loads, we will use this load event to start our timer. To add a load event to our first form, double click on the form (make sure you are in design view). Once you double click on the form you will see the following code namespace PracticalTask1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void timer1_Tick(object sender, EventArgs e) { timer1.Stop(); frmLogin login = new frmLogin(); login.Show(); this.Hide(); }

}

}

private void Form1_Load(object sender, EventArgs e) { ADD CODE HERE }

When this form loads we want to start our timer1. As we have set it to a 3 second interval, our first form will display for 3 seconds then it will be hidden and our frmLogin form will load. Add the code below to the From1_Load() event timer1.Start();

Now test you code by pressing the start button

www.CodeNI.co.uk [email protected]