Gerard2202
Gerard2202

Reputation: 151

How to move form box to a new location

I have a windows form project, and I want the whole form to change location automatically, but the truth is that I have no idea what to call, and where to call it. I have searched online, and all code I discovered was incomplete. I am fairly new at this, so it did not help me. Here is the code that I am working with, if it helps:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Media;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private SoundPlayer _soundplayer;
        public Form1()
        {
            InitializeComponent();
            SoundPlayer player = new SoundPlayer(Properties.Resources.sound);
            player.Play();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            var myForm = new Form2();
            myForm.Show();
        }


        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            _soundplayer.PlayLooping();
        }

    }
}

Upvotes: 0

Views: 4608

Answers (2)

Andy G
Andy G

Reputation: 19367

Change the Location for the form:

this.Location = new Point(400, 500);

You just need to decide which event will trigger this code; for example, the Click event of a button.

MSDN: Location

Upvotes: 4

FeliceM
FeliceM

Reputation: 4219

To position forms using the Properties window In the Properties window, choose the form from the drop-down. Set the form's StartPosition property to Manual. Type the values for the Location property, separated by a comma, to position the form, where the first number (X) is the distance from the left border of the display area and second number (Y) is the distance from the upper border of the display area. Note Expand the Location property to enter the X and Y subproperty values individually. Reference MSDN

Upvotes: 0

Related Questions