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 7822573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:57:24+00:00 2026-06-02T07:57:24+00:00

I have a problem with my winforms C# project. I want to move a

  • 0

I have a problem with my winforms C# project.

I want to move a new (custom) button around the form (at run time).
How can I do that?

    Button[] buttons = new Button[1000];
        int counter = 0;
        Button myText = new Button();
private void button2_Click(object sender, EventArgs e)
        {
                Button myText = new Button();
                myText.Tag = counter;
                myText.Location = new Point(x2,y2);
                myText.Text = Convert.ToString(textBox3.Text);
                this.Controls.Add(myText);
                myText.MouseMove += new MouseEventHandler(myText_MouseMove);
                myText.MouseDown += new MouseEventHandler(myText_MouseDown);
                buttons[counter] = myText;
                counter++;
        }
 public void myText_MouseMove(object sender, MouseEventArgs e)
        {
            int s = e.GetHashCode();
            int check = 0;
            for (int i = 0; i < counter; i++)
            {
                if (buttons[i].GetHashCode() == s)
                    check = i;
            }
            if (e.Button == MouseButtons.Left)
            {
                buttons[check].Left += e.X - move.X;
                buttons[check].Top += e.Y - move.Y;
            }
        }
        void myText_MouseDown(object sender, MouseEventArgs e)
        {
            move = e.Location;
        }

I use the code above to create the new button and I am trying to move it around the form.

If I code for just 1 button I am able to move it but, I want to be able to do this for more buttons as well.

  • 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-02T07:57:25+00:00Added an answer on June 2, 2026 at 7:57 am

    Try this

    public void myText_MouseMove(object sender, MouseEventArgs e)
        {
            Button button = (Button)sender;
            if (e.Button == MouseButtons.Left)
            {
                button .Left += e.X - move.X;
                button .Top += e.Y - move.Y;
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem with fancybox. I want to write a function that will run
I have simple WinForms application where modifying Windows Registry. The problem is that in
Project: Winforms, .NET framework: 3.5 (Visual Studio 2008) My problem is: I have a
i am new in project for company that have bigger enterprise/accounting system based on
I'm facing a really perplexing problem.. I have a .Net 2.0 C# WinForms project.
I have a WinForm project that I want to be usable as a console
I have a very small VS2008 Winforms project that will not start. When I
I have what seems to be a simple problem that I can't solve myself.
I have a combobox which is populated by the Keys enumeration (winforms). The problem
I have problem creating new instance of excel 2007 using VBA (from Access 2002).

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.