Calling Application.Exit sends the WM_QUIT message to the application, this the pump catches and the WM_CLOSE event is not (typically) called, so OnClosing is not executed. That much you've discovered. When handling the WM_QUERYENDSESSION notification message, though, you should set Message.Result = new IntPtr(1); in order to essentially return TRUE as the return value so that the shutdown process isn't canceled. By not setting this value, FALSE is essentially returned and the shutdown process is cancelled as you have noticed.
For more information, see the documentation for the WM_QUERYENDSESSION notification message at http://msdn.microsoft.com/library/en-us/sysinfo/base/wm_queryendsession.asp[^] and pay close attention the return value documentation and the remarks section. Hopefully this will help, but it won't combine those two handlers. Honestly though, it really doesn't matter - it solves the problem and that's what counts. Besides, the Closing event is triggered by the WM_CLOSE message which isn't sent to Windows unless the application pump handles an appropriate message and sends or posts the message itself (at least, from what I remember).
Now, if I select the first item in the list, and press delete, the exception IndexOutOfRange occurs, however the item is then successfully deleted. If I select any other item in the list and try to delete it, it works fine, no exception
Why does deleting the first item cause an exception
I tried the code above, but again i get exactly the same results! I can delete any item in the listbox no problems, but when i try to delete the first item, it deletes, but causes an IndexOutOfRangeException!!
I tried creating a whole new project with just a listbox, textbox add and delete button, and it worked perfectly, no errors at all. It just causes problems in my current project!!!
You should sett up some breakpoints and find out what index the first Item in your listbox has. It should have index 0.
It might be that you add the item to your listbox in a special way?
What is the most efficient way to wait for a worker thread to complete. I'm making a file sending program and I want to only send one file at a time (i.e. wait for the previous send to finish), but I want the user thread to keep processing messages because I have status indicators and other options that need to be available while sending.
Sounds like what you want is one dedicated thread that sits idle in an infinite loop waiting for files to send. Each send request would be enqueued into a blocking queue. The dedicated thread wakes up when an item is available in the queue and processes it accordingly.
Thanks for the reply,
I was thinking too hard, I solved the problem with a single boolean value that I set when I begin and end a send thread, the user thread just check the value of the boolean...somtimes its too easy to see.
You should look at invoking delegates asynchronously as well: Including Asynchronous Calls[^]. Might help solve the problem another way, even though I see you have solved it using state (just make sure you synchronize access to that state using a Monitor - ala the lock keyword if you like - against a static object or something similar).
I have a combobox with autocomplete.
This combobox is linked to a field of a database.
I want to do this and I don't know how:
When I choose an item of the combobox to display in a form automatically
the information of that field.
I've made the form and I linked the controls to database using Bindings.
I use OleDb.
Please help me!
Hi, I've just spent weeks making a huge 'wizard' type form for a client and then they phone me today and tell me they want it as a windows form because its costing them too much money entering the data on the internet
What I really want is a utility for converting web forms to windows forms but since I realise this may not exist can anyone offer me any tips on how to convert it.
Unfortunatley we got the money up front, which seemed like a good idea at the time because it was good money
I can convert most of the form fields to their windows equivalent with a find and replace and most of the form logic is well encapsulated. The only real problem is then I've got to position all the form fields, actucally that is a nightmare because theres over a 100 textboxes and groups of radio buttons etc etc.
If you get REALLY (and I mean REALLY) desperate, you could host your ASP.NET solution on each machine .
I'd look at it as a learning opportunity, and a chance to do a UI exactly how you want it .
I, for one, do not think the problem was that the band was down. I think that the problem may have been that there was a Stonehenge monument on the stage that was in danger of being crushed by a dwarf.
I am getting a weird problem with the tabpage here is a step by step account of how to reproduce my problem
1) add a tabcontrol and then a tabpage to a form
2) add a custom contorl to the tabpage (a normal control eg label will work as well but i am using a custom control)
3) add functionality that allows you to drag the control/label (i just use the controls mousedown to start DoDragDrop, and then use the drag_enter/move events of the tabpage to make it drag)
4) now set a background image, some big image say 1mb and 1024x768 and drag the contorl/label, you should see that it is slow to paint the control when dragging
HOW CAN I MAKE THE PAINTING SMOOTH?
I have tried double buffer overriding onpaintbackground etc.. but that is not the solution as if i add my control to a panel and then set the panels backgroundimage to a large image the painting is smooth!!!! So the panel does something to make painting smooth that the tabpage does not. The solution i have come across is detailed below however it has a weird problem which i will list below. For now use the following steps to produce my problem
Start a new project
1) inherit from tabpage and produce a custom tabpage class with the code below
2) add tabcontrol to form and add the inherited tabpage to it
3) add user control to tabpage then add functionality to drag the control
4) make an Image object in the and set it to a large image in the form class name it LARGE_IMAGE
e.g. Image LARGE_IMAGE = Image.FromFile(@"C:\image.jpg");
5) add 4 buttons to the form which the following in their click events
tabpage.imagex = LARGE_IMAGE;
tabpage.imagex = null;
tabpage.BackGroundImage = LARGE_IMAGE;
tabpage.BackGroundImage = null;
6) OK now that everythings ready we can begin the test! If you press Button 1 you will see that when you drag the control the image appears underneath it however the painting isnt very smooth but is smoother than in the first example above where you set the backgroundimage. Now if you press Button 2 so the tabpage.imagex is set to null. painting should be smooth again as no background is set. (Forget about the backimage getting erased and painting when u move the control i understand why that is happening that is not the Problem).
Now press Button 3 to set the tabpage's backgroundimage and drag the control around it is slow to paint now press button 4 to set the backgroundimage to null. OK THIS IS THE WEIRD PART, Press Button 1 and tabpage.imagex should paint SMOOTH!!!!! IT is fine now.
I cant understand why this happens? It only paints smooth after setting and unsetting the tabpage's backgroundimage. I have tried doing the following
in the constructor of the inherited tabpage but that does not make it smooth, i have added the 3 lines of code to a seperate button again to no change. It seems i have to add the code to different buttons only then will it work!!! WIERD.
Please please can anyone shed any light on this situation? I want smooth painting on the tabpage with out pressing mulitples buttons LOL
First, never store a Graphics object and always call Dipose on it when you're finished.
Second, using the ClientRectangle of the Button means that the rectangle will be within the Button, which maintains its own state. Drawing a rectangle within said bounds won't be visible. Instead you must inflate the rectangle (you can use Rectangle.Inflate) by the size of the pen (5, in your example).
You'll also want to do this in the override for OnPaint in your Panel-derived class (or in the Paint event if from another - possibly container - class. Painting this rectangle once will work (if you take into account what I mentioned above) but once the surface requires repainting it won't show up (and only part of it - the invalidated part - will disappear if the whole surface doesn't need repainting). So, in DrawScanArea(Button button) instead set a value to signify that the border should be painted (like a non empty rectangle which you would have to store anyway) and in OnPaint check that state and draw the border if necessary.
Sorry but I realy don't seem to understand the OnPaint event or something, couse I don't get the rectangle to show.
I have a Main class, I've created my own panel class(MyPanel) and added it to the Main form.
On the panel I want to draw the rectangle.(The rectangle shall only be visable for a short period before it will be drawn at a different position)
If I in the MyPanel class add:
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
In my Main class i have:
private void DrawScanArea(Rectangle rect)
Should i create a System.Windows.Forms.PaintEventArgs in my Main class to call up the OnPaint(...) in the panel class?(well I can't since it is protected), so how will it be called up? I would asume it happens when I call the line: grfx.DrawRectangle(pen,rect); in DrawScanArea(Rectangle rect)
, or should the grfx.DrawRectangle(pen,rect);, be placed in the OnPaint(..) in MyPanel class?
First, OnPaint is not an event, it's a method that corresponds to the Paint event. You override OnPaint in a class that derives from another control when you want to control the painting from within that class. You have the Paint event when you want to control the painting from another class. The base class (Control, actually) will call the virtual OnPaint method along with the PaintEventArgs. You should call the base class's OnPaint which will raise the Paint event and might even do other painting operations specific to the base control: