Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9140405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:30:44+00:00 2026-06-17T09:30:44+00:00

I am trying to learn .NET programming. As a part of my learning, I

  • 0

I am trying to learn .NET programming. As a part of my learning, I tried to make some effects on buttons. It is working… but not as smooth as I imagined! Is there any better way to do this? Thank you in advance!

My need:

There are 3 buttons.
When you hover the mouse over one of them, it expands and when you mouse out from that button, it returns to its initial size.

private void button1_MouseHover(object sender, EventArgs e)
    {
        button1.BackColor = Color.White;
        button1.Width = 130;
        button1.BringToFront();            
    }

    private void button1_MouseLeave(object sender, EventArgs e)
    {
        button1.BackColor = Color.Red;
        button1.Width = 75;         
    }

    private void button2_MouseHover(object sender, EventArgs e)
    {
        button2.BackColor = Color.Gray;
        button2.Width = 130;
        button2.BringToFront();            
    }

    private void Form1_MouseLeave(object sender, EventArgs e)
    {
        button2.BackColor = Color.Red;
        button2.Width = 75;

    }

    private void button3_MouseHover(object sender, EventArgs e)
    {
        button3.BackColor = Color.DimGray;
        button3.Width = 130;
        button3.BringToFront();
    }

    private void button3_MouseLeave(object sender, EventArgs e)
    {
        button3.BackColor = Color.Red;
        button3.Width = 75;

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:30:45+00:00Added an answer on June 17, 2026 at 9:30 am

    So first off, you don’t want to do the exact same thing 3 times. Create a single method to add the appropriate handlers for a button, and then just write the code once to handle any given button.

    Note that you can go into the expand/contract tick handlers and use the percentComplete value to set the height as well, to move the color along a spectrum (this would involve some mathematics of colors to do though) or to alter any other aspect of the button. If you’re really motivated to generalize it you could add a parameter to the method of Action<double> that does something to the object based on the given percent progress.

    public void AddAnimation(Button button)
    {
        var expandTimer = new System.Windows.Forms.Timer();
        var contractTimer = new System.Windows.Forms.Timer();
    
        expandTimer.Interval = 10;//can adjust to determine the refresh rate
        contractTimer.Interval = 10;
    
        DateTime animationStarted = DateTime.Now;
    
        //TODO update as appropriate or make it a parameter
        TimeSpan animationDuration = TimeSpan.FromMilliseconds(250);
        int initialWidth = 75;
        int endWidth = 130;
    
        button.MouseHover += (_, args) =>
        {
            contractTimer.Stop();
            expandTimer.Start();
            animationStarted = DateTime.Now;
            button.BackColor = Color.DimGray;
        };
    
        button.MouseLeave += (_, args) =>
        {
            expandTimer.Stop();
            contractTimer.Start();
            animationStarted = DateTime.Now;
            button.BackColor = Color.Red;
        };
    
        expandTimer.Tick += (_, args) =>
        {
            double percentComplete = (DateTime.Now - animationStarted).Ticks
                / (double)animationDuration.Ticks;
    
            if (percentComplete >= 1)
            {
                expandTimer.Stop();
            }
            else
            {
                button.Width = (int)(initialWidth +
                    (endWidth - initialWidth) * percentComplete);
            }
        };
    
        contractTimer.Tick += (_, args) =>
        {
            double percentComplete = (DateTime.Now - animationStarted).Ticks
                / (double)animationDuration.Ticks;
    
            if (percentComplete >= 1)
            {
                contractTimer.Stop();
            }
            else
            {
                button.Width = (int)(endWidth -
                    (endWidth - initialWidth) * percentComplete);
            }
        };
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to learn some ASP.NET MVC. Please bear with me but asp.net
I am trying to learn some VB.NET for my coo-op that starts next week,
I'm trying to learn some C#.net. I'm just trying to expose the AdventureWorks database
I am trying to learn more about the PHP function sprintf() but php.net did
I'm PHP developer but trying to learn ASP.NET MVC. In PHP when i need
I'm trying to learn how to use the library vcglib (http://vcg.sourceforge.net/index.php/Main_Page) but I'm having
I am a .net developer that's trying to learn how to do cocoa programming
I'm trying to learn ASP.NET MVC 4, so I'm trying to make a blog
I'm working through examples in a book trying to learn ASP.NET, and I've stumbled
I'm trying to learn more about COM, specifically .NET/COM Interop, but I've been unable

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.