Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between form.show and form.Activate

Tags:

c#

.net

winforms

I would like to know the difference between form.show() and form.activate().

I have multiple forms that already opened and i would like to active my form that is behind another form which is the best way to call my desired form form.show() or form.activate()?

like image 1000
Syed Wahhab Avatar asked Apr 19 '18 05:04

Syed Wahhab


3 Answers

The method Show() displays the form to the user.

The method Activate() brings the form to the front (it gives the form focus).

For example:

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

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 form2 = new Form2();
        form2.Show();
        this.Activate();
    }
}

The above code will show form2 by calling form2.Show(); but form1 will be in front of form2 (in focus) because of the this.Activate(); call.

See MSDN documentation:

  • Show()
  • Activate()
like image 188
Slaven Tojic Avatar answered Sep 26 '22 14:09

Slaven Tojic


From the documentation

Form.Activate Method ()

Activating a form brings it to the front if this is the active application, or it flashes the window caption if this is not the active application. The form must be visible for this method to have any effect. To determine the active form in an application, use the ActiveForm property or the ActiveMdiChild property if your forms are in a Multiple-document interface (MDI) application.

Form.Show Method

Showing the control is equivalent to setting the Visible property to true. After the Show method is called, the Visible property returns a value of true until the Hide method is called.


Answer

I have multiple forms that already opened and i would like to active my form that is behind another form which is the best way to call my desired form form.show() or form.activate()?

If your form is already open Activate it probably the one you want

Tip : If you ever wonder what a .net method does, just go and type it into google, usually the help is the first thing that shows up, plus a myriad of other questions and answers

like image 40
TheGeneral Avatar answered Sep 25 '22 14:09

TheGeneral


form.activate() activates the form, which means if you have input elements (such as text boxes), it will focus to that particular form regardless of any other form out there. Eg. If you have shown form 1,2 and 3. And if you activate form 2, the form 2 will get focused to the user.

If you use form.show() it will just display/show the form to the user. Thus the activate() is gets the highest priority in terms of user engagement.

like image 42
Senura Dissanayake Avatar answered Sep 25 '22 14:09

Senura Dissanayake