I'm building an app in C# using VS 2008 - I've added a method of checking if a file has changed when it is closed, but this only works for the File>close menu. Is there any way to get the red X in the top right to actually do anything before shutting everything? If so, how? I've only been doing C# for a few days, and this is incredibly confusing - there are no methods for the overall interface window anywhere. Help is much appreciated. Thanks.
Use the Form.FormClosing event. Or the FormClosed event, that comes later and cannot cancel the closng.
And from the File|Close menuItem, just Close() the Form.
If you do that, you have 1 spot (FormClosing) where all the possible ways of closing a Form (including ALT+F4 and TaskManager) converge.
Do take a look at e.CloseReason, you don't want to be in the way when it is for example WindowsShutDown
You could probably do it through the window's closing event: http://msdn.microsoft.com/en-us/library/system.windows.forms.form.closing.aspx
Related
Hi
Is there any way to find out if the close button (x button on form) was clicked. However without involving FormClosing and FormClosed events ?
Without those Events? That's a tough one...no, I don't think so.
I mean, you hook yourself into the message pump of the form to figure out if those actions were invoked, but seems like a little overkill to me.
You can use PreProcessMessage to see the message on its' way to your form.
I have a C# windows forms application. The way I currently have it set up, when Form1_Load() runs it checks for recovered unsaved data and if it finds some it prompts the user if they want to open that data. When the program runs it works alright but the message box is shown right away and the main program form (Form1) does not show until after the user clicks yes or no. I would like the Form1 to pop up first and then the message box prompt.
Now to get around this problem before I have created a timer in my Form, started the timer in the Form1_Load() method, and then performed the check and user prompt in the first Timer Tick Event. This technique solves the problem but is seems like there might be a better way.
Do you guys have any better ideas?
Edit: I think I have also used a background worker to do something similar. It just seems kinda goofy to go through all the trouble of invoking the method to back to the form thread and all that crap just to have it delayed a couple milliseconds!
I would use Form1_Shown()
Use the Shown event. It seems to suit what you need, and will only display the first time the form is shown.
Form f1 = new Form();
f1.Shown += new EventHandler(f1_Shown);
public void f1_Shown(object sender, EventArgs e)
{
// Show dialog in here
}
Try the "Shown" event:
Form.Show Event
Using a Windows.Forms.Timer is a good, stable, well-known, and easily understood technique for doing what you want. I would avoid any other timer objects.
The form's Shown event works well.
Overload / override the Show method. (My preferred technique for greater control.) In this method, I would do the checking needed. When ready, I would call the base.Show method, then do any other processing, such as message boxes, prompts, logging, or whatever.
How do I create a ballon tool tip with a close button.
I can show a tooltip:
TaskbarIcon.ShowBalloonTip(10000);
but I can't do the opposite:
TaskbarIcon.CloseBalloonTip();
Or even a way to show a close box on a Balloon Tip.
I saw this question posted on another site but with no (free) answer.
Thanks in advance
I was able to find a simple answer. Instead of using:
TaskbarIcon.ShowBalloonTip(10000);
I could use the second form of this function:
TaskbarIcon.ShowBalloonTip(10000,"Title","Message",ToolTipIcon.None);
This actually adds a close box to the balloon tip!
You might find this interesting:
http://www.tooltips.net/
This question has a helpful answer on closing the balloon.
Unless you need to hook an event on close, you don't need a button on the balloon, and even if you do, you can hook the balloon's click event to accomplish the same thing.
There are flags that allow you to do things like put an X in the upper right hand corner of the balloon so that the user can dismiss it. See here for more info:
http://msdn.microsoft.com/en-us/magazine/cc188923.aspx
I had a working program (Windows Forms Project) complete with buttons, labels, textboxes e.t.c. and the underlying code.
In an attempt to shortcut my work, I decided to add tab controls and move everything in my main form into tab 1 (cut and pasted).
As you can imagine it didn't work. I then got rid of the tab conrol and pasted everything back into the main form but the program doesn't work anymore.
Can someone tell me what's wrong please?
I'm working in MS V studio 2008 express
Thanks.
I have done this many times, but I usually just drag them into the TabControl. Maybe in the cut and paste operation your controls have become unwired from the event declarations.
The event handlers that you coded are still there. However, they are not associated with the control any more. I'm not sure if you're using VB.Net or C#, but the fix is the same - it's manual and tedious if you have a bunch of controls, but not too difficult. Here are the instructions for fixing a single button control, and you'll have to apply the concepts across the board.
These instructions are specific to C#. I can give you VB instructions as well as I've done this plenty of times.
Double click on the button to generate a new event handler. If the button is named Button1, the original event handler was probably called Button1_Click. Now it should be Button1_Click1.
Delete the Button1_Click1 function and compile. You'll get errors and if you doible-click on the error in the error pane it will take you to the form,designer.cs file to a line that looks like:
this.Button1.Click += new System.EventHandler(this.Button1_Click1);
Change this to
this.Button1.Click += new System.EventHandler(this.Button1_Click);
to point to the previously existing event handler, and the event handler will be fixed.
Possibly some of the events had code lost.
If you do it again it will probably work.
For an alternative method see my message
HI,
I have a form in C# app. On this form I capture a KeyDown event Alt+U which will open a second form. In the second form I have a toolStripButton with shortcutkey Alt+U (the same which I used to open the form with) which prints a document. Now, my problem is when I open the second form It will automatically trigger the event of clicking toolstripbutton since it has the same shortcutkey as I used to open the form with. How can I prevent this from happen.
Regards Johan
This doesn't answer your question, but you really should think about making two different shortcut keys for these two very different actions. Having two identical shortcut keys that do two entirely different actions is very confusing IMO.
To answer your question though, I would have some property on the second form like "ShouldRaise" or something, and only raise the Alt + U event in the second form if that flag is true. Set it to false initially, but then in the KeyUp in the first form, set it to true.
Would it not just be easier to change the shortcut of one to something else? I agree with BFree its not the best design have the same shortcut for 2 completely different functions. All shortcuts/accelerator keys should be unique.
Why not change the shortcut for the form page changing to something like:
Ctrl+Right (Go to next page)
Ctrl+Left (Go to previous page)
On the second form do you actually have a ToolStripMenuItem instead? (a ToolStripButton doesn't have the ShortcutKeys property).
Do you instantiate a new form when the user presses Alt-U on the parent form?
Did you check the sender object on the handler that prints the document to see if it was the parent form?
Can't seem to reproduce your problem; a little explaining would help.
Another easy solution is to do some check of what form you are in from the event listener. You could just return inside the event handler inside your second form.
Again not the most elegant solution but should be a decent fix.