C# Minimize to Tray - c#

I know you will be thinking "Not again this question", as I found like a hundred results when I searched for it. But when I put in the code as described on the pages here, it just minimizes to right above the start menu.
This is the code I use (I added a message box to see if the code gets triggered, but the message box never pops up):
private void Form1_Resize(object sender, EventArgs e)
{
MessageBox.Show("Works1");
if (WindowState == FormWindowState.Minimized)
{
this.Hide();
}
}
Because I don't know if it links to Form1 or Form, I have tried both, to no avail.
private void Form_Resize(object sender, EventArgs e)
{
MessageBox.Show("Works");
if (WindowState == FormWindowState.Minimized)
{
this.Hide();
}
}
Now, when you double click on the Form, it puts this line in the Form1.Designer.cs:
this.Load += new System.EventHandler(this.Form1_Load);
Do I need a similar line to trigger the minimize event?
As you can see, I am completely lost :)
Oh, and it doesn't minimize to the taskbar, as I am using the following code to hide the form on run:
protected override void OnLoad(EventArgs e)
{
Visible = false; // Hide form window.
ShowInTaskbar = false; // Remove from taskbar.
base.OnLoad(e);
}

You need the event
private void Form1_Resize(object sender, EventArgs e)
{
}
Creating Event Handlers on the Windows Forms Designer

Add a NotifyIcon component to your Form. Make sure you set an icon via the properties pane otherwise it will be invisible.
Create an event handler for the form's Control.SizeChanged event. In that event handler place the following code:
sample code:
private void MainForm_SizeChanged(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
ShowInTaskbar = false;
}
And then to make the form visible again the NotifyIcon.MouseDoubleClick event handler you can place the following code:
private void trayIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
WindowState = FormWindowState.Normal;
ShowInTaskbar = true;
}

The basic thing you need to know is events. Events are triggered when certain things happen to your form (or any control). For example, when the form is resized, or loaded, or clicked, an event is raised. You can hook into this event to execute your own code when the event happens.
In your case you want to execute code to minimize the form, on the event that the form is resized. So you need to hook your method to the resize event. The name of your method is not relevant, so let's use a better name:
private void HideWhenMinimized(object sender, EventArgs e)
{
MessageBox.Show("Works1");
if (WindowState == FormWindowState.Minimized)
{
this.Hide();
}
}
To hook your HideWhenMinimized method into the Resize event of the form, you have to do it like this:
this.Resize += new System.EventHandler(this.HideWhenMinimized);
If you add that line of code in the form's constructor or Load event, then your code gets called as soon as the form is resized.

Related

Close Form when not clicking on it

I want to know if there is any event when you click on the rest of the screen and not the Windows Form the Form closes. Is it because of the ShowDialog()?
My Main Form is just with a notifyIcon and when I click it I call Form1.ShowDialog();
Main.cs:
private void ShowForm1(object sender, EventArgs e)
{
form1.Left = Cursor.Position.X;
form1.Top = Screen.PrimaryScreen.WorkingArea.Bottom - form1.Height;
form1.ShowDialog();
}
Form1.cs:
private void Form1_Load(object sender, EventArgs e)
{
label1.Text = "Test";
}
You need to run the dialog box non-modally, not modally. Think about it, when you run it modally, the dialog box takes over the UI and plays games with mouse-clicks elsewhere, preventing you from running. You don't want it to be modal anyway.
I created a simple Windows form with a button that includes this handler to open a small AutoCloseDialog form I created (and populated with a few controls):
private void button1_Click(object sender, EventArgs e)
{
var dlg = new AutoCloseDialog();
dlg.Show(this); //NOTE: Show(), not ShowDialog()
}
Then, in the AutoCloseDialog form, I wired up the Deactivate event. I did it in the designer (where this code is generated):
this.Deactivate += new System.EventHandler(this.AutoCloseDialog_Deactivate);
Finally, here is the handler for the Deactivate event.
private void AutoCloseDialog_Deactivate(object sender, EventArgs e)
{
this.Close();
}
I think this does what you are asking.
Yes, you can create a similar function like this, which closes the Form if the Form lost focus (in Form1.cs)
public void Form_LostFocus(object sender, EventArgs e)
{
Close();
}
and then you add the LoseFocus EventHandler (in Form1.Designer.cs):
this.LostFocus += new EventHandler(Form_LostFocus);

C# WinForms: bring back focus to form after button click

my simple WinForms app consists of one Form which has 2 important methods:
private void MainWindow_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Q)
{
qKeyPressed = true;
}
keyTimer.Start();
}
private void MainWindow_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Q)
{
qKeyPressed = false;
}
keyTimer.Stop();
}
In form's constructor I use button1.TabStop = false; to set button1 focus to false, so my form can handle methods KeyUp and KeyDown. I want my form to be focused even after button1 click event. I tried:
button1.TabStop = false;
this.Focus();
in private void button1_Click(object sender, EventArgs e) but this doesn't seem to work and button after click looks like focused and methods KeyUp and KeyDown are disabled. How can my problem be solved? Thanks in advance
Run this in the button click function:
this.ActiveControl = null;
When you click the button, you set it as the active control. Removing this property will let you put focus on your form.

