I'm changing the visibility of some labels on the form , when a contol is moved. When all labels are invisible , i want to close application. Is there a way to do this ?
-
This coud be the answer that you are looking for: http://stackoverflow.com/questions/3419159/how-to-get-all-child-controls-of-a-windows-forms-form-of-a-specific-type-button – kiks73 Dec 13 '14 at 08:17
2 Answers
Create a function like this:
public void CheckLabels()
{
bool AllHidden = true;
foreach (Control c in this.Controls)
{
if (c.GetType().Name == "Label")
{
if (c.Visible == true)
{
AllHidden = false;
Break;
}
}
}
if (AllHidden)
{
//Do whatever you want. For example:
this.Close();
}
}
Call this function from a button click event of Visibility_Changed
event of all labels.
EDIT:
Another way is create your own label by inheriting from System.Windows.Forms.Label
.
public partial class MyLabel : System.Windows.Forms.Label { public MyLabel() { InitializeComponent(); this.VisibleChanged += new EventHandler(MyLabel_VisibleChanged); }
void MyLabel_VisibleChanged(object sender, EventArgs e)
{
CheckLabels();
}
public void CheckLabels()
{
bool AllHidden = true;
foreach (System.Windows.Forms.Control c in this.FindForm().Controls)
{
if (c.GetType().Name == "MyLabel")
{
if (c.Visible == true)
{
AllHidden = false;
break;
}
}
}
if (AllHidden)
{
//Do whatever you want. For example:
this.FindForm().Close();
}
}
And use MyLabel
in your form. Thats it!!

- 18,593
- 13
- 50
- 55
-
1
-
@Steve: Yup, that will do. I am answering considering the OP is a beginner. – Raging Bull Dec 13 '14 at 08:28
-
Want to write it once. Not to write 25 times. Is there any event handler that raises for every change on the main form ? – Magic Fingers Dec 13 '14 at 08:28
-
@MagicFingers: If you want to write it once, you can use inheritance. I will add it to my answer. – Raging Bull Dec 13 '14 at 08:29
You can keep track of how many visible Label
controls there are, and when the count reaches 0, you can close your form (or perform whatever other action is required to close the program in your case).
For example:
partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
int visibleLabelCount = 0;
foreach (Label label in Controls.OfType<Label>())
{
// Check property, in case some Label is initialized as not
// visible in the Designer
if (label.Visible)
{
visibleLabelCount++;
}
label.VisibleChanged += (sender, e) =>
{
visibleLabelCount += ((Label)sender).Visible ? 1 : -1;
if (visibleLabelCount <= 0)
{
Close();
}
}
}
}
}
The above attaches an event handler to each Label
control found in your Form
class, and at the same time counts the currently-visible Label
controls. The event handler adjusts the count of visible Label
controls as the visibility of each changes; if a Label
becomes visible, the count is increased, if it becomes not visible, it's decreased. If the count reaches zero, the Form
's Close()
method is called to close the form and the program.

- 68,759
- 7
- 102
- 136
-
As i said , dont want to write on each label. I want to check every possible change on form. – Magic Fingers Dec 13 '14 at 09:02
-
What do you mean? "Every possible change" is pretty broad. And what does "write on each label" mean? The above code doesn't "write on each label" and it is the most efficient way to accomplish the goal you stated. Why do you want to do it a less efficient way? – Peter Duniho Dec 13 '14 at 09:23