Click here to Skip to main content
15,888,066 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to to pass a rectangle from one function to a second function
For some reason the rectangel read to the event function as null
How can I do this?



C#
      private Point startpoint;
      private Rectangle rect;

      private void canvas1_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
      {

          startpoint = Mouse.GetPosition(canvas1);

// the rectengle that i creat and i what to pass him to the canvas1_MouseMove Event function
          Rectangle rect = new Rectangle();
          rect.Stroke = Brushes.Black;
          rect.StrokeThickness = 1;


          canvas1.Children.Add(rect);



      }



      private void canvas1_MouseMove(object sender, MouseEventArgs e)
      {
         //to here i want to pass the rectangle
         // when i debug the code the rect value is null!
         // so i cant control his Properties


              rect.Width = 5;// her i got excsption
                             //"Object reference not set to an instance of an object."
              rect.Height = 6;





      }
Posted
Updated 14-Nov-13 4:40am
v6
Comments
BillWoodruff 14-Nov-13 10:17am    
I can't understand your question. You create a variable named in the scope of whatever (Class ?) and in both MouseRightButtonDown and MouseMove functions. That's three Rectangles named 'rect.

The TextBox you set text of doesn't make use of 'rect.

You need to rewrite your question.

1 solution

This is the simplest way to do it.

C#
        private Point startpoint;

// "rect" is declared at Class level so both canvas1_MouseRightButtonDown and canvas1_MouseMove can access
        private Rectangle rect; 
 
        private void canvas1_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            startpoint = Mouse.GetPosition(canvas1);
 
            rect = new Rectangle();  // Changed declaration and assignment to just assignment
            rect.Stroke = Brushes.Black;
            rect.StrokeThickness = 1;
 
            canvas1.Children.Add(rect);
         }
 
        
 
        private void canvas1_MouseMove(object sender, MouseEventArgs e)
        {
        if (rect != null) // Check to be sure rect is OK. Mouse will move before right-click.
        {
                 rect.Width = 5;
                rect.Height = 6;
        }
                
        }
 
Share this answer
 
v10
Comments
[no name] 14-Nov-13 10:13am    
i try the i got exption on

rect.Width = Math.Abs(w); //Object reference not set to an instance of an object.
Mike Meinz 14-Nov-13 10:20am    
That means that canvas1_MouseMove was called and click == true was true before canvas1_MouseRightButtonDown was completed. What if you moved click = true; to the bottom of canvas1_MouseRightButtonDown?

And maybe add a check into canvas1_MouseMove that rect is not null to ensure that canvas1_MouseRightButtonDown was executed before canvas1_MouseMove.

See revised Solution 1.
[no name] 14-Nov-13 10:33am    
Not related to the variable click
I just want to move the rectangle from one function to another
Mike Meinz 14-Nov-13 10:36am    
The variable click is what you are using as a signal to indicate that the rect variable was instantiated. See revised Solution 1.

if (click == true && rect != null)
[no name] 14-Nov-13 10:42am    
the variable click has nothing to do With this
i revised my question

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