Why on Chart control the MouseWheel event never called ? And also the AxisViewChanged never called?

I have on form1 designer a windows forms chart control.
In form1 constructor i added:
chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
chart1.MouseWheel += chart1_MouseWheel;
Then i have the MouseWheel event:
void chart1_MouseWheel(object sender, MouseEventArgs e)
{
MessageBox.Show("hi");
}
And in the designer i also added the event AxisViewChanged:
private void chart1_AxisViewChanged(object sender, ViewEventArgs e)
{
paintToCalaculate = true;
chart1.Invalidate();
}
But using a breakpoint in the MouseWheel event or on the AxisViewChanged event it's never get and stop there.

Invoking a Button Click Event of UserControl from its parent Form using Function Keys Like (F3,F6)

Code in User Control (Windows User Control).
I have a button and when click on the button some logic will fire.
below is button event.
private void btnCancel_Click(object sender, EventArgs e)
{
// Some Logic
}
I have Main form (Windows form)
I am adding the above user control in a Panel of my Main Winform.
From here I want to invoke the user control btnCancel_Click event using some function Keys Like (F3,F4).
This is a very new thing for me. Please help me in this regard.
Thanks .
Krishna
Set your parent form KeyPreview attribute to true and then in the KeyDown event of your parent form, look for if(e.KeyCode == Keys.F3), then write a public method for your usercontrol which fires the btnCancel_Click. You can access it from your parent form.
Parent Form:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F3)
{
MyUserControl.FireButtonEvent();
}
}
Usercontrol:
public void FireButtonEvent()
{
this.btnCancel_Click(null, EventArgs.Empty);
}
Create a KeyEvent method. for example:
private void KeyDown(Object sender, KeyEventArgs e)
{
string sKey = e.KeyValue.ToString();
if (sKey==112) {//SomeCode;} // F1
}
In the designer code link the method to a key press event on the main form:
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.KeyDown);
Look on the net for the key values of the button you want to activate it,
F1 to F24 are values 112 to 135.

How to determine which form was brought to front by clicking it?

I have an application with a Panel containing children Form objects. When I click one of the children Form it brings to front. I would like to know which one is in front now...
I've looked in event list but cant find proper event form my purpose :(
These methods doesn't work:
protected void OpenedFileForm_Enter(object sender, EventArgs e)
{
MessageBox.Show("enter");
}
protected void OpenedFileForm_Click(object sender, EventArgs e)
{
MessageBox.Show("click");
}
protected void OpenedFileForm_Activated(object sender, EventArgs e)
{
MessageBox.Show("activated");
}
protected void OpenedFileForm_MouseClick(object sender, MouseEventArgs e)
{
MessageBox.Show("mouse click");
}
protected void OpenedFileForm_Shown(object sender, EventArgs e)
{
MessageBox.Show("shown");
}
OpenFileDialog openFile1 = new OpenFileDialog();
openFile1.DefaultExt = "*.txt";
openFile1.Filter = "TXT Files|*.txt|RTF Files|*.rtf";
if (openFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK &&
openFile1.FileName.Length > 0)
{
switch (Path.GetExtension(openFile1.FileName))
{
case ".txt":
txtForm childTXT = new txtForm();
this.childForms.Add(childTXT);
childTXT.Parent = this.mainPanel;
childTXT.richTextBox1.LoadFile(openFile1.FileName, RichTextBoxStreamType.PlainText);
childTXT.Show();
break;
}
}
Have you tried the Form.Activated Event?
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.activated(v=vs.80).aspx
Edit:
If you are in an MDI application, you might need to use MdiChildActivate instead.
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.mdichildactivate.aspx
This code can only work when you set the Form.TopLevel property to false. Which makes it turn into a child control, almost indistinguishable from a UserControl.
This has many side-effects, for one there is no notion of "front" anymore. The Z-order of child controls is determined by their position in their parent's Controls collection. And it affects the events it fires, Activated and Deactivated will never fire. Furthermore, the Form class was designed to be a container control, it doesn't like taking the focus itself. Its child controls get the focus, the Form class doesn't have any use for focus. Which is why the Enter, Click and MouseClick events don't fire, they are events that require focus.
Long story short, what you are trying to do doesn't make a wholeheckofalot of sense. If it is strictly the Z-order you want to fix then write an event handler for the MouseDown event:
void OpenedFileForm_MouseDown(object sender, MouseEventArgs e) {
var frm = (Form)sender;
frm.BringToFront();
}
You could add frm.Select() to get the Enter event to fire, but only do that if the form doesn't contain any focusable controls itself. Do note that there is evidence that you don't assign the events correctly in your code. The Shown event does fire. It is also important that you set the FormBorderStyle to None, the title bar cannot indicate activation status anymore.
Ok, I got this! Thx for help everyone. You gave me a hint to think about equity of my strange MDI idea where Panel is parent for other Forms. I Removed SplitContainer containing Panel and just did standard MDI application, where Forms are MDIChildren of main Form.
childTXT.MdiParent = this;

Categories

Resources