Link timer to a button

Asked

Viewed 94 times

0

Hello, I’m starting now to program in c++ and I’m developing a college project, it’s a periodic table. We thought of using a button for each element (118 in total). My goal is to animate the button so that when the mouse passes over it doubles in size, for this I used a Timer:

private: System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e) {
        int x = 70;
        if(x <= 140){
            button1->Size = System::Drawing::Size(x, x);
            x += 5;
        }else{ timer1-> Enabled = false;}
    }

    private: System::Void button1_MouseHover(System::Object^  sender, System::EventArgs^  e) {
        timer1 -> Enabled = true;
    }

I tried several things, but so far I could not automate this process, I wanted to create some function that linked the event timer with the button I want to animate, for example:

private: System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e, System::Windows::Forms::Button^ bt) {
        int x = 70;
        if(x <= 140){
            bt->Size = System::Drawing::Size(x, x);
            x += 5;
        }else{ timer1-> Enabled = false;}
    }

or something like:

scaleUp(System::Windows::Forms::Button^ bt);

private: System::Void button1_MouseHover(System::Object^  sender,    System::EventArgs^  e) {
        scaleUp(button1);
    }

How to create a solution to the problem without creating 118 timers?

  • 1

    the tag is wrong, that’s not C++, it’s that Microsoft dialect called "C++/CX" or something...

  • Probably the simplest solution would be to link the same Mousehover event on all buttons and within the code programmed for the event to cast the System::Object parameter for System::Windows:Forms::Button using dynamic_cast. Done this you can call scaleUp by passing as parameter the pointer of type Button.

  • I’ll try that, thank you very much.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.