0

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.

2
  • Visible is already true, setting it again to true won't fire the event. Do note that its property getter is wonky, it will tell you whether you can actually see it. So the debugger will tell you that it is false. But the internal state is true. Commented Jun 6 at 17:30
  • @HansPassant Im setting the Panel To Visible= true; in the ProcessBtn_Click() so it becomes Visible in the form so I can interact with it I did notice that the Property of the Panel Visible is True whenever I start the application the whole time even before Control.Add-ing it to the Form. Commented Jun 6 at 21:27

1 Answer 1

0

Many somethingChanged events are not triggered on component creation. The form is born with Visible set to true, and the event triggers when Visible property changes. It could be a philosophical view, but in WinForms the creation of an object is not a "change" of its property values.

You should then trigger the function in your components constructor.

Something like:

constructor() {
  RefreshFormOnVisibilityChange();
}

private void ProcessesPanel_VisibleChanged(object sender, EventArgs e)
{
  RefreshFormOnVisibilityChange();
}

private void RefreshFormOnVisibilityChange()
{
  if (Visible==true)
  {
    form1Reference.DisableKeyboardHook();
  }
  else if (Visible == false) 
  {
    form1Reference.ResetOriginalFormSize();
    form1Reference.EnableKeyboardHook();
  }
}
4
  • This didnt work since when I click the button to Make the Panel visible after its been added to the Controls.Add, VisibilityChanged doesnt fire when its set to true the first time as if processesControlPanel.Visible = true; didnt happen and only made it visible to Me the User. Is it possible that the Controls.Add in the same method makes Visible=true not work as intended. And If I Controls.Add() in the initializer in Form1 it makes the Panel Visible at the start of the Porgram but is Behind the other buttons and labels that I have and need to set it right after that to False but that messes up Commented Jun 6 at 16:23
  • @Yordan Voyvode 2cond part of my comment: but that Messes up the If statement(Visible=false) and resetsOriginalFormSize and enables the windows hook when it shoulds. Commented Jun 6 at 16:27
  • If you have another form (which you omitted in your question), what prevents you from calling RefreshFormOnVisibilityChange() in the parent form, after adding the child? AFAICT, it seems just a problem in identifying the right moment to manually call the function. Commented Jun 7 at 6:48
  • I have only 1 Form and the others are classes I ran It on both but I Method you gave me just does the same thing as before it just Adds another check at the beginning of the start of the program but the code runs the same,the Panel starts again with Visibility=true(Which is a quirk as others said)its just that the code is in a method now. I now just made the Logic a bit more static I used DisableKdHook() in the ProcessBtn_Click and Put EnableKdHook() in the Cancel and Ok button Logic.Im gonna write an answer based on everything I tried. Since I couldnt find a definitive answer. Commented Jun 7 at 19:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.