I have overridden the OnVisibleChanged method:

 protected override void OnVisibleChanged(EventArgs e) { base.OnVisibleChanged(e); this.Visible = false; } 

that is, when the program is loaded, the form is immediately hidden.

How can I then call the same method, but only vice versa, so that the form shows?

I tried this:

 this.Show(); this.Visible = true; 

All is in vain, well, it is understandable, because OnVisibleChanged is OnVisibleChanged , and its OnVisibleChanged is false .

Hide / show form does not suit me to hide / show.

    2 answers 2

    Changing the visibility in the form-responsive function is not correct. You need to transfer your code from OnVisibleChanged to something else, for example, to OnLoad . And the @altexoander code is used in any function (except OnVisibleChanged ).

     private void Form_Load(object sender, EventArgs e) { ... // Что то еще. this.Visible = false; } public void ChangeVisible() { this.Visible = this.Visible ? false : true; } 
    • Or the code is not working, or I'm stupid. I did everything as you wrote, at startup - the form is NOT hidden, and then (after a second) an error flies (not to your lines, but to "an object link does not indicate an object instance.") - Max
    • @ Maxim WPF or Winforms? - Mirdin
    • sorry for so long, Winforms. - Max
    • @ Maxim, then look. In the form that we will hide we are looking for, in the properties, the Load event and double click on it. Go to the form code, to a specific event handler and 'this.Visible = false; to it 'this.Visible = false; this 'this.Visible = false; . We delete all the art from OnVisibleChanged , we don’t touch the handler itself (we get an empty function). In the form class, we add the PUBLIC function ChangeVisible . Now we can from another form, run this form in a hidden form, and manage its visibility (do not forget to only subscribe to Form.Deactivate) - Mirdin
    • In Form_Load, this.Visible = false; does not hide the form. - Maxim

    Check the visibility of the form and just change the code. For example, you have now

    this.Visible = false

    Who bothers you to check and deliver this?

     protected override void OnVisibleChanged(EventArgs e) { base.OnVisibleChanged(e); this.Visible = this.Visible ? false : true; } 

    Those. for each VisibleChanged call, you will simply ignore external calls and alternately change the visibility from true to false (for example, 2 calls to the .Show form will appear and hide again).

    Beauty is not enough, but this solution is "vlob."

    • Still does not show the form. - Max
    • @Mirdin, uh, not quite understood. How to do this to display the form do this.Visible = false? Well, it doesn't work anyway. - Maxim
    • @Maxim, unfortunately, even this will not work, since the function will loop, changing the form visibility, we call OnVisibleChanged . I therefore deleted my comment. - Mirdin
    • @ Maxim, and how are you trying to change the visibility of the form? I'm just wondering why you are not changing. - alexoander
    • @alexoander, now you have corrected a little code? When using, if I'm not mistaken, the ternary operator in OnVisibleChanged, an error pops up when the application starts: An unhandled exception of the type "System.StackOverflowException" in System.Windows.Forms.dll - Maxim