Click here to Skip to main content
15,888,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I created a rectangle within a function
Now I want to pass this rectangle into a function of events
How can I do this?

Sample code I created:

C#
        private void button2_Checked(object sender, RoutedEventArgs e)
        {
       
        //create new rectengle

           Rectangle rect = new Rectangle();
           rect.Visibility = Visibility.Visible;
           rect.Width = slider1.Value;
           rect.Height = slider2.Value;
           SolidColorBrush myBrush = new SolidColorBrush(Colors.Green);
           rect.Stroke = Brushes.Red;
           rect.StrokeThickness = 1;
           rect.Fill = myBrush;

            // Creat event to the new rect

           rect.MouseLeftButtonDown += rect_MouseLeftButtonDown;
           rect.MouseLeftButtonUp += rect_MouseLeftButtonUp;
           rect.MouseMove += rect_MouseMove;
           

          

            myCanvas.Children.Insert(0, rect);

        }


private bool _isRectDragInProg;

// one of the event function
//Function is called but the value rectangle unidentified
     private void rect_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {

           
            _isRectDragInProg = true;
          
            //Here I want to pass the rectangle

                rect.CaptureMouse();
           
        }
Posted
Updated 7-Nov-13 22:50pm
v2

1 solution

Hi Member,

So your Problem is just how to obtain a reference to the clicked (draged...) rectangle in the eventhandler method? This is easy - you will get it through the sender parameter like this:

C#
Rectangle rect = sender as Rectangle;


Or did you mean something else?
 
Share this answer
 
v2
Comments
[no name] 8-Nov-13 5:10am    
tnx!

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