1

I have a checkbox at the moment where, when I click it, it should allow the user to click two places on a canvas, and then a messagebox with the coordinates of both clicks appear, and then the checkbox should uncheck itself. I've tried various things and keep running into a few problems.

  1. Checking the box is a RoutedEventArgs where as clicking on the canvas is a MouseButtonEventArgs.
  2. I'm unable to store the second mouse click (the first click appears twice); I've tried various for loops, while loops, etc.
  3. I'm unable to get the box to uncheck itself after the message box appears, regardless of where I put the .Checked == false. I get an error that says system.windows.etcetc.checked can only appaer on the left hand side of += or -=.

I'd like to handle the whole thing in a function related to the checkbox aka the routedeventargs and not a canvas click method.

I can figure out #2 but 1 and 3 have me stumped.

This is a sample of the method which is subscribed to from a canvas mousedown in the xaml:

   public void get_Scaling(object sender, MouseButtonEventArgs e)
    {
        Point startPoint;
        Point endPoint;

        while (Scale_btn.IsChecked == true)
        {
            startPoint = e.GetPosition(canvas1);

            endPoint = e.GetPosition(canvas1);

            System.Windows.MessageBox.Show("Start point is" + startPoint + "and end point is" + endPoint, "test", MessageBoxButton.OK, MessageBoxImage.Information);
        }
    }
pfinferno
  • 1,779
  • 3
  • 34
  • 62

2 Answers2

2

There are a couple of issues I see.

1) The way to programmatically uncheck a CheckBox is to use the IsChecked property.

Scale_btn.IsChecked = false;

2) Remember that your while loop is running inside of a single MouseDown event handler. You won't be able to capture two different MouseDown events within that while loop. In order to achieve your goal, you need to bring the Point objects outside of the event handler and use another variable to keep track of which click you are capturing.

    bool firstPointCaptured = false;
    Point startPoint;
    Point endPoint;

    private void get_Scaling(object sender, MouseButtonEventArgs e)
    {

        if (Scale_btn.IsChecked == true)
        {
            if (!firstPointCaptured)
            {
                startPoint = Mouse.GetPosition(canvas1);
                firstPointCaptured = true;
            }
            else
            {
                endPoint = Mouse.GetPosition(canvas1);
                System.Windows.MessageBox.Show("Start point is" + startPoint + "and end point is" + endPoint, "test", MessageBoxButton.OK, MessageBoxImage.Information);
                Scale_btn.IsChecked = false;
                firstPointCaptured = false;
            }
        }
    }
Gordon True
  • 963
  • 7
  • 11
  • Just before I checked this I was trying something with a bool flag, and this confirmed it! Thanks for clarifying the IsChecked vs Checked. I always thought 'Checked' was the control for checking/unchecking the box. – pfinferno Jan 28 '15 at 16:13
2
  1. This is how you get mouse coords (you do not need e, so your question 1 is invalid).

  2. Currently your starPoint == endPoint. Do you understand you have to get 2 mouse click events (while remembering points) before you are able show MessageBox?

  3. You have to use IsChecked property, instead of Checked event to change checked state of CheckBox.

Community
  • 1
  • 1
Sinatr
  • 20,892
  • 15
  • 90
  • 319