0

This is my current code which takes the bool variable from the settings (checkbox checked or unchecked) and then sets the TopMost property for my application.

namespace POC_App
{
    public partial class mnView : Form
    {
        public mnView()
        {
            InitializeComponent();

            // Start program minimized if setting is set to true
            if (Properties.Settings.Default.setting_startmini)
            {
                this.Visible = false;
                if (this.WindowState == FormWindowState.Normal)
                {
                    this.WindowState = FormWindowState.Minimized;
                }
                this.ShowInTaskbar = false;
            }

            // Make the program on top
            if (Properties.Settings.Default.setting_alwaystop)
            {
                this.TopMost = true;
            }
        }
    }
}

The problem is that it will make my application stay on top of all other windows, but I have to restart the application every time I check or uncheck the box. What do I do to get it so I don't have to restart the application every time I set the bool setting?

Manvaril
  • 160
  • 3
  • 15
  • look [here](http://stackoverflow.com/questions/1463417/what-is-the-right-way-to-bring-a-windows-forms-application-to-the-foreground) – ZivS Apr 18 '14 at 16:57
  • Where is the code snippet that you've shown us actually located in your code? We need to see some more context. Are you checking the setting only on application startup, or are you doing it in response to some event? – Cody Gray - on strike Apr 18 '14 at 17:58
  • Added more complete code above... – Manvaril Apr 18 '14 at 20:26
  • @Manvaril, thank you for edit your post and deleting your duplicate. – gunr2171 Apr 18 '14 at 20:27

1 Answers1

0

From what I'm seeing this runs at application startup as a setting from global settings, not whenever a checkbox is checked or unchecked. I think your logic is a little off here for what your trying to do.

When checkbox is checked, this.topmost = true,

as opposed to setting the properties.settings which will work next time you restart your application

something like:

 checkBox1_CheckedChanged(object sender, EventArgs e)
  {
       if (checkbox1.checkstate = checked)
          this.TopMost = true;
          this.Focus();
   }

I didnt type theese using vb so check the spelling and grammer and such, but this sounds like the logic your trying to do. If calling from a nonmain form you might have to use this.Parent.Form etc

DidIReallyWriteThat
  • 1,033
  • 1
  • 10
  • 39
  • This is sort of correct. The code does *not* run at *compile* time. But it does appear to run only at *application startup*, not in response to user action (e.g. clicking a checkbox). – Cody Gray - on strike Apr 18 '14 at 17:57
  • @CodyGray My mistake, you are right. I was in a bit of a hurry, and for the purposes of his question theres not really a difference in compile time and application startup – DidIReallyWriteThat Apr 18 '14 at 18:52