【问题标题】:C++ timer, windows forms, start on button clickC++ 计时器,Windows 窗体,单击按钮开始
【发布时间】:2020-08-31 13:51:02
【问题描述】:

我需要你的帮助。我也在尝试在 Visual Studio 2010 中使用 Windows 窗体在 C++ 中构建一个应用程序。该应用程序会稍微复杂一些,但现在我遇到的问题是如何启动一个计时器,我将它拖放到Windows 窗体,在单击名为“开始”的普通按钮时,出于某些原因,例如 timer1.Start() timer1.Stop() 或代替 . , -> 不起作用,我收到错误,在 Start 和 Stop 的左侧有一个类或其他东西。另外我不知道为什么我的计时器滴答一次,我确实将 Seconds 初始化为 1 但如果这也可以避免,那么它将保持在 0:0 会很棒。谢谢 !我只是放了相关代码,我还有其他东西,比如文本框等。我设法让一个文本框只接受数字,但是当我在另一个文本框上尝试相同的代码只接受数字时,我的“设计”面板不会再给我看windows窗体并出现错误,这将是另一个很好解决的问题。所以,button1_Click()...

   //
    // timer1
    //
    this->timer1->Enabled = true;
    this->timer1->Interval = 1000;
    this->timer1->Tick += gcnew System::EventHandler(this, &Form1::timer1_Tick);
    //
    // Time
    //
    this->Time->AutoSize = true;
    this->Time->Enabled = false;
    this->Time->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 14.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
    static_cast<System::Byte>(0)));
    this->Time->Location = System::Drawing::Point(121, 24);
    this->Time->Name = L"Time";
    this->Time->Size = System::Drawing::Size(35, 24);
    this->Time->TabIndex = 0;
    this->Time->Text = L"0:0";
    this->Time->Click += gcnew System::EventHandler(this, &Form1::Time_Click);


    #pragma endregion
    private: System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e) {


    Second = 0;
    Minute = 0;

    //to be executed on pressing the Start Button

    Second++;

    if (Second == 60)
    {
    Second = 0;
    Minute++;
    }
    Sec = Convert::ToString(Second);
    Min = Convert::ToString(Minute);
    Time ->Text = Min + ":" + Sec;

    std::string afisaj = "You have reached elapsed time";
    std::string info = "Paused";

    //std::string afisaj;
                    //std::cout << "Please, enter your full name: ";
                    //std::getline (std::cin,afisaj);
                    //std::cout << "Hello, " << name << "!\n";

           //std::string info;
                    //std::cout << "Please, enter your full name: ";
                    //std::getline (std::cin,info);
                    //std::cout << "Hello, " << name << "!\n";

    if ((Minute == 1) && (Second == 35))
    //MessageBox(NULL,"You have reached elapsed time","Paused",MB_OK);
       MessageBox::Show( "You have reached elapsed time", "Paused",MessageBoxButtons::OK, MessageBoxIcon::Exclamation );

    }


private: System::Void textBox1_KeyPress(System::Object^  sender, System::Windows::Forms::KeyPressEventArgs^  e)
{

    if(e->KeyChar == '.')
{
        if( this->textBox1->Text->Contains(".") && !this->textBox1->SelectedText->Contains(".") )
            e->Handled = true;  
    }
    // Allow negative numbers
    else if(e->KeyChar == '-' && !(this->textBox1->Text->Contains("-"))){
        e->Handled = true;
        textBox1->Text = "-" + textBox1->Text;
    }
    // Accept only digits ".", "-" and the Backspace character
    else if(!Char::IsDigit(e->KeyChar)&& e->KeyChar != 0x08){
        e->Handled = true;
    }

}
private: System::Void Time_Click(System::Object^  sender, System::EventArgs^  e) {
}
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e)
{

【问题讨论】:

    标签: c++ windows forms button timer


    【解决方案1】:
    private: System::Void MyForm_Load(System::Object^ sender, System::EventArgs^ e) {
    }
    
    private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
        button1->Visible = false;
        timer1->Start();//when u click on button -> timer start
    }
           int n = 0; int i = 1;
    private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) {
        this->BackgroundImage = imageList1->Images[n];
        if (n == 2) {
            i = -1;
        }
        if (n == 0) {
            i = 1;
        }
        n += i;
    
    }
    

    【讨论】:

      猜你喜欢
      • 2022-12-11
      • 2011-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多