Start and Stop Timer Control




End Sub

Помимо этих двух функций в VB.Net сохранились функции для работы со временем: Hour() (часы), Minute() (минуты), Second() (секунды). Эти функции вырезают часы, минуты, секунды из указанно времени. Например, Minute(TimeString) - вырезает минуты из текущего времени. И с прежних версий VB остались функции для работы с датами: Day() (год), Month() (месяц), Year (год). Эти функции вырезают из текущей даты: день, месяц, год. Например, Month(DateString), вырезайет из текущей даты месяц.

Еще сохранилась функция WeekDay, она нужна для определения дня недели(например Вторник). Сейчас мы сделаем программку, которая будет определять день недели, на форму добавь еще одну метку(Label3, свойства такие же как и у предыдущих меток), вот код:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' При загрузке формы:

 

Dim Den As Integer

 

Den = Weekday(DateString) ' Определяем день недели

If Den = 0 Then Label3.Text = "Воскресенье"

If Den = 1 Then Label3.Text = "Понедельник"

If Den = 2 Then Label3.Text = "Вторник"

If Den = 3 Then Label3.Text = "Среда"

If Den = 4 Then Label3.Text = "Четверг"

If Den = 5 Then Label3.Text = "Пятница"

If Den = 6 Then Label3.Text = "Суббота"

 

End Sub

 

 

What is Timer Control?

Timer Control plays an important role in the Client side programming and Server side programming, also used in Windows Services. By using this Timer Control, windows allow you to control when actions take place without the interaction of another thread.

Use of Timer Control

We can use Timer Control in many situations in our development environment. If you want to run some code after a certain interval of time continuously, you can use the Timer control. As well as to start a process at a fixed time schedule, to increase or decrease the speed in an animation graphics with time schedule etc. you can use the Timer Control. The Visual Studio toolbox has a Timer Control that allowing you to drag and drop the timer controls directly onto a Windows Forms designer. At runtime it does not have a visual representation and works as a component in the background.

How to Timer Control?

With the Timer Control, we can control programs in millisecond, seconds, minutes and even in hours. The Timer Control allows us to set Interval property in milliseconds (1 second is equal to 1000 milliseconds). For example, if we want to set an interval of two minute we set the value at Interval property as 120000, means 120x1000.

The Timer Control starts its functioning only after its Enabled property is set to True, by default Enabled property is False.

Timer example

The following program shows a Timer example that display current system time in a Label control. For doing this, we need one Label control and a Timer Control. Here in this program, we can see the Label Control is updated each seconds because we set Timer Interval as 1 second, that is 1000 milliseconds. After drag and drop the Timer Control in the designer form, double click the Timer control and set the DateTime.Now.ToString to Label control text property.

 

Public Class Form1

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

Label1.Text = DateTime.Now.ToString

End Sub

End Class

Start and Stop Timer Control

We can control the Timer Control Object that when it start its function as well as when it stop its function. The Timer Control has a start and stop methods to perform these actions.

Here is an example for start and stop methods of the Timer Control. In this example we run this program only 10 seconds. So we start the Timer in the Form_Load event and stop the Timer after 10 seconds. We set timer Interval property as 1000 milliseconds (1 second) and in run time the Timer will execute 10 times its Tick event.

Задание:
Создайте программу, которая будет отображать:
1. Системное время
2. Системную дату
3. Количество времени прошедшее с момента запуска программы (секунды, минуты)
4. Три счетчика времени (в секундах), то есть при нажатии на объект ButtonN должно быть записано время прошедшее с момента запуска программы до момента нажатия на кнопку (Button)

 

Программа также должна иметь кнопку, по которой все таймеры останавливаются.



Поделиться:




Поиск по сайту

©2015-2024 poisk-ru.ru
Все права принадлежать их авторам. Данный сайт не претендует на авторства, а предоставляет бесплатное использование.
Дата создания страницы: 2017-12-29 Нарушение авторских прав и Нарушение персональных данных


Поиск по сайту: