Hiding form when are other controls focus - c#

This question is related to this my question. Now I have form in my class and when user click on button I show (or hide) form. That´s ok. But I want to hide form when I move with origin form or when I click somewhere in origin form. The new form is behind that origin form. I was trying events like lostfocus and others but It didn´t help. So I think I need some trick that check from my control if there was click in parrent form (origin form) or some other hack. I know the best would be that I put code but I have many lines so I think that best way will be if you help me in general way and then I try to applicate to my app.

You can do it with a global mouse and keyboard hook. In fact, its been wrapped up into well documented, well structured .NET API over at CodePlex
Go over there and download it. Then, set up a global mouse hook:
_mouseListener = new MouseHookListener(new GlobalHooker());
_mouseListener.MouseMove += HandleGlobalHookMouseMove;
_mouseListener.Start();
The key here is that you will receive the MouseMove event ANY time the mouse moves ANYWHERE on the desktop, not just within the bounds of your window.
private void HandleAppHookMouseMove(object sender, MouseEventArgs e)
{
if (this.Bounds.Contains(e.Location))
{
HandleEnter();
}
else
{
HandleLeave();
}
}
You can also setup one for MouseClick. The combination of the two will enable you to determine any time the mouse moves over your origin form, or the mouse is clicked when its over it. Unlike the LostFocus and other events you tried, focus is irrelevant.

Does below help?
public partial class Form1 : Form
{
Form f2 = new Form2();
public Form1()
{
InitializeComponent();
f2.Show();
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (this.ClientRectangle.Contains(e.Location) && f2.Visible) { f2.Hide(); }
}
private void button1_Click(object sender, EventArgs e)
{
f2.Visible = !f2.Visible ? true : false;
}
}

Related

How to detect if a window is closed C#, Windows Form App?

So, I'm making a payroll management system as a hobby project to help my resume and general knowledge of c#. So, I'm making a UI and I can open a new window just fine with this code:
private void button1_Click(object sender, EventArgs e)
{
CreateAdminAcct createAcct = new CreateAdminAcct();
createAcct.StartPosition = FormStartPosition.CenterScreen;
createAcct.Show();
this.Hide();
}
however, I don't know the event to check when the little red "x" button is clicked, because when that button is clicked, I want to go back to the main screen because I hide the main screen when that button is clicked, and when i click the red "x" on the screen that just opened, it closes, but the application continues to run in the background.
If there is some better way to manage multiple menus, I'm open to suggestions, however, this is what I've found easiest.
Thanks in advance
I second Robert Harvey's suggestion; this gives the user the reassurance tha tht emain window is still open/ nothing got lost, but it's unreachably "behind" the CreateAdminAcct form while the CreateAdminAcct form is open
private void button1_Click(object sender, EventArgs e)
{
CreateAdminAcct createAcct = new CreateAdminAcct();
createAcct.StartPosition = FormStartPosition.CenterScreen;
createAcct.ShowDialog();
//do any code here that needs to access createAcct before it's lost
MessageBox.Show(createAcct.NewAdmin.Name);
}
If you really do want to hide your main form, pass the main form itself to createAcct, and make it createAcct's job to re-open the main form when it is closing
private void button1_Click(object sender, EventArgs e)
{
CreateAdminAcct createAcct = new CreateAdminAcct(this); //note passing this form to constructor
createAcct.StartPosition = FormStartPosition.CenterScreen;
createAcct.Show();
}
class CreateAcctForm : Form{
private Form _showWhenClosing;
CreateAcctForm(Form revertTo){
InitializeComponent();
_showWhenClosing = revertTo;
}
}
void Form_Closing(object sender, ...){ //event
_showWhenClosing.Show();
}
Side note: please rename your controls after you drop them ona form. code that's stuffed with label57, textbox25 is effectively obfuscated and really wearisome to follow

Windows Forms click event not fired when clicking on label?

