Programming a button that when clicking for the second time, the form returns to the previous position

Asked

Viewed 65 times

0

Good staff.

I have a program in C# with several formats created. Each form has Formborderstyle:None and has a button that when clicking maximizes the form.

What I intend is that by clicking that same button for the second time, the form returns to its previous size and position. How do I do this?

Regards, Entryway.

  • Not knowing the code makes it harder to help, but it has several ways to do this, the easiest and write this information in json and the second click set of properties with and heigt

1 answer

0

There are several ways to do this. Follow an example:

The code below is a Form whole. I created the same only for testing. Remember to adapt the same with your code that already exists in your Form:

public partial class Form1 : Form
{
    private int _clicks = 1;
    private int _originalWidth;
    private int _originalHeight;
    private Point _originalPosition;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if(_clicks == 1)
        {
            _originalHeight = Height;
            _originalWidth = Width;
            _originalPosition = Location;
        }

        if(_clicks == 2)
        {
            Width = _originalWidth;
            Height = _originalHeight;
            Location = _originalPosition;
            _clicks = 0;
        }

        _clicks++;
    }
}
  • In the first Click of Button, the width, height and position of the Form are saved.
  • In the second Click of Button, the previously saved values are restored and the Form returns to the same initial dimensions and position. Click counter (_clicks) is reset and behaves again as if no Click has been executed.

Browser other questions tagged

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