Programming of a button that when clicking for the 2nd time, the form returns to the previous position

0

Good people.

I have a C # program with multiple forms created. Each form has FormBorderStyle: None and has a button that when clicking maximizes the form.

What I want is that by clicking the same button a second time, the form returns to the previous size and position. How do I do that?

Regards, EntryWay.

    
asked by anonymous 09.03.2018 / 13:42

1 answer

0

There are several ways to do this. Here is an example:

The code below is an integer% with%. I created the same just 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 Form of Click , the width, height, and position of Button are saved.
  • In the second% of% of% with%, previously saved values are restored and Form returns to the same initial dimensions and the same position. The click counter ( Click ) is reset and behaves as if no Button has been executed.
09.03.2018 / 14:32