I have Windows Form TestForm, and in my Form I have several labels that are only used to display some text.
I need to display a MessageBox.Show anytime the Form is clicked. So I have an event handler for the click, which looks like this:
private void TestForm_Click(object sender, EventArgs e)
{
MessageBox.Show("The form has been clicked");
}
Unfortunately, the click event doesn't fire when I click over a label in the Form. Is there a way to fix this, besides consuming the click event for the labels?
Thanks.
To use the same click event for all labels:
In the properties for each label, go to the Events (lightning bolt tab).
You will see (probably near the top) a label for Click, click the dropdown for this event, and you will be shown a list of handlers that you could use for that label.
Here's the Properties > Events > Click handler (bottom right):
Because all of your labels are of the same type, and produce the same EventArgs, you are able to use the same handler for all of them.
Then, when you are adding more Labels, just choose the event handler from the Click event dropdown:
Hope this helps!
To flesh out LarsTech's comment, I have used something like this in the past when I was having problems with labels overlapping each other and lack of true transparency in WinForms. What I did was make the labels invisible on the Form, then iterate through them in the Form's paint event, pull the information out of them and then use Graphics.DrawString to draw the text. That way you you will still be able see them in design mode.
This is a quick example of what I mean.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
foreach (var temp in this.Controls)
{
if (temp is Label) //Verify that control is a label
{
Label lbl =(Label)temp;
e.Graphics.DrawString(lbl.Text, lbl.Font, new SolidBrush(lbl.ForeColor), new Rectangle(lbl.Location, lbl.Size));
}
}
}
private void Form1_Click(object sender, EventArgs e)
{
MessageBox.Show("The Form has been clicked");
}
}

Make the second form enable again after disabled

I just face a new problem
I have one form which there is a button in it and when user click on this button another form will be appear and open.
I set an events that when user double click on the second form the form will be disabled and user can't do anything when form become disabled.
But i want to set a way that user can make the second form enable again.
I tried some events that when user press enter on the second form the second form become enable again and this is my code :
f3.Enabled = true;
But in fact when i press enter after disable the form nothing happen at all.
I tried some another way something like when user pressDown a key the second form become enable and this is my code :
f3.Enabled = true;
But something is make me angry that when the second form is open i cant do anything with the first form and i have to close the second form first.
But cause of disabled i can't close the second form.
what is your advice ?
what events can i add or what code should i put in my program to make this way to enable and disable the second form so easily ?
thanks in advance for your advice .
Update
Would you please tell me how can i put some controls in one grid and disable them ?
For example i don't want that user make any changes except close the form by clicking on 'x' button.
Update
This is my Form number 3
As you see i have 2 buttons and 3 labels which i didn't put code in labels.
but i wanna when user click on start all controls in form become disabled (especially click on form : it means that user can't click on the form like when its disabled) except Exit button. And above the form 'x' Button and minimized and maximized button become enable.
Update
I did this code in Form 2:
public partial class Form2 : Form{
public Form2()
{
InitializeComponent();
}
private void Form2_DoubleClick(object sender, EventArgs e)
{
this.Enabled = false;
}
private void Form2_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
this.Enabled = true;
}
}
}
And already a code that you wrote there.
But i have that problem yet.
I put a picture that maybe can help ya.
maybe can help ya this image
Update
I tried KeyPreview in my form and changed it into true but i have that problem yet.
Any advice ?
It means that there is no code for my idea ?!?
.Update
I tried so many code for this question and couldn't find my answer yet...
let me describe better that what is my problem
I have 2 forms and i wanna when user click on the second form the form become non clickable and user can't do anything when user click on that button except minimize and maximize and exit from the form.
I found some codes but they didn't help me...
Maybe you can :
this is my code in the button :
private void btnStart_Click(object sender, EventArgs e)
{
f3.Visible = false;
}
and i also tried this code :
private void btnStart_Click(object sender, EventArgs e)
{
this.Enabled = false;
}
And also set keypreview for this form but when i click on the button the form become Disable and i cant do anything except close the program from taskbar.
any advice ?
You can do it something like this for forms:
Form1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.ShowDialog();
}
}
Form2:
// on form, set form property KeyPreview to true
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_DoubleClick(object sender, EventArgs e)
{
this.Enabled = false;
}
private void Form2_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
this.Enabled = true;
}
}
}
If you want to do with grid view or something, you can enable/disable grid in the same way.
Please let me know if you have further questions.
I hope it will help you.. !

