3 Replies - 377 Views - Last Post: 03 December 2016 - 01:57 PM Rate Topic: -----

#1 DarthRipper  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 02-December 16

Custom Minimize Functionality not Working in Second Form

Posted 02 December 2016 - 05:48 PM

Hey Everyone

I am working in .NET 4.5 Windows Forms Application, and I have two forms, FormA and FormB. I have set the FormBorderStyle Property to None for both forms. I have created two buttons on each form an exit and a minimize button. I am also hiding FormA in a button click event that initializes FormB call FormABtn. The code in FormA is:

 namespace MainSystem
{
    #region Class
    public partial class FormA : Form
    {
        #region Form Initilization Method
        public FormA()
        {
            InitializeComponent();
        }
        #endregion

        #region Minimize Button Click Method
        private void BtnMinimize_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }
        #endregion

        #region Exit Button Click Method
        private void BtnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region FormABtn Button Click Method
        private void FormABtn_Click(object sender, EventArgs e)
        {
            FormB formB = new MainSystem.FormB ();
            formB.FormClosed += new FormClosedEventHandler(FormB_FormClosed);
            this.Hide();
            formB.Show();
        }
        #endregion

        #region FormB Form Close Event
        void FormB_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Show();
        }
        #endregion
    }
    #endregion
} 


The code in FormB is:


 namespace MainSystem
{
    #region Class
    public partial class FormB : Form
    {
        #region FormB Form Initialization
        public FormB()
        {
            InitializeComponent();
        }
        #endregion

        #region Minimize Button Click Method
        private void BtnMinimize_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }
        #endregion

        #region Exit Button Click Method
        private void BtnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion
    }
    #endregion
} 


The problem is in FormB when I click my minimize button the form seems to do nothing at all. The Form remains open on the screen. If I use the minimize button in FormA, it works just fine. I have tried removing the “this” keyword. I have tried passing a reference of FormA. I have tried making FormB a Modal Form and then a Modeless Form. I do not want to make it an MDI Child Form. Then I ran out of options to try, so I started trying things that I know are not the cause of the problem like changing the method and variable names so that they are not the same as FormA, and setting FormB as the owner. I just cannot seem to get the Minimize button in FormB to minimize. Can someone please help?

Is This A Good Question/Topic? 0
  • +

Replies To: Custom Minimize Functionality not Working in Second Form

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5898
  • View blog
  • Posts: 20,136
  • Joined: 05-May 12

Re: Custom Minimize Functionality not Working in Second Form

Posted 02 December 2016 - 07:40 PM

Are you sure you hooked up the minimize button event handler for FormB? I'm asking because the following works fine for me:

FormA.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TwoForm
{
    public partial class FormA : Form
    {
        public FormA()
        {
            InitializeComponent();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnFormB_Click(object sender, EventArgs e)
        {
            Hide();
            var formB = new FormB();
            formB.FormClosed += FormB_FormClosed;
            formB.Show();
        }

        private void FormB_FormClosed(object sender, FormClosedEventArgs e)
        {
            Show();
        }
    }
}



FormB.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TwoForm
{
    public partial class FormB : Form
    {
        public FormB()
        {
            InitializeComponent();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnMinimize_Click(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Minimized;
        }
    }
}


Was This Post Helpful? 1
  • +
  • -

#3 DarthRipper  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 02-December 16

Re: Custom Minimize Functionality not Working in Second Form

Posted 03 December 2016 - 01:47 PM

Skydiver,

Thanks for the response. I will have a look because you may be right. I started working on this late at night and was falling asleep off and on. I will check and confirm it is wired up right.
Was This Post Helpful? 0
  • +
  • -

#4 DarthRipper  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 02-December 16

Re: Custom Minimize Functionality not Working in Second Form

Posted 03 December 2016 - 01:57 PM

Sky,

You were right, I had wired up the Exit button's even in FormB but forgot to wire up the Minimize button. That is why I should not code when I am sleepy, LOL. Anyway thanks a lot, I voted your post up.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1