Reputation: 49
I have multiple forms. One of them is called Settings and contains a checkbox with the following code:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
this.TopMost = true;
}
else if (!checkBox1.Checked)
{
this.TopMost = false;
}
}
Now, this only works when I'm on the Settings form. As soon as I open another form e.g. the Program form (this closes the Settings form) it doesn't stay on top. Through the checkbox ApplicationSettings property I already made the checkbox stay on checked/unchecked if a user closes and reopens the Settings form. But this won't make the always on top feature work on the other forms.
How can I make other forms aswell stay on top of other programs after checking the checkbox in the Settings form and closing the checkbox form?
Upvotes: 0
Views: 209
Reputation: 125277
Create a bool property in Settings file of the project, save the checkbox checked property into the settings. Then for each form, you can bind TopMost
property to that setting. You can also put binding logic inside a base form and other forms derive from it.
Example
Solution explorer → Your project → Properties node → double click on Settings.settings
Add a new property, Name = TopMost
and Type = bool
and set the value to false
and set the Scope = User
and save settings.
In the form which is responsible for editing the settings, in the Load
event handler, read the Checked
value of CheckBox
from settings and in the Click
event handler of the save button, save the CheckBox.Checked
into the setting property:
private void SettingForm_Load(object sender, EventArgs e)
{
topMostCheckBox.Checked = Properties.Settings.Default.TopMost;
}
private void SaveButton_Click(object sender, EventArgs e)
{
Properties.Settings.Default.TopMost = topMostCheckBox.Checked;
Properties.Settings.Default.Save();
}
In the other forms, or in a base form, setup the property bindings to the setting using designer or using code:
private void BaseForm_Load(object sender, EventArgs e)
{
this.DataBindings.Add(new Binding("TopMost", Properties.Settings.Default,
"TopMost", true, DataSourceUpdateMode.OnPropertyChanged));
}
Since it has been setup using data-binding, as soon as you save settings, TopMost
will be applied to all new and open forms which derive from this base form.
More information:
Upvotes: 2
Reputation: 348
If you are doing this in Settings Form it only be run for Setting Form. To go for all forms Declare a public static class
public static class isTopMost
{
public static bool TopMost;
}
Settings Forms
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
isTopMost.TopMost=checkBox1.Checked;
this.TopMost = isTopMost.TopMost; // add this line all forms form_load events
}
Upvotes: 0
Reputation: 72153
For new forms opened after the checkbox is selected, you must set TopMost
from a global variable on initialization.
If you want to set TopMost
on all currently open forms, you can loop through them, like so:
foreach (Form frm in Application.OpenForms)
frm.TopMost = true;
Upvotes: 0