How do i close the Form ? Tried everything .

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Im using in Form global key hooking to hook combination of keys : Ctrl + M
So when i click once Ctrl + M it will show/open the new Form and then when i make Ctrl + M again it will close the new Form .


I went this site : http://www.codeproject.com/Articles/19004/A-Simple-C-Global-Low-Level-Keyboard-Hook
Downloaded the source files added to my project the file i need and everything and the Ctrl + M is working.
This is the code in Form1 in the event of ctrl + key when opening/showing the Form :void gkh_KeyDown(object sender, KeyEventArgs e)
{
if ((e.KeyCode == System.Windows.Forms.Keys.LControlKey) || (e.KeyCode == System.Windows.Forms.Keys.RControlKey))
{
controlDown = true;
}

if (e.KeyCode == System.Windows.Forms.Keys.M && controlDown)
{
// Do CTRL-M action
if (mf == null)
{
mf = new MagnifierMainForm(false);
mf.StartPosition = FormStartPosition.Manual;
mf.Location = Control.MousePosition;
this.Select();
}
else
{
magnifierform = new MagnifierForm();
magnifierform.Close();
}
}
}

Now MagnifierMainForm is a Form im using to show the second Form i want .
This is the constructor code of the MagnifierMainForm :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();
}

}

Since i did false in the instance of the MagnifierMainForm in the ctrl + m code it will do the ELSE part only and will show the other Form i wanted the MagnifierForm !!!
There is : MagnifierMainForm and MagnifierForm.
I want to show and close the MagnifierForm !
So first time i use the MagnifierMainForm doing false and sgow the MagnifierForm.
Now i want to make that another CTRL + M will close the MagnifierForm !
So in Form1 in the event of the ctrl + m in the else part i did :magnifierform = new MagnifierForm();
magnifierform.Close();

I used a breakpoint and i see that in the second ctlr + m its going to the MagnifierForm instance and to the magnifierform.Close();
Then in the MagnifierForm constructor 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()
{
}

I added the second instance :public MagnifierForm()
{
}

That does nothing since the first instance is called from the MagnifierMainForm.
But for closing the MagnifierForm i tried to so it directly .
But nothing i used a breakpoint and i see its doing the MagnifierForm instance i see that its getting to the magnifierform.Close(); but it does nothing its not closing it.
I also tried in the MagnifierForm instance to add : this.Close(); like this :public MagnifierForm()
{
this.Close();
}

But nothing i see its doing the line this.Close(); but in fact its not closing it.
Tried in Form in the code of the ctrl + m to do it like this :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
}

But also didnt work. I cant understand why i cant close the form ? The MagnifierForm ?

View the full article
 
Back
Top