Search code examples
c#winformsformsoverlayalways-on-top

C# - Window always on top problem


So i'm making an overlay, and I need it to be always on top. To do so, i just set

chackBox1.checked = true;

private void checkChanged(object sender, EventArgs e)
{
        this.TopMost = checkBox1.Checked;
}  

as it is suggested in many places. The problem here is 1) I need a checkbox button... but thats not rly an issue, I can set to to Visible = false. 2) It never works untill I manually check the box with my mouse! Even if I set it to checked, set form1.isTopMost = true (after initComponents) and call the checkChanged event, I always have to manually check the box before it always stays on top!

please help! How do I make it so that the overlay loads up always on top?

My next step is making a thread that will force the form to stay on top, but I would like to avoid this LOL

Thanks,

Dave


Solution

  • There is something else wrong. Those property change events fire whenever the property value changes, regardless of whether this was done by your code, by the control's own code or databinding. If checked is already true, it may not fire the handler until the check box state changes.

    You should fire off a MessageBox in your checkChanged(..) event to display when you programmatically set checkBox1 to true or check the checkbox manually. This will assist in debugging. Since checkChanged isn't the typical default name for this type of event, my guess is that you changed it so it could be pointing to a different event handler function altogether.