I have a UserPanel class that has Controls in it buttons,Labels ect. When the Panel is created at runtime it passes to the Constructor of Form1 (this) so I can use some methods and I have a ProcessBtn_Click that sets the location of the Panel and adds it to the Controls and sets it to Panel.Visible = true; and I have a ProcessesPanel_VisibleChanged event that should trigger when I click the ProcessBtn_Click that sets Panel.Visible to true but it doesnt! When I click the Button the Panel Shows and everything but the ProcessesPanel_VisibleChanged event doesnt trigger (the first time) in my UserPanel Class only when I press the Cancel button on the Panel and click the ProcessBtn_Click again it triggers the VisibleChanged event normally but not the First time. Why is that? How can I fix it? I tried looking some articles MSDN and blog post by Joe White(for creating my own getter setter bool Visibility variable but it worked the same)
//In UserPanel Visibility Event
private void ProcessesPanel_VisibleChanged(object sender, EventArgs e)
{
//THIS DOESNT WORK Untill the Second time I set Visible=true; in the ProcessBtn_Click
if (Visible==true) //<--It doesnt work the First time
{
form1Reference.DisableKeyboardHook();
}
//Gets fired the first time when I click Cancel button that sets Visible=false; in the UserPanel class
else if (Visible == false)
{
form1Reference.ResetOriginalFormSize(); //If Panel Dissapears bring back the originalFormSize before expanding
form1Reference.EnableKeyboardHook();
}
}
//In Form1 ProcessBtn_Click button method that Controls.Adds the Panel and makes it Visible=true
private void ProccessBtn_Click(object sender, EventArgs e)
{
SaveOriginalFormSize(); //If you do it in ProcessesPanel VisibleChanged it would save the Already expanded new Form Size rather thann the previous one(so do it here before the Panel changes the Form1 size)
if (!Controls.Contains(processesControlPanel)) //The Controlls Collection is smart enough to not add the Panel again after the first time( but with the Check it avoids unnecessary operations)
{
//processesControlPanel.Visible = false;
processesControlPanel.Location = new Point(0, 41);
Controls.Add(processesControlPanel);
}
processesControlPanel.Visible = true; //Makes the Panel visible again after its been set to false when pressing Cancel button or Clicking on the main Form1
processesControlPanel.BringToFront();
}
Tried as well to move the Event in the main Form1 in the initializer += Panel_VisibilityChanged still does the same when I click ProcessBtn it doesnt fire the Visible==true (first time)If statement but if I do for Visible=false it does.