我如何关闭表格?

时间:2013-06-03 22:48:13

标签: c# winforms

我有这个显示/打开新表格的代码:

在gkh_Keydown事件中,当我点击Ctrl + M时,它显示/打开新表格。 现在我想这样做,当我再次点击Ctrl + M它将关闭新表格。

当我点击一次打开新表格时,它首先进入此表格:

public MagnifierMainForm(bool showMain)
        {
            InitializeComponent();

            if (showMain == true)
            {

                GetConfiguration();

                //--- My Init ---
                FormBorderStyle = FormBorderStyle.None;
                TopMost = true;
                StartPosition = FormStartPosition.CenterScreen;

                mImageMagnifierMainControlPanel = Properties.Resources.magControlPanel20061222;

                if (mImageMagnifierMainControlPanel == null)
                    throw new Exception("Resource cannot be found!");

                Width = mImageMagnifierMainControlPanel.Width;
                Height = mImageMagnifierMainControlPanel.Height;

                HotSpot hsConfiguration = new HotSpot(new Rectangle(50, 15, 35, 30));
                hsConfiguration.OnMouseDown += new HotSpot.MouseEventDelegate(hsConfiguration_OnMouseDown);
                hsConfiguration.OnMouseUp += new HotSpot.MouseEventDelegate(hsConfiguration_OnMouseUp);
                hsConfiguration.OnMouseMove += new HotSpot.MouseEventDelegate(hsConfiguration_OnMouseMove);

                HotSpot hsMagnfier = new HotSpot(new Rectangle(10, 15, 30, 30));
                hsMagnfier.OnMouseMove += new HotSpot.MouseEventDelegate(hsMagnfier_OnMouseMove);
                hsMagnfier.OnMouseDown += new HotSpot.MouseEventDelegate(hsMagnfier_OnMouseDown);
                hsMagnfier.OnMouseUp += new HotSpot.MouseEventDelegate(hsMagnfier_OnMouseUp);

                HotSpot hsExit = new HotSpot(new Rectangle(95, 20, 15, 15));
                hsExit.OnMouseUp += new HotSpot.MouseEventDelegate(hsExit_OnMouseUp);

                mHotSpots.Add(hsConfiguration);
                mHotSpots.Add(hsMagnfier);
                mHotSpots.Add(hsExit);

                ShowInTaskbar = false;
                this.Show();
            }
            else
            {
                GetConfiguration();
                int x = mLastCursorPosition.X;
                int y = mLastCursorPosition.Y;
                MagnifierForm magnifier = new MagnifierForm(mConfiguration, System.Windows.Forms.Cursor.Position);//mLastCursorPosition);                
                magnifier.Show();
            }

        }

因为我做错了它正在做其他部分:

GetConfiguration();
                    int x = mLastCursorPosition.X;
                    int y = mLastCursorPosition.Y;
                    MagnifierForm magnifier = new MagnifierForm(mConfiguration, System.Windows.Forms.Cursor.Position);//mLastCursorPosition);                
                    magnifier.Show();

magnifier.Show();确实显示新表格。

现在我希望如果我再次按Ctrl + M它会关闭Form magnifier.Show();

所以在我做过的else部分的gkh_KeyDown事件中的Form中:

magnifierform.Close();

这次仅为放大镜形式添加了一个新变量,并尝试将其关闭。 所以在magnifierform中我做了:

public MagnifierForm(Configuration configuration, Point startPoint)
        {
            InitializeComponent();

            //--- My Init ---
            mConfiguration = configuration;
            FormBorderStyle = FormBorderStyle.None;

            ShowInTaskbar = mConfiguration.ShowInTaskbar;
            TopMost = mConfiguration.TopMostWindow;
            Width = mConfiguration.MagnifierWidth;
            Height = mConfiguration.MagnifierHeight;

            // Make the window (the form) circular
            GraphicsPath gp = new GraphicsPath();
            gp.AddEllipse(ClientRectangle);
            Region = new Region(gp);

            mImageMagnifier = Properties.Resources.magnifierGlass;

            mTimer = new Timer();
            mTimer.Enabled = true;
            mTimer.Interval = 20;
            mTimer.Tick += new EventHandler(HandleTimer);

            mScreenImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
                                     Screen.PrimaryScreen.Bounds.Height);
            mStartPoint = startPoint;
            mTargetPoint = startPoint;

            if (mConfiguration.ShowInTaskbar)
                ShowInTaskbar = true;
            else
                ShowInTaskbar = false;
        }

        public MagnifierForm()
        {

        }

添加了另一个不执行任何操作的实例,因为我只想关闭它。 但它从未关闭过。

magnifierform是MagnifierForm表单的一个变量,我想直接关闭它而不是像以前那样使用其他形式的MagnifierMainForm。

我只想关闭它,但它从未关闭过。我在线上使用了一个断点:

magnifierform.Close();

在第二个Ctrl + M上它到达那里但它不会关闭MagnifierForm。什么都不做。

修改

我在MagnifierForm中添加了其他内容:我添加了:

public MagnifierForm()
        {
            this.Close();
        }

在else方面的gkh_KeyDown事件中的Form1中,我将其更改为:

else
                {
                    magnifierform = new MagnifierForm();
                }

因此实例我在第二个Ctrl + M上执行此操作 断点再次停在那里但是当我继续它没有关闭表格时。

2 个答案:

答案 0 :(得分:1)

那么,如果MagnifierMainForm()已经打开你要关闭它吗?...否则创建一个新实例并显示它?在按下Ctrl + M时触发的代码中,执行以下操作:

        Form frmToClose = null;
        foreach (Form frm in Application.OpenForms)
        {
            if (frm is MagnifierMainForm)
            {
                frmToClose = frm;
                break;
            }
        }
        if (frmToClose != null)
        {
            frmToClose.Close();
        }
        else
        {
            // create a new instance of MagnifierMainForm() and display it
        }

答案 1 :(得分:0)

使用此方法form.Dispose();