简体   繁体   中英

How do I make TopMost happen to all my Forms?

I have a thing where you click "options" and it opens the Form "options" I want it so when you enable "TopMost" it happens on the main Form rather than the settings,

        {
            if (bunifuiOSSwitch1.Value == true)
            {
                this.TopMost = true;
            }
            else
            {
                this.TopMost = false;
            }
        }

Thats the code I have in the button

this.TopMost sets the current form that is executing the code (the settings form) to be TopMost, when you probably want some other form to be the topmost.. This means that your settings form either needs some access to whatever form you want to be topmost:

public class SettingsForm{

  private Form _mainForm;

  public SettingsForm(Form mainForm){  //settings form constructor takes mainform as a parameter
    _mainForm = mainForm;
  }

  void ApplySettingsButtonClick(...)
  {
    _mainForm.TopMost = bunifuiOSSwitch1.Value;
  }
}

And then open your settings form you pass the mainform in:

void OpenSettingsButon_Click(...){
  new SettingsForm(this).ShowDialog();
}

Or you have some shared thing that both forms can access, like the Settings mechanism, where your settings form will set Properties.Default.Settings.MainfFOrmIsTopMost = true and then your main from can react to the change (perhaps by an event handler) and set the value of topmost based on the Settings setting


Careful though; in either case, making the main form topmost will make it sit on top of the settings window.. and if the settings window is a dialog it could get stuck behind unless it is also TopMost.

Using TopMost is often something that actually should be avoided if you can..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM