|
Inside your MouseDown event:
1. Capture the mouse
2. Begin your repetitive operation
3. Handle the MouseUp event to stop the operation and release the mouse
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
The problem with this "general recommendation" is that it will fail if there is an "open-ended loop," like a while loop, with no halting condition, in the MouseDown event. ... And the programmer somehow expects the MouseUp event to terminate the Loop.
They will then find that the no-halt loop never relinquishes control, and MouseUp will not be called. And your application is "off to Disneyland."
See my answer below for three strategies to handle this scenario.
best, Bill
"The first principle is that you must not fool yourself, and you are the easiest person to fool." Richard Feynman
|
|
|
|
|
The MouseDown event is fired when you press the mouse button in, so it will only be fired once for every time you press the mouse. The corresponding event you need to hook into is the MouseUp event.
|
|
|
|
|
Best solution would be to capture the mouse down event, start a thread that loops while listening for a boolean that will become false by the time the mouse up event is fired.
hope that helps.
Δημιουργία websites, web εφαρμογών και εφαρμογών ηλεκτρονικού υπολογιστή. http://www.remiakstudio.gr
|
|
|
|
|
+5 Yes, a threaded option will work: would be interesting if you would say something about how the thread will update the boolean which will be running in (theoretically) a main form's gui environment.
best, Bill
"The first principle is that you must not fool yourself, and you are the easiest person to fool." Richard Feynman
modified 10-Feb-12 1:48am.
|
|
|
|
|
static bool mousedown ?
Set to true in mousdown event which also launches the thread with the loop init, set to false on mouseup event, loop ends and thread stops.
That's how I would do it.
V.
|
|
|
|
|
For example: a while loop running in a MouseDown EventHandler ... with no terminating condition ... is going to run "forever." The MouseUp EventHandler is never going to be called: and you lose control of your Application.
There are three strategies that could work here to avoid this "pitfall."
By the way, the same issues described here would apply in the case of using a specific key press to trigger a non-halting loop.
The technical reason for the "pitfall" is that an unterminated loop launched in a MouseDown EventHandler is going to block the thread ... in this case the main thread, which happens to be your application.
1. Raven has mentioned the threading option already: hope he'll flesh it out with some code. It will work !
2. Quick and nasty: create a WinForm project: note: this example will undoubtedly spawn a series of responses/sermons on the "utter evil" of using "Application.DoEvents," but I already hang garlic on my computer to avoid being attacked by its meme, myself
a. add a Button named "button1" : hook up the MouseDown and the MouseUp EventHandlers to the Button as shown below:
b. add this code:
private bool IsMouseDown = false;
private int tick = 0;
private void button1_MouseDown(object sender, MouseEventArgs e)
{
IsMouseDown = true;
while (IsMouseDown)
{
tick++;
Application.DoEvents();
}
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
IsMouseDown = false;
Console.WriteLine(String.Format("ticks = {0}", tick));
}
3. Use a Timer : activate the Timer in the MouseDown EventHandler; de-activate in the Mouse-Up EventHandler. Put the code to be done in the loop (assuming it has no built-in halt checking) inside the Timer's Tick EventHandler.
"The first principle is that you must not fool yourself, and you are the easiest person to fool." Richard Feynman
modified 10-Feb-12 2:43am.
|
|
|
|
|
|
Do not do 2 unless there's really no alternative. 3 is a far better non-threaded approach.
|
|
|
|
|
5'd for solution number 3, because that's how I'd do it.
|
|
|
|
|
If this is for something UI related, I'd start a timer instead of a new thread. That avoids the various synchronisation issues and is good enough for UI updates.
The other question is: can you do whatever you're trying to do on mouse move? Dragging things around is a common scenario that you can do without a loop/timer.
|
|
|
|
|
Hi,
is it possible to replace the "Application / Form" Icon, when minimized to TaskBar (not Tray), during runtime with a picturebox ?
I want a mouseover, get the color from cursor and put the current color "in the icon". Mousecursor moves, Taskbar (Icon/P-Box) color changes.
Greetings from Cold Germany,
T0M
|
|
|
|
|
What is shown in the taskbar is one of the icons belonging to your application, there is no PictureBox involved here. You can switch icons any time you like, I'm not sure how fast Windows will pick it up though.
And then you must be careful, an ICO file typically contains icons at several sizes; which icon from your ICO file is being shown on the taskbar may well depend on the Windows version you use and maybe also some Windows settings (e.g. since Vista, Windows can stack together the icons of similar processes; since Win7 Windows can show a progress bar on top of the app's taskbar button).
I'm afraid you will need to experiment a lot.
Luc Pattyn [My Articles] Nil Volentibus Arduum
Fed up by FireFox memory leaks I switched to Opera and now CP doesn't perform its paste magic, so links will not be offered. Sorry.
|
|
|
|
|
Not with a picture box as far as I'm aware. As Luc has correctly stated, what you see is an icon from your application.
You could maybe create an icon of the solid colour you want dynamically in memory and assign that...? Untested but may work.
|
|
|
|
|
This icon is the one assigned to the Form.Icon property: documentation[^].
|
|
|
|
|
Thanks for the answers and suggestions
|
|
|
|
|
I am working on a Png encoder software . This software has a requirement that it should generate a png file without the PLTE(palette) chunk , so I want to programmatically delete the "palette chunk" using C#.Net.
Anybody out here knows how it can be done using the dotnet framework?
|
|
|
|
|
Why don't you just not encode it as paletted in the first place?
Anyway if you're going to forcefully remove the PLTE chunk (which is pretty easy), beware that that makes the PNG invalid (indexed PNG's need a PLTE chunk).
|
|
|
|
|
Yes right ... Deleting palette chunk will make the PNG invalid i know that .. but thats the requirement.
You said that the deleting palette chunk is easy. How do i implement that in C#.Net??
Kindly reply.
|
|
|
|
|
Ok that's odd, but ok. This is what I would do.
Read the file back with a BinaryReader. Beware that integers in PNG are stored big-endian, so if you need their integer value you have to byte-reverse it (there is of course no good reason to reverse the FourCC's, just reverse the constants you test them against).
Write all chunks to an other stream unless the FourCC of the chunk is PLTE (FourCC's can not be read as strings because they are not strings in the way BinaryReader wants them, just read an int32 and test against 0x45544C50 (ETLP in ASCII) - the other FourCC's are not relevant, just write anything "unknown" to the destination stream).
|
|
|
|
|
You'll have to work with the file format directly. The .Net API won't let you create an indexed image without the colour table, because it's invalid.
|
|
|
|
|
I have created a .png file from say a bmp using the Image.Save() call. Now I want to delete the PLTE chunk from the .png file created , how do I do it??
Best Regards
|
|
|
|
|
Read the file using file reading tools (e.g. FileStream), find the PLTE chunk and rewrite the file not including it. This will require you to understand the PNG file format so you can recognise the part you want to process differently.
|
|
|
|
|
hello guys... I have this database application on <b>machine 1</b>. Now I want to save some of the records on <b>machine 2</b>, in sql server. This is client-server architecture. Client is not the problem here, it will be merely a record like
ID - Name - StartTime - EndTime
--- ------ --------- --------
The problem is server. I dont know what to implement on machine 2 (the server). Which of the following should I use
- WCF
- socket programming
- or remotely access the sql server on machine 2.
I am just confused whats the best choice?
|
|
|
|
|
Depends.
Will all the machines always (forever) be on the same network behind the firewall? If so, you can just use ADO.NET or whatever and connect directly to both SQL servers.
Will there be a lot of data transferred?
I'd DEFINITELY avoid WCF because its very slow and network traffic is very large (relative to the real data) because its all SOAP & WSDL.
If you need to access the server from outside the firewall, transfer a lot of data, etc. I'd go with the socket server middle man approach. Thats really the most scalable, but also the most work to get going .
|
|
|
|