简体   繁体   English

如何关闭表格?

[英]How do i close the Form?

I have this code which Show/open a new Form : 我有显示/打开新表格的代码:

In the gkh_Keydown event when i click on Ctrl + M it's showing/opening the new Form . 在gkh_Keydown事件中,当我单击Ctrl + M时,它将显示/打开新的Form。 Now i want to do that when i click again Ctrl + M it will close the new Form . 现在,当我再次单击Ctrl + M时,它将关闭新表格。

When i click once to open the new Form it's going to this Form first : 当我单击一次以打开新表格时,它将首先转到该表格:

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();
            }

        }

And since i did false it's doing the else part : 而且由于我做错了,所以在做其他部分:

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

magnifier.Show(); magnifier.Show(); do show the new Form . 确实显示新表格。

Now i want that if i do Ctrl + M again it will close the Form magnifier.Show(); 现在,我希望如果再次执行Ctrl + M,它将关闭Form magnifier.Show();。

So in the Form in the gkh_KeyDown event in the else part i did : 因此,在其他部分的gkh_KeyDown事件的Form中,我做到了:

magnifierform.Close();

Added a new variable this time only for magnifierform and try to close it. 这次仅针对放大镜形式添加了一个新变量,然后尝试将其关闭。 So in magnifierform i did : 所以在放大镜形式我做到了:

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()
        {

        }

Added another instance that does nothing sincei only want to close it. 添加了另一个不执行任何操作的实例,因为只希望将其关闭。 But it never closed . 但是它从未关闭过。

magnifierform is a variable of the MagnifierForm form i want to close it directly and not as before i used the other form MagnifierMainForm. magnifierform是MagnifierForm形式的变量,我想直接将其关闭,而不是像我以前使用其他形式的MagnifierMainForm一样。

I just want to close it but it never closed. 我只想关闭它,但从未关闭过。 I used a breakpoint on the line : 我在行上使用了一个断点:

magnifierform.Close();

On second Ctrl + M it's getting there but it dosen't close the MagnifierForm . 在第二次Ctrl + M时到达那里,但没有关闭MagnifierForm。 Just does nothing. 什么也没做。

EDIT 编辑

Tried something else now in the MagnifierForm i added : 现在我在MagnifierForm中尝试了其他东西:

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

In Form1 in the gkh_KeyDown event in the else side i changed it to : 在Form1中else端的gkh_KeyDown事件中,我将其更改为:

else
                {
                    magnifierform = new MagnifierForm();
                }

So the instance i'm doing it on the second Ctrl + M And again the breakpoint stop there but when i do continue it's not closing the Form . 因此,实例在第二个Ctrl + M上执行,断点再次在此处停止,但是当我继续执行时,它没有关闭Form。

So, if MagnifierMainForm() is already open you want to close it?...otherwise create a new instance and show it? 那么,如果MagnifierMainForm()已经打开,您想关闭它吗?...否则创建一个新实例并显示它? In your code that fires when Ctrl+M is pressed, do something like: 在按下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
        }

使用此方法form.Dispose();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM