如何关闭表格?
本文关键字:表格 何关闭 | 更新日期: 2023-09-27 18:16:35
我有这个代码显示/打开一个新的窗体:
在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();
}
}
因为我设置了false,所以它在执行else部分:
GetConfiguration();
int x = mLastCursorPosition.X;
int y = mLastCursorPosition.Y;
MagnifierForm magnifier = new MagnifierForm(mConfiguration, System.Windows.Forms.Cursor.Position);//mLastCursorPosition);
magnifier.Show();
magnifier.Show ();show new Form .
现在我想,如果我再次按Ctrl + M,它将关闭窗体放大镜。
所以在Form的gkh_KeyDown事件中在else部分我做了:
magnifierform.Close();
为magnifierform添加了一个新变量,并尝试关闭它。在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();
}
在Form1中,在gkh_KeyDown事件中,我将其更改为:
else
{
magnifierform = new MagnifierForm();
}
所以我在第二个Ctrl + M上做断点在这里停止了但是当我继续时它并没有关闭表单
所以,如果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
}
使用此方法form.Dispose();