Click here to Skip to main content
15,908,906 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
System.Drawing.Graphics graphicsObj;
graphicsObj = this.CreateGraphics();
Pen myPen = new Pen(System.Drawing.Color.Black, 5);
Rectangle myRectangle = new Rectangle(20, 200, 40, 80);
graphicsObj.DrawRectangle(myPen, myRectangle);


I created that rectangle in a method called draw1();
I have another method called draw2();..
How can I refer to that same rectangle in draw1(); to change it's background colour? (I know how to change the background colour, I just need to call it in method 2).. Like an instantiation kind of thing.
Posted
Updated 10-Feb-11 4:03am
v2

Do not create graphics! (This is a rule of thumb, but valid in 99.90% cases.)
This topic was discussed many times. Always use Paint event:

C#
myControl.Paint += delegate(object sender, PaintEventArgs eventArgs) {
   using (Pen myPen = new Pen(System.Drawing.Color.Black, 5)) {
      Rectangle myRectangle = new Rectangle(20, 200, 40, 80);
      eventArgs.Graphics.DrawRectangle(myPen, myRectangle);
      DrawRectangleDetail(eventArgs.Graphics, myRectangle);
      DrawOnTopOfRectangle(eventArgs.Graphics, myRectangle);
      DrawSomethingElse(eventArgs.Graphics);
   } //using: here myPen.Dispose() is called automatically
}; //myControl.Paint


Also, where do you call myPen.Dispose? In the code above it is shown: through the using construct (see comment).

Now, about your methods Draw1, Draw2, etc. Call them all from the handler show above. Make them accepting a Graphics parameter, if you need to re-use already initialized Rectange, add a Rectangle parameter to those which require that. See the calls to Draw... in my sample.

—SA
 
Share this answer
 
v2
Comments
Manas Bhardwaj 10-Feb-11 11:04am    
+5
Then just promote your rectangle to a member variable instead of local variable.

Rectangle _rect = new Rectangle(20, 200, 40, 80);
void Draw1()
{
   //use _rect here...
}
void Draw2()
{
   //use _rect here...
}
//use this function to update your rectangle when needed
void UpdateRect()
{
   _rect = new Rectangle(...);
}
 
Share this answer
 
v2
make the return type of method1 as Rectangle[^]

private Rectangle Draw1()
{
	Rectangle _myRect = null;
	//whatever
	return _myRect;
}


Now you can pass it to the method2 to make changes there.
C#
private void Draw2(Rectangle PobjRect)
{
    //whatever
}
 
Share this answer
 
v2
Comments
johannesnestler 10-Feb-11 10:29am    
this is not the solution for this kind of problem - moving the variable to a higher scope-level is the answer - (Look at Answer2).
Manas Bhardwaj 10-Feb-11 10:59am    
It surely is. You can pass the Rectangle object to the method2 to make the changes in it.

I also updated my answer to make it more obvious.

Global variables are never encouraged. If your method can handle it why global variables?
johannesnestler 10-Feb-11 12:14pm    
Hi Manas,

I'm suprised by your answer. Of course in a common sense your solution is "correct".
But for this specific problem I have never seen such code - you write your drawing functions like this, returning the drawn objects? If you think about a typical Forms-App where do you define your Rectangle in the first place? Where is the "master" method, managing/dispatching the returned references? In a Form-Constructor - no? in Main method - no? So where? After concidering this (I have ~14 years experience in Windows-GUI programing) it brought me to the upper comment: "This is not the solution for THIS KIND OF PROBLEM".

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