I try to use mouse leave event handler but i can not work it.
private void Form1_MouseLeave(object sender, EventArgs e)
{
this.Close();
}
I don't want to fade. It should be closed immediately when the mouse cursor goes out of the form screen.
To close the whole application use
Environment.Exit(0);
The MouseLeave event will fire if the mouse enters a child control, which is probably not what you want.
Try using a timer:
private System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
private bool mouseEntered = false;
public Form1() {
InitializeComponent();
timer.Tick += timer_Tick;
timer.Start();
}
protected override void OnMouseEnter(EventArgs e) {
mouseEntered = true;
base.OnMouseEnter(e);
}
void timer_Tick(object sender, EventArgs e) {
if (mouseEntered && !this.Bounds.Contains(Cursor.Position)) {
this.Close();
}
}
If you have several forms I prefer Application.Exit().
Form.Close() only works for a single form application.
If there is only one form in that application you are using, then Application.Exit will close the application itself.
If there is one more form that you are navigating to from the main form, then in the new form, use Form2.Close(), this will take you to the back to the main application that you are using.
If you want to close both of them, then first give Form2.Close() and then give Application.Exit(). That would suffice.
I made a little test program to recreate your problem.
namespace OddFormTest
{
using System;
using System.Windows.Forms;
public class OddForm : Form
{
public OddForm()
{
this.Leave += Leaver;
}
[STAThread]
internal static void Main()
{
Application.Run(new OddForm);
}
private void Leave(object sender, EventArgs e)
{
this.Close()
}
}
}
As you infer in the question. The Leave event doesn't fire after you leave your application.
Related
Im not able to modify from my Form1 an element that belongs to Form 2.
public partial class Project : Form
{
public Form2 form = new Form2();
public Project()
{
InitializeComponent();
}
private void Project_Load(object sender, EventArgs e)
{
form.CreateControl();
}
private void buttonOpenForm2_Click(object sender, EventArgs e)
{
form.Show();
}
private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
var indata = *whatever serial input data here*
bool result = Int32.TryParse(indata, out int data);
if (result) {
form.chart1.Invoke(new Action(() => { form.chart1.Series[0].Points.AddY(data); }));
}
}
Any time I press my button in order to show Form2 and its chart, an exception is raised in form.chart1.Invoke: Invoke or BeginInvoke cannot be called on a control until the window handle has been created.
Why is this happening if i'm forcing form to do a CreateControl() ?
The error message is telling you what is wrong.
Your serial port is firing before the form2 (that holds your chart) is fully created
I guess you could check the visible flag (there are probably many others)
private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
...
if(!form?.Visible)
return;
form.chart1.Invoke(...
The assumption is if its not visible, you don't want to display the data
Further reading
Order of Events in Windows Forms
Application Startup and Shutdown Events
The Form and Control classes expose a set of events related to
application startup and shutdown. When a Windows Forms application
starts, the startup events of the main form are raised in the
following order:
Control.HandleCreated
Control.BindingContextChanged
Form.Load
Control.VisibleChanged
Form.Activated
Form.Shown
I am building a game. I have a Menu page, with a "Start" button. I would like to know how I can make the button direct the user to a new page with the game. I thought of simply changing all buttons' and labels' visibility to false, but that would be very messy. I thought of closing the form and reopening a new one as said here:
http://social.msdn.microsoft.com/Forums/windows/en-US/936c8ca3-0809-4ddb-890c-426521fe60f1/c-open-a-new-form-and-close-a-form?forum=winforms
Like this:
public static void ThreadProc()
{
Application.Run(new Form());
}
private void button1_Click(object sender, EventArgs e)
{
System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(ThreadProc));
t.Start();
this.Close();
}
but when I click the button, you can see the form closing and reopening again, and it doesn't even reopen at the same coordinates.
Is there any way to do such a thing? I want it to move from page to page as it would if this was a website. Is this possible? If so how? Thanks in advance :-)
Use UserControls for every view you need and then switch between them in your code by adding/removing them to/from your form.
Example: The start-page is a UserControl containing the button that starts the game. The game UI is another UserControl that contains all the logic and visuals for your game.
You could then use something like he following:
public class StartView : UserControl
{
...
private void btnStart_Click(object sender, EventArgs e)
{
// Raise a separate event upon the button being clicked
if (StartButtonPressed != null)
StartButtonPressed(this, EventArgs.Empty);
}
public event EventHandler<EventArgs> StartButtonPressed;
}
public class GameView : UserControl
{
...
}
public UserControl SwitchView(UserControl newView)
{
UserControl oldControl = null;
if (this.Controls.Count > 0)
{ oldControl = (UserControl)this.Controls[0];
this.Controls.RemoveAt(0);
}
this.Controls.Add(newView);
newView.Dock = DockStyle.Fill;
return oldControl;
}
You can now create the start view in Form_Load:
public void Form_Load(object sender, EventArgs e)
{
StartView v = new StartView();
v.StartButtonPressed += StartButtonPressed;
SwitchView(v);
}
The event handler that reacts to the start button being pressed would do this:
public void StartButtonPressed(object senderView, EventArgs e)
{
GameView v = new GameView();
UserControl old = SwitchView(v);
if (old != null)
old.Dispose();
}
I have created a Windows form in which i have added WebKit Browser. I have created a timer which sends user to 'PlayVideo' Form after every 5 seconds.
I want the timer to stop and start again if user clicks on the form.
The problem i am facing is that it is not detecting form clicks. If i remove WebKit Browser from FORM it detects Form Clicks and reset timer.
Kindly tell me a way to detect mouse clicks on WebKitBrowser.
private void timer1_Tick(object sender, EventArgs e)
{
videoplayer videoplayer = new videoplayer();
videoplayer.Show();
this.Hide();
timer1.Stop();
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Start();
}
private void Form1_MouseDoubleClick(object sender, MouseEventArgs e)
{
timer1.Stop();
timer1.Start();
}
Looks like you need some form-wide mouse event. In fact you can handle the DoubleClick event of the WebBrowser instead, however I've tried it and that event (and some other mouse events) is not supported. So you can try implementing the following form-wide mouse event handler with the help of IMessageFilter:
public partial class Form1 : Form, IMessageFilter
{
public Form1()
{
InitializeComponent();
Application.AddMessageFilter(this);
}
//...
//method implementation of the interface IMessageFilter
public bool PreFilterMessage(ref Message m)
{
//WM_LBUTTONDBLCLK = 0x203
if (m.Msg == 0x203) {
//your code goes here...
timer1.Stop();
timer1.Start();
}
return false;
}
}
I want to run my form (with all controls disabled)
and over it there will be another form for username and password run as showDialog!
people will not be able to go to the main form without login!
private void Form1_Load(object sender, EventArgs e)
{
this.Show();
Form2 f2 = new Form2 ();
f2.ShowDialog();
}
I tried the code above and it dose not work as it should!
how I can achieve it the way I need?
cheers
From the parent form:
childForm.ShowDialog(this);
That will make the child form modal to the parent form. As far as the location goes, there is a property off of the form (that you will want to set on the child form) that tells it where to start (center screen, center parent, etc)
System.Windows.Forms.Form implements IWin32Window, this is why it works.
It isn't clear what the issue/question is, but you could try making sure you pass in the parent-form, i.e.
using(var childForm = new MySpecialLoginForm(...)) {
childForm.ShowDialog(this);
}
erm...
DialogResult result = mySecondForm.ShowDialog()
That will disable the parent form until this one is closed. DialogResult will be an enum value that is something like OK/Cancel/YesNo etc
I typically use the following pattern if I want to do sth. after the Form has fully loaded:
public partial class BaseForm : Form
{
public event EventHandler Loaded;
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
Application.Idle += OnLoaded;
}
protected void OnLoaded(object sender, EventArgs e)
{
Application.Idle -= OnLoaded;
if (Loaded != null)
{
Loaded(sender, e);
}
}
}
If I derive my main form from BaseForm I have a Loaded event which, in your case, I would use as follows.
static class Program
{
[STAThread]
static void Main()
{
var mainForm = new MainForm();
mainForm.Loaded += (sender, e) => { new LoginDialog().ShowDialog(mainForm); };
Application.Run(mainForm);
}
}
Hey! I am not trying to push my luck here but I have another c# question.
I have tried every possible event I found using google.
Here is the code:
private void Form1_OnClose()
{
MessageBox.Show("I was closed -2");
}
private void Form1_Exit(object sender, EventArgs evArgs)
{
MessageBox.Show("I was closed -1");
}
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
MessageBox.Show("I was closed 0");
}
private void Form1_Closed(object sender, EventArgs e)
{
MessageBox.Show("I was closed 1");
}
private void Form1_FormClosed(Object sender, FormClosedEventArgs e)
{
MessageBox.Show("I was closed 2");
}
Not one of these trigger anything when I either do Alt+f4 or click on the X button.
What am I doing wrong here?
You might be missing actual subscription code, which is something along these lines:
this.Closing += Form1_Closing;
Instead, try overriding OnXXX methods - this is the preferred way of doing things.
The error is likely that you aren't wiring the events at the right time. Check your program.cs file. It should look something like this:
using System;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;
namespace Test
{
internal class Program
{
private static void Main(string[] args)
{
Form form = new Form2();
form.Closing += form_Closing;
Application.Run(form);
}
private static void form_Closing(object sender, CancelEventArgs e)
{
MessageBox.Show("Closing");
}
}
}
I just ran this and the event fired.
Are these methods actually assigned as event handlers? Go to design mode, select the form, then click the little lightning bolt above the properties window. Then find the event you want (Closing probably) and double click it.