Click here to Skip to main content
15,907,906 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Ok I tought this was working but I just realized that there is a problem,

I am saving the mouse coordinates to an array and then to a txt file. Here is the code:
Point p2 = PointToClient(Cursor.Position);
          string coords = string.Format("({0},{1})", p2.X, p2.Y);
          Clipboard.SetText(coords);
          imgcoor[0] = p2.X;
          imgcoor[1] = p2.Y;
          imgcoor[2] = p2.X;
          imgcoor[3] = p2.Y;


          TextWriter tw = new StreamWriter("xk24.txt",true);
          tw.WriteLine(imgcoor[0].ToString() + "," + imgcoor[1].ToString() + "," + imgcoor[2].ToString() + "," + imgcoor[3].ToString());
          tw.Close();


What i want it when i first click imgcoor[0] and imgcoor[1] should give me x1 and y1 and the second click should give me x2 and y2 as imgcoor[2] and imgcoor[3]. However right now it is only giving me x1 and y1 repeatedly so my out come for the first click is like this 407,156,407,156 where as it should be 407,156 and should wait for the second click and add the coordinates as x2 and y2 407,156,300,350 any ideas?

Thank you,
Posted
Comments
Christian Graus 21-May-10 9:06am    
Wow - I feel like I see an iteration of this code every couple of minutes. Do you know how to use the debugger ? You can also use Console.WriteLine and it will output in the IDE. Perhaps you need to learn to debug a bit more so you can diagnose some of this stuff yourself ?
TolgaCiftci 21-May-10 9:16am    
I totally do :) Learning slowly but surely

First you will need to distinguish between first and second clicks.
For that you could use a boolean variable which is set the first time this code executes.

The first time the code executes, only set imgcoor[0] and imgcoor[1] to the mouse co-ordinates.

The next time it executes, you can trace it is the second click via the boolean variable so you can set imgcoor[2] and imgcoor[3] to the new coordinates.
 
Share this answer
 
Don't use an array. Use a List. At that point, you can do something like this:

public class PointsList : List<point>
{
    public void SaveToFile(string filename)
    {
        StringBuilder data = new StringBuilder();
        foreach (Point point in this)
        {
            data.AppendFormat("{0},{1}\r\n", point.X, point.Y);
        }
        // do what you need to do to write data.ToString() to the file
    }
}</point>


If it were me, I wouldn't write one item at a time because that would create a crapload of overhead (opening/closing the file for each point written).
 
Share this answer
 
Comments
TolgaCiftci 21-May-10 10:06am    
Thanks for the advise
What is going wrong in your code is in this part
C#
imgcoor[0] = p2.X;
imgcoor[1] = p2.Y;
imgcoor[2] = p2.X;
imgcoor[3] = p2.Y;


You are using p2 twice, so you are getting it back twice.

Possibly it should be
C#
imgcoor[0] = p1.X;
imgcoor[1] = p1.Y;
imgcoor[2] = p2.X;
imgcoor[3] = p2.Y;

// or
imgcoor[0] = p2.X;
imgcoor[1] = p2.Y;
imgcoor[2] = p3.X;
imgcoor[3] = p3.Y;


or similar.

However, like others, I feel you are not using the best methodology. Still, press on, and once you get a feel for what's happening you might see a better way.

Good luck! :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900