2

Sorry, I am new to C# and am unsure what I am doing wrong.

Here is the code I am using:

private void chkSmallMenu_CheckedChanged(object sender, EventArgs e)
{
    frmSmallMenu sm = null;
    if (chkSmallMenu.Checked) 
    { 
        if (sm is null || sm.IsDisposed)
        { 
            sm = new frmSmallMenu(); 
        } 
        sm.Show(); 
    } 
    else 
    {
        MessageBox.Show("close");
        sm?.Close(); 
    }
}

The window will open but when I uncheck the box nothing happens and I have no idea why. I have tried looking for an answer but nothing has worked for me.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • The code calls Close() on a form object that was just created and never shown. SM needs to be a member of the class instead of a local variable so its value can stay valid. Subscribe the FormClosed event to set it back to null. – Hans Passant Feb 05 '22 at 12:03
  • Sorry I don't really understand, could you provide code of what I need to do? – Jordan Haskell Feb 05 '22 at 12:16
  • 1
    You have already asked this. Use the `CheckedChanged` event, not `MouseClick`. Then `frmSmallMenu sm = null; /* in the event handler */ if (chkSmallMenu.Checked) { if (sm is null || sm.IsDisposed) { sm = new frmSmallMenu(); } sm.Show(); } else { sm?.Close(); }` – Jimi Feb 05 '22 at 12:34
  • 1
    @Jimi I have just updated my code in visual studio and on this question and the window still will not close. I have used a message box to see if it is running the else statement correctly and it is, the message box shows but the window does not close, hide or anything. – Jordan Haskell Feb 05 '22 at 12:50
  • `frmSmallMenu sm = null;` is in the wrong place. You need to leave it as a Field and replace `frmSmallMenu sm = new frmSmallMenu();` that you had before. -- Did you see the `/* in the event handler */` comment? – Jimi Feb 05 '22 at 12:56
  • Does this answer your question? [VS C# How can I Show/Hide a top-level window from a parent form](https://stackoverflow.com/questions/70993820/vs-c-sharp-how-can-i-show-hide-a-top-level-window-from-a-parent-form) – Bent Tranberg Feb 05 '22 at 13:20
  • @Jimi Sorry but as mentioned before I am not good with c#. When you say event handler are you talking about 'private void chkSmallMenu_CheckedChanged(object sender, EventArgs e)' ? And what do you mean by I should leave 'frmSmallMenu sm = null;' as a field? – Jordan Haskell Feb 05 '22 at 14:34
  • Yes, the `privat void chkSmallMenu_CheckedChanged()` part is an event handler, and a mostly private `field` is part of or kind of the opposite of a public `property`, see also https://stackoverflow.com/questions/295104/what-is-the-difference-between-a-field-and-a-property – Stefan Wuebbe Feb 05 '22 at 16:47

3 Answers3

1

Try this:

frmSmallMenu sm = new frmSmallMenu();

    private void chkSmallMenu_CheckedChanged(object sender, EventArgs e)
    {
        if (chkSmallMenu.Checked == true)
        {
            sm.Show();
        }
        else
        {
            MessageBox.Show("close");
            sm.Hide();
        }
    }
  • You need to add in those checks for if sm has been `disposed`, though, for when the user clicks on that red X in the top right... – Idle_Mind Feb 05 '22 at 18:50
1

This modification of your code would probably do what you want by first looking whether the other Form is already running or not:

namespace WinFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// The following `uselessField ` is a `field`. See also https://stackoverflow.com/questions/295104/what-is-the-difference-between-a-field-and-a-property
        /// `(Since it is `unused`, you would get a CS0169 Warning in the "Error List" window)
        private int uselessField;

        /// <summary>
        /// **Event handler** of the "chkSmallMenu" `CheckBox` control on your `Form`.
        /// (You would probably get an IDE1006 Info in your "Error List" window because
        /// the control name and/or the event handler name respectively, starts with a lower case
        /// https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/naming-rules)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void chkSmallMenu_CheckedChanged(object sender, EventArgs e)
        {
            // the following `sm` is a `variable`. See also https://stackoverflow.com/questions/295104/what-is-the-difference-between-a-field-and-a-property

            var sm = Application.OpenForms.OfType<frmSmallMenu>().FirstOrDefault();
            // the following `Checked` **property** belongs to the WinForms Checkbox class and `IsDisposed` belongs to the other `Form`
            if (chkSmallMenu.Checked)
            {
                if (sm?.IsDisposed != true)
                {
                    sm = new frmSmallMenu();
                }
                sm.Show();
            }
            else
            {
                MessageBox.Show("close");
                sm?.Close();
            }
        }
    }
}
Stefan Wuebbe
  • 2,109
  • 5
  • 17
  • 28
0

This fixed my issue:

frmSmallMenu sm = new frmSmallMenu();

private void chkSmallMenu_CheckedChanged(object sender, EventArgs e)
{
    if (chkSmallMenu.Checked == true)
    {
        sm.Show();
    }
    else
    {
        MessageBox.Show("close");
        sm.Hide();
    }
}
Elikill58
  • 4,050
  • 24
  • 23
  • 45