Need a Little Help with Windows.Forms.Timer
I'm having some difficulty implimenting a timer in my Windows.Forms project. I want the application to present a single Form for a specific period of time and close itself (currently I have the timer popping a message box with a 'hello' message).
My project is compiling properly, but the timer isn't working. I've read many posts, read the Microsoft guides and other resources and can't seem to figure this one out.
My simplistic code is as follows:
///////////////////////////////////////////////////////////////////////////////////////////
#pragma once
namespace hide {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Summary for Form1
///
/// WARNING: If you change the name of this class, you will need to change the
/// 'Resource File Name' property for the managed resource compiler tool
/// associated with all .resx files this class depends on. Otherwise,
/// the designers will not be able to interact properly with localized
/// resources associated with this form.
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
createTimer();
InitializeTimer();
}
public:
void createTimer()
{
System::Windows::Forms::Timer^ mytimer1 = gcnew
System::Windows::Forms::Timer();
}
private:
void InitializeTimer()
{
timer1->Interval = 10000;
timer1->Enabled = true;
timer1->Start();
}
void mytimer1(System::Object ^ sender, System::EventArgs ^ e)
{
MessageBox::Show("Hello", "Hello", MessageBoxButtons::OK);
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Timer^ timer1;
protected:
private: System::ComponentModel::IContainer^ components;
private:
/// <summary>
/// Required designer variable.
/// </summary>
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->components = (gcnew System::ComponentModel::Container());
this->timer1 = (gcnew System::Windows::Forms::Timer(this->components));
this->SuspendLayout();
//
// timer1
//
this->timer1->Tick += gcnew System::EventHandler(this, &Form1::timer1_Tick_1);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(829, 476);
this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::Fixed3D;
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void timer1_Tick_1(System::Object^ sender, System::EventArgs^ e) {
}
};
}
///////////////////////////////////////////////////////////////////////////////////////////////
Any help or guidance would be greatly appreciated.
Re: Need a Little Help with Windows.Forms.Timer
First, your program is C++/CLI and you shoud therefore ask your question in the appropriate Managed C++ and C++/CLI forum.
On first sight I can tell you the following about your program: You have two timer objects here, one that you probably added using the IDE (timer1) and one you create locally in your function createTimer() that is named mytimer1. These two timer objects are completely unrelated, and in particular your timer object named mytimer1 is unrelated to the function with the same name that is a member of the form class. This function is obviously intended to pop up your message box, and it would most likely do so if it ever would be called.
Moreover, the timer object named mytimer1 that you create in createTimer() is completely useless as it is bound to a local variable that goes out of scope once createTimer() finishes, i.e. instantly.
You also have an event handler for the timer1 tick event (timer1_Tick_1()) that should call MessageBox::Show(), but it is empty and thus does nothing.
Probably more details over there in the other forum.
Please use code tags when you post code next time. It will make your post much more readable.
Ah, and... Welcome to CodeGuru! :)
Re: Need a Little Help with Windows.Forms.Timer
Thank you Eri - your guidance helped me get it going. I really appreciate you taking the time.
(I looked for code tags when I pasted my code, but didn't see any available).
1 Attachment(s)
Re: Need a Little Help with Windows.Forms.Timer
Quote:
Originally Posted by
hypervista
Thank you Eri - your guidance helped me get it going. I really appreciate you taking the time.
Congratulations, you're welcome. :)
Quote:
(I looked for code tags when I pasted my code, but didn't see any available).
The Standard as well as the Advanced Post Editor has a button that looks like the one in the attached image. It will either insert code tags and place the cursor between them for you to enter code, or it will wrap the code tags around the selected text.
Re: Need a Little Help with Windows.Forms.Timer
Quote:
The Standard as well as the Advanced Post Editor has a button that looks like the one in the attached image.
Huh ? What are you talking about?
edit : holy sh!t... after many years being a member I found this option in my settings. Why isn't this enabled by default ?
Re: Need a Little Help with Windows.Forms.Timer
Quote:
Originally Posted by
Skizmo
edit : holy sh!t... after many years being a member I found this option in my settings. Why isn't this enabled by default ?
I actually don't remember any more what the original default setting in my User CP was after joining, as I changed it to the Advanced Editor before making my first post. Later I found out that the Advanced Editor has certain bugs that make it practically unusable for me (and some others), so I switched to the Standard Editor and was surprised how little I missed there. (See http://www.codeguru.com/forum/showthread.php?t=501226.)
Just curious: What was it set to in your profile before you read this thread here?
I'm always happy if I happen to know something that even helps one of the forum celebrities... :)