指定したタイマの間隔が経過し、タイマが有効である場合に発生します。
名前空間: System.Windows.Forms
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)
構文
'宣言
Public Event Tick As EventHandler
'使用
Dim instance As Timer
Dim handler As EventHandler
AddHandler instance.Tick, handler
public event EventHandler Tick
public:
event EventHandler^ Tick {
void add (EventHandler^ value);
void remove (EventHandler^ value);
}
/** @event */
public void add_Tick (EventHandler value)
/** @event */
public void remove_Tick (EventHandler value)
JScript では、イベントは使用できますが、新規に宣言することはできません。
解説
Tick イベントから Debug クラスの Assert メソッドを呼び出すと、アサーション ダイアログ ボックスを表示することによって Windows で Tick イベントが繰り返し発生する場合があるため、期待どおりに動作しない場合があります。このイベント内では、アサーションのテストを使用せずに、Write、WriteIf、WriteLine、または WriteLineIf の各メソッドを使用することをお勧めします。
イベント処理の詳細については、「イベントの利用」を参照してください。
使用例
5 秒ごとにアラームを発生させる単純な間隔タイマを実装するコード例を次に示します。アラームが発生すると、MessageBox にアラームが起動した回数が表示され、タイマの実行を継続するかどうかをユーザーに問い合わせるメッセージが表示されます。
Public Class Class1
Private Shared myTimer As New System.Windows.Forms.Timer()
Private Shared alarmCounter As Integer = 1
Private Shared exitFlag As Boolean = False
' This is the method to run when the timer is raised.
Private Shared Sub TimerEventProcessor(myObject As Object, _
myEventArgs As EventArgs)
myTimer.Stop()
' Displays a message box asking whether to continue running the timer.
If MessageBox.Show("Continue running?", "Count is: " & alarmCounter, _
MessageBoxButtons.YesNo) = DialogResult.Yes Then
' Restarts the timer and increments the counter.
alarmCounter += 1
myTimer.Enabled = True
Else
' Stops the timer.
exitFlag = True
End If
End Sub
Public Shared Sub Main()
' Adds the event and the event handler for the method that will
' process the timer event to the timer.
AddHandler myTimer.Tick, AddressOf TimerEventProcessor
' Sets the timer interval to 5 seconds.
myTimer.Interval = 5000
myTimer.Start()
' Runs the timer, and raises the event.
While exitFlag = False
' Processes all the events in the queue.
Application.DoEvents()
End While
End Sub
End Class
public class Class1 {
static System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
static int alarmCounter = 1;
static bool exitFlag = false;
// This is the method to run when the timer is raised.
private static void TimerEventProcessor(Object myObject,
EventArgs myEventArgs) {
myTimer.Stop();
// Displays a message box asking whether to continue running the timer.
if(MessageBox.Show("Continue running?", "Count is: " + alarmCounter,
MessageBoxButtons.YesNo) == DialogResult.Yes) {
// Restarts the timer and increments the counter.
alarmCounter +=1;
myTimer.Enabled = true;
}
else {
// Stops the timer.
exitFlag = true;
}
}
public static int Main() {
/* Adds the event and the event handler for the method that will
process the timer event to the timer. */
myTimer.Tick += new EventHandler(TimerEventProcessor);
// Sets the timer interval to 5 seconds.
myTimer.Interval = 5000;
myTimer.Start();
// Runs the timer, and raises the event.
while(exitFlag == false) {
// Processes all the events in the queue.
Application.DoEvents();
}
return 0;
}
}
public ref class Class1
{
private:
static System::Windows::Forms::Timer^ myTimer = gcnew System::Windows::Forms::Timer;
static int alarmCounter = 1;
static bool exitFlag = false;
// This is the method to run when the timer is raised.
static void TimerEventProcessor( Object^ /*myObject*/, EventArgs^ /*myEventArgs*/ )
{
myTimer->Stop();
// Displays a message box asking whether to continue running the timer.
if ( MessageBox::Show( "Continue running?", String::Format( "Count is: {0}", alarmCounter ), MessageBoxButtons::YesNo ) == DialogResult::Yes )
{
// Restarts the timer and increments the counter.
alarmCounter += 1;
myTimer->Enabled = true;
}
else
{
// Stops the timer.
exitFlag = true;
}
}
public:
static void Main()
{
/* Adds the event and the event handler for the method that will
process the timer event to the timer. */
myTimer->Tick += gcnew EventHandler( TimerEventProcessor );
// Sets the timer interval to 5 seconds.
myTimer->Interval = 5000;
myTimer->Start();
// Runs the timer, and raises the event.
while ( exitFlag == false )
{
// Processes all the events in the queue.
Application::DoEvents();
}
}
};
int main()
{
Class1::Main();
}
public class Class1
{
private static System.Windows.Forms.Timer myTimer =
new System.Windows.Forms.Timer();
private static int alarmCounter = 1;
private static boolean exitFlag = false;
// This is the method to run when the timer is raised.
private static void TimerEventProcessor(Object myObject,
EventArgs myEventArgs)
{
myTimer.Stop();
// Displays a message box asking whether to continue running the timer.
if (MessageBox.Show("Continue running?", "Count is: "
+ alarmCounter, MessageBoxButtons.YesNo).Equals(DialogResult.Yes)) {
// Restarts the timer and increments the counter.
alarmCounter += 1;
myTimer.set_Enabled(true);
}
else {
// Stops the timer.
exitFlag = true;
}
} //TimerEventProcessor
public static void main(String[] args)
{
/* Adds the event and the event handler for the method that will
process the timer event to the timer.
*/
myTimer.add_Tick(new EventHandler(TimerEventProcessor));
// Sets the timer interval to 5 seconds.
myTimer.set_Interval(5000);
myTimer.Start();
// Runs the timer, and raises the event.
while (exitFlag == false) {
// Processes all the events in the queue.
Application.DoEvents();
}
return;
} //main
} //Class1
プラットフォーム
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
バージョン情報
.NET Framework
サポート対象 : 2.0、1.1、1.0
.NET Compact Framework
サポート対象 : 2.0、1.0