[英]How do i close the Form?
我有顯示/打開新表格的代碼:
在gkh_Keydown事件中,當我單擊Ctrl + M時,它將顯示/打開新的Form。 現在,當我再次單擊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();。
因此,在其他部分的gkh_KeyDown事件的Form中,我做到了:
magnifierform.Close();
這次僅針對放大鏡形式添加了一個新變量,然后嘗試將其關閉。 所以在放大鏡形式我做到了:
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中else端的gkh_KeyDown事件中,我將其更改為:
else
{
magnifierform = new MagnifierForm();
}
因此,實例在第二個Ctrl + M上執行,斷點再次在此處停止,但是當我繼續執行時,它沒有關閉Form。
那么,如果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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.