Search code examples
c#winformsbuttonfloatingmethod-group

How to make a window floating on desktop?


I am trying to create a button to make a windows floating on the desktop using "topmost" but I can't assign a Boolean to the button, because it is a method group

 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            if(this.TopMost)
            {
                this.TopMost = true;
                Button1_Click = true;
            }
            else
            {
                this.TopMost = false;
                Button1_Click = false;
            }
        }
    }

Solution

  • The answer is actually pretty short:

    private void Button1_Click(object sender, EventArgs e)
    {
     TopMost = !TopMost;
    }
    

    You don't need to include this since you are already in the scope of your form and your if else logic can be shortened to e = !e.

    Button1_Click
    

    Is a method, you can't assign a value to it.