How to change protection level of menustrip so I can code in other form where menustrip isn't it

EDIT: If I change in Home Form private to public void then I must do a kinda concvert to bool from void... but I don't know how that works. Can you help me guys?
I am stuck here in the code.... I wanted to know how to access to my other form which has menustrip from another form.
E.G:
I want that clicking on the menustrip from other form where menustrip doesn't exists.
Here is the code:
Form 1
Home frm = new Home();
frm.IsMdiContainer = true;
if(frm.Controls["todasEntradasToolStripMenuItem"].Click += frm.todasEntradasToolStripMenuItem_Click)
{
{something}
}
The form Home is "frm" variable and it is where it has the menu strip. I want help to change the protection level so that this form (Form1) can accept this code... Anyone can help me please?
Solution 1 (nice):
Add your Click event in some Init-method or the constructor in Home. There you can access your control.
todasEntradasToolStripMenuItem.Click += todasEntradasToolStripMenuItem_Click;
Also in Home you define a new event:
public event EventHandler<EventArgs> TodasEntradasToolStripMenuItemClick;
private void OnTodasEntradasToolStripMenuItemClick(EventArgs e)
{
if (todasEntradasToolStripMenuItem != null)
{
TodasEntradasToolStripMenuItemClick(this, e);
}
}
In the Click handler you raise your own public event:
private void todasEntradasToolStripMenuItem_Click(object sender, System.EventArgs e)
{
OnTodasEntradasToolStripMenuItemClick(e);
}
In Form1 you add your Handler to this public event:
Home frm = new Home();
frm.TodasEntradasToolStripMenuItemClick += frm_TodasEntradasToolStripMenuItemClick;
In this handler you can "do something":
private void frm_TodasEntradasToolStripMenuItemClick(object sender, EventArgs e)
{
// Do something
}
Solution 2 (do not do it):
You asked for changing the protection level. So you can change
private todasEntradasToolStripMenuItem
in Home to
internal todasEntradasToolStripMenuItem
or even
public todasEntradasToolStripMenuItem
But I do not suggest you not to do this. You should choose Solution 1. With Solution 2 you would open Home for more changes than you have to.

How can I make a window act like a Context Menu?

I am trying to build a total new window acts as Context Menu.
the only problem i have is: when I am pressing the mouse buttons outside the window (ContextMenu), the window does not close. I can't find the event that can catch this action.
this is the code i am using now:
public partial class ContextMenu : Window
{
public ContextMenu()
{
InitializeComponent();
this.ShowInTaskbar = false;
this.Deactivated += new EventHandler(ContextMenu_Deactivated);
}
void ContextMenu_Deactivated(object sender, EventArgs e)
{
this.Hide();
}
protected override void OnDeactivated(EventArgs e)
{
base.OnDeactivated(e);
this.Hide();
}
protected override void OnMouseDown(MouseButtonEventArgs e)
{
base.OnMouseDown(e);
this.Hide();
}
protected override void OnKeyDown(System.Windows.Input.KeyEventArgs e)
{
base.OnKeyDown(e);
this.Hide();
}
protected override void OnLostFocus(RoutedEventArgs e)
{
base.OnLostFocus(e);
this.Hide();
}
}
non of the functions above catches the mouse press outside the window (ContextMenu).
I have tried to use http://www.hardcodet.net/taskbar, but the examples I found are not something like what i am looking for.
Looks like you need processing of global mouse hooks.
Here is nice solution to this issue
http://www.codeproject.com/Articles/7294/Processing-Global-Mouse-and-Keyboard-Hooks-in-C
A control cannot detect mouse clicks that are outside their bounding Rectangles. However, the Window can detect a mouse click anywhere within its border. Therefore, all you need to do is to handle a PreviewMouseDown event in the MainWindow.xaml.cs file and then pass a message to the relevant control each time the event is raised.
I believe you'll want to use Mouse.Capture to detect a click away from your window.
This question+answer may lead you in the right direction:
How do I use CaptureMouse or Mouse.Capture in my C# WPF application?

Categories