I want my application to hide when the user want to close it.
after that by clicking on notifyicon the form will be shown .
Here is my coded : [form name = login]
private void login_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
this.hide();
}
and for notifyicon :
private void NIcon_MouseClick(object sender, MouseEventArgs e)
{
this.Show();
}
but it doesn't work! What is wrong?
You can try also ShowInTaskbar property
private void LoginForm_FormClosing(object sender, FormClosingEventArgs e)
{
Hide();
e.Cancel = true;
ShowInTaskbar = false;
}
private void LoginForm_DoubleClick(object sender, EventArgs e)
{
ShowInTaskbar = true;
Show();
}
You have to add them to the event handlers also, try this on form_load
this.FormClosing += LoginForm_FormClosing;
notifyIcon1.DoubleClick += LoginForm_DoubleClick;
Then you can do like this
login form;
private void LoginForm_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
form.hide();
}
private void LoginForm_DoubleClick(object sender, EventArgs e)
{
form.Show();
}
Also read this it will be helpful.
Related
I want to move my WinForms application into the system tray on the click of a tool strip menu item, but when I try to mention the method I've written, I get an error like: Conversion from System.EventArgs to System.Windows.Forms.MouseEventArgs not possible, but I don't want to convert anything, I'm just giving the method my argument e.
Declaration of methods I'm trying to use:
private void Form1_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
Hide();
notifyIcon.Visible = true;
}
}
private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
Show();
this.WindowState = FormWindowState.Normal;
notifyIcon.Visible = false;
}
(note that these two are simple event handler when resizing my form and double-clicking on the icon in the system tray)
I want to use them both like so
private void inTrayVerschiebenToolStripMenuItem_Click(object sender, EventArgs e)
{
Form1_Resize(sender, e);
notifyIcon_MouseDoubleClick(sender, e);
}
but on the latter e, I'm getting the aforementioned error. I'm currently using VS 2019 with C#
notifyIcon_MouseDoubleClick requires a MouseEventArgs argument; right now, e is an EventArgs parameter; whether it is actually a MouseEventArgs instance: we don't know; but: you can't pass an EventArgs to something that is demanding a MouseEventArgs value. The compiler is simply telling you that it can't do that for you, so: you're going to have to be more specific and tell it how to get a MouseEventArgs value to pass to notifyIcon_MouseDoubleClick.
Perhaps alternatively; restructure the code a bit, i.e.
private void Form1_Resize(object sender, EventArgs e)
{
HandleResize();
}
private void HandleResize()
{
if (this.WindowState == FormWindowState.Minimized)
{
Hide();
notifyIcon.Visible = true;
}
}
private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
HandleDoubleClick();
}
private void HandleDoubleClick()
{
Show();
this.WindowState = FormWindowState.Normal;
notifyIcon.Visible = false;
}
private void inTrayVerschiebenToolStripMenuItem_Click(object sender, EventArgs e)
{
HandleResize();
HandleDoubleClick();
}
How can I change the image of a form PictureBox, from another form?
Example. Form2 image needs to change Form1 image.
I tried this.
exmp.Code 1
//This is for Form1
private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
Form2 Fr2 = new Form2();
Fr2.Owner = this;
}
private void pictureBox2_Click(object sender, EventArgs e)
{
Fr2.Show();
}
//This is for Form2
private void pictureBox2_Click(object sender, EventArgs e)
{
try
{
(this.Owner as Form1).pictureBox1.Image = pictureBox2.Image;
}
catch
{
}
}
This code doesn't work!
But if I try this it will work.
exmp.Code 1
//Form1
private void pictureBox1_Click(object sender, EventArgs e)
{
Form2 Fr2 = new Form2();
Fr2.Show();
Fr2.Owner = this;
}
//Form2
private void pictureBox2_Click(object sender, EventArgs e)
{
try
{
(this.Owner as Form1).pictureBox1.Image = pictureBox2.Image;
}
catch
{
}
}
But I cannot use the second code because I need to click a ContextMenuStrip to open Form2.
Any question write in comments!
In Form1, "Fr2" needs to be declared at Form level so you can access it from both "pictureBox1_MouseEnter" and "pictureBox2_Click". Set the Owner by passing "this" to Show().
This is for Form1:
private Form2 Fr2;
private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
Fr2 = new Form2();
}
private void pictureBox2_Click(object sender, EventArgs e)
{
if (Fr2!=null && !Fr2.IsDisposed)
{
Fr2.Show(this); // <-- pass Form1 as the Owner
}
}
This is for Form2:
private void pictureBox2_Click(object sender, EventArgs e)
{
Form1 Fr1 = this.Owner as Form1;
if (Fr1!=null)
{
Fr1.pictureBox1.Image = pictureBox2.Image;
}
}
To access "pictureBox1" in Form1, though, you'd have to change the Modifiers property of it to public.
i want to have a custom close window.
My scenario is:
The original X button function's is to minimize the window, while the custom will actually close it. My default X is like below:
// add handler by double clicking on Closing event in Properties Box
// My below X button
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
this.Hide();
}
However, when i try to create the custom close button like this:
void closeButton_Click(object sender, RoutedEventArgs e)
{
Close();
}
It will just minimize like the default. So how do i it? Thanks for reading.
private bool _forceClose;
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if(!_forceClose)
{
e.Cancel = true;
this.Hide();
}
}
void closeButton_Click(object sender, RoutedEventArgs e)
{
_forceClose = true;
Close();
}
You can remove closing event in the button click
public MainWindow()
{
InitializeComponent();
this.Closing += MainWindow_Closing;
}
void MainWindow_Closing(object sender, CancelEventArgs e)
{
e.Cancel = true;
this.Hide();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.Closing -= MainWindow_Closing;
this.Close();
}
Please note I'm trying to do the following, when I hover the mouse on a button I want the panel to be visible, when the mouse leaves the button or panel, the panel should not be visible. Below you can see the code I have, but the panel it's not staying visible.
private void FormMain()
{
buttonMenu.MouseEnter += new EventHandler(buttonMenu_MouseEnter); //open panel
buttonMenu.MouseLeave += new EventHandler(buttonMenu_MouseLeave);
panelMenu.MouseEnter += new EventHandler(panelMenu_MouseEnter);
panelMenu.MouseLeave += new EventHandler(panelMenu_MouseLeave);
mbB1.MouseEnter += new EventHandler(mbB1_MouseEnter);//button in panel
mbB2.MouseEnter += new EventHandler(mbB2_MouseEnter);//button in panel
}
private void buttonMenu_MouseEnter(object sender, EventArgs e)
{
this.panelMenu.Visible = true;
}
private void buttonMenu_MouseLeave(object sender, EventArgs e)
{
this.panelMenu.Visible = false;
}
private void panelMenu_MouseEnter(object sender, EventArgs e)
{
this.panelMenu.Visible = true;
}
private void panelMenu_MouseLeave(object sender, EventArgs e)
{
this.panelMenu.Visible = false;
}
private void mbB1_MouseEnter(object sender, EventArgs e)
{
this.panelMenu.Visible = true;
}
private void mbB2_MouseEnter(object sender, EventArgs e)
{
this.panelMenu.Visible = true;
}
One solution that came to my mind is using a short timer. You may further optimize it to cut down on LoC, but it works. You may also lower the timer's delay, but 100ms is what I think would be a safe one.
On a side note, I don't think this is a good design. If you want this kind of behaviour, you should either use a ContextMenuStrip or a Click event on the button, and a hide event on panelMenu.MouseLeave. Still, if it's what you really need, this is how I solved it:
public partial class Form1 : Form
{
private bool mouseInPanel;
private Timer hideTimer;
public Form1()
{
InitializeComponent();
buttonMenu.MouseEnter += new EventHandler(button_MouseEnter);
buttonMenu.MouseLeave += new EventHandler(button_MouseLeave);
mbB1.MouseEnter += panelButton_MouseEnter;
mbB2.MouseEnter += panelButton_MouseEnter;
panelMenu.MouseEnter += new EventHandler(panelMenu_MouseEnter);
panelMenu.MouseLeave += new EventHandler(panelMenu_MouseLeave);
hideTimer = new Timer {Interval = 100};
hideTimer.Tick += hidePanel;
}
private void button_MouseEnter(object sender, EventArgs e)
{
this.panelMenu.Visible = true;
}
private void button_MouseLeave(object sender, EventArgs e)
{
hideTimer.Start();
}
private void panelMenu_MouseEnter(object sender, EventArgs e)
{
mouseInPanel = true;
this.panelMenu.Visible = true;
}
private void panelMenu_MouseLeave(object sender, EventArgs e)
{
mouseInPanel = false;
hideTimer.Start();
}
private void panelButton_MouseEnter(object sender, EventArgs e)
{
mouseInPanel = true;
this.panelMenu.Visible = true;
}
private void hidePanel(object sender, EventArgs e)
{
hideTimer.Stop();
if (!mouseInPanel) this.panelMenu.Visible = false;
}
}
What I figured is your mouse actions needed a little bit of a delay, else the panel (along with your buttons mbB1 and mbB2) gets hidden before those buttons' actions could be triggered.
This is because by entering the panel buttons you leave the panel, and it disappears (along with it's capability to receive mouse actions) right before mbB1/mbB2's action can trigger.
I have two forms, Form 2 is inheriting from Form 1.
What I need to do is that when I close both Form 1 and Form 2 another form which asks if user is sure to quit appears. Then if user clicks Yes, another form which asks if the user wants to save the game appears if and only if the form which the user closes is Form 2 and not Form 1 since for Form 1 there is no saving necessary.
This is what I managed to do:
// These are the Form 1 closing and closed event handlers:
private void GameForm_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
SureClose sc = new SureClose();
sc.StartPosition = FormStartPosition.CenterScreen;
sc.Show();
}
private void GameForm_FormClosed(object sender, FormClosedEventArgs e)
{
MainMenu menu = new MainMenu();
menu.Show();
}
Then in Sure Close: // Please note that Tournament is Form 2 inheriting from GameForm (Form 1)
private void yesButton_Click(object sender, EventArgs e)
{
this.Hide();
if (GameForm.ActiveForm is Tournament)
{
SaveGame sg = new SaveGame();
sg.StartPosition = FormStartPosition.CenterScreen;
sg.Show();
}
else
{
GameForm.ActiveForm.Close();
}
}
private void noButton_Click(object sender, EventArgs e)
{
this.Hide();
}
// This is the SaveGame Form:
private void saveButton_Click(object sender, EventArgs e)
{
// Still to do saving!
}
private void dontSaveButton_Click(object sender, EventArgs e)
{
this.Hide();
GameForm.ActiveForm.Close();
}
The problem is that when in the yesButton event handler in SureClose Form I have GameForm.ActiveForm.Close(), this is going back to the GameForm Closing event handler therefore the SureClose dialog is appearing again.
I tried doing: if (e.CloseReason() == CloseReason.UserClosing)
but obviously it doesn't work either because the reason of closing will always be the user :/
How can I solve this?
Thanks a lot for any help !
Form1 :
private void GameForm_FormClosing(object sender, FormClosingEventArgs e)
{
if(SureClose())
{
SaveChanges();
}
else
{
e.Cancel = true;
}
}
private bool SureClose()
{
using(SureClose sc = new SureClose())
{
sc.StartPosition = FormStartPosition.CenterScreen;
DialogResult result = sc.ShowDialog();
if(result == DialogResult.OK)
{
return true;
}
else
{
return false;
}
}
}
protected virtual void SaveChanges()
{
}
Form2:
protected override void SaveChanges()
{
using(SaveGame sg = new SaveGame())
{
sg.StartPosition = FormStartPosition.CenterScreen;
DialogResult result = sg.ShowDialog();
if(result == DialogResult.OK)
{
//saving code here
}
}
}
SureClose form and SaveGame form:
private void yesButton_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
}
private void noButton_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
}