How to use my paint method on other forms. Not working!

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 2119 Views - Last Post: 01 December 2011 - 03:29 PM Rate Topic: -----

#1 morphuisogrady  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 25-April 11

How to use my paint method on other forms. Not working!

Posted 01 December 2011 - 01:57 PM

Hi there,

I want to daraw some graphics on a form. Each form should be identical so I need to repeat this on all forms.

On the main forms paint event I have the following
public void DrawGraphics()
        {
            Graphics surface = this.CreateGraphics();
            Pen pen1 = new Pen(Color.Black, 1.0F);
            Rectangle rec1 = new Rectangle(710, 2, 80, 568);
            surface.DrawRectangle(pen1, rec1);
            LinearGradientBrush brush1 = new LinearGradientBrush(rec1, Color.Black, Color.DarkKhaki, LinearGradientMode.Vertical);
            surface.FillRectangle(brush1, rec1);

        }



Now on the second form in order to call the method I must first instantiate the form. But when I call the method the graphic is not drawing. Anyone any ideas?

private void frmPaymentRequired_Paint(object sender, PaintEventArgs e)
        {
            frmMainScreen myForm = new frmMainScreen();
            myForm.DrawGraphics();
            
        }



Is This A Good Question/Topic? 0
  • +

Replies To: How to use my paint method on other forms. Not working!

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13758
  • View blog
  • Posts: 54,934
  • Joined: 12-June 08

Re: How to use my paint method on other forms. Not working!

Posted 01 December 2011 - 01:59 PM

I moved this out of the non-help/advanced topic section and into the general help.
Was This Post Helpful? 0
  • +
  • -

#3 morphuisogrady  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 25-April 11

Re: How to use my paint method on other forms. Not working!

Posted 01 December 2011 - 02:16 PM

View Postmodi123_1, on 01 December 2011 - 01:59 PM, said:

I moved this out of the non-help/advanced topic section and into the general help.


Thats ok. I probably wont get an answer anyway!
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13758
  • View blog
  • Posts: 54,934
  • Joined: 12-June 08

Re: How to use my paint method on other forms. Not working!

Posted 01 December 2011 - 02:29 PM

Such the positive outlook! :clap:
Was This Post Helpful? 0
  • +
  • -

#5 morphuisogrady  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 25-April 11

Re: How to use my paint method on other forms. Not working!

Posted 01 December 2011 - 02:32 PM

I know. No one ever answers me here.

DO you know any good forums for beginners?
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13758
  • View blog
  • Posts: 54,934
  • Joined: 12-June 08

Re: How to use my paint method on other forms. Not working!

Posted 01 December 2011 - 02:37 PM

Aw, I disagree! Looking at your prior posts each one has someone responding to it! Heck some were even me!
Was This Post Helpful? 0
  • +
  • -

#7 morphuisogrady  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 25-April 11

Re: How to use my paint method on other forms. Not working!

Posted 01 December 2011 - 02:44 PM

Awww Thank you!


Can you answer my new post :)?
Was This Post Helpful? 0
  • +
  • -

#8 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: How to use my paint method on other forms. Not working!

Posted 01 December 2011 - 02:44 PM

Instead of creating the Graphics Surface, why not pass it in as reference parameter to the function.
Was This Post Helpful? 0
  • +
  • -

#9 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13758
  • View blog
  • Posts: 54,934
  • Joined: 12-June 08

Re: How to use my paint method on other forms. Not working!

Posted 01 December 2011 - 02:48 PM

My thought was why have four forms? Why not have for panels on one form? You know - sort of like how maya or a 3d designer app would do i!
Was This Post Helpful? 0
  • +
  • -

#10 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 183
  • View blog
  • Posts: 785
  • Joined: 27-October 11

Re: How to use my paint method on other forms. Not working!

Posted 01 December 2011 - 02:48 PM

I saw you declared your method public, that kindda breakes one of the top concepts of OOP (Encapsulation), if it has to be used by an another form use it like this:

public void DrawGraphics()
{
this._DrawGraphics();
}

private void _DrawGraphics()
{
//Do your work here...
}



But there is an even better way to do this, make a class for that
Let's call that class Painter.cs!

Now for the method...

If you do it this way it becomes a problem to initialize the Graphics, you could:

If you are calling this method from some event initialize the graphics there, then pass it as an parameter to the method... :)

Here's the idea:

//This is in your Form
private void some_Event(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            Painter.DrawGraphics(ref g);
        }

// This is in your Painter class

public void DrawGraphics(ref Graphics g)
        {
            this._DrawGraphics(ref g);
        }

private void _DrawGraphics(ref Graphics g)
        {
            // Do your drawing here
        }



This may be a litlle bit complicated, but It's even more complicated passing data between forms (sort of)...
Just a quick pointer: the ref keyword tells that you are REFERENCING an existing object, and not COPYING INTO A BUFFER

Some info about the ref keyword on the MSDN

By the way this is what AdamSpeight2008 said, at least the reference parameter

Of topic:

Isn't function in Java, and method in C#? Or can you say function in C#?... @AdamSpeight2008 Just curious...

This post has been edited by RexGrammer: 01 December 2011 - 02:54 PM

Was This Post Helpful? 0
  • +
  • -

#11 morphuisogrady  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 25-April 11

Re: How to use my paint method on other forms. Not working!

Posted 01 December 2011 - 02:56 PM

This is a lot more complicated for me (new to C#) but if its the better way to do it then I'll do it this way. I'm real eager to learn how to do things right from the start.
Was This Post Helpful? 0
  • +
  • -

#12 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 183
  • View blog
  • Posts: 785
  • Joined: 27-October 11

Re: How to use my paint method on other forms. Not working!

Posted 01 December 2011 - 03:01 PM

Well you could make a custom event, see this tutorial here

and call the method in each of these events...
...and make a method in a individual class but you would still need to "ref" the graphics...

(This is just a more complicated way of doing the above mentioned)

P.S. A litlle forum etiquette: you don't have to quote a post if it's the first above you...
And it also isn't complicated, just try it out, you'll see... (You can't know if you don't try it)

This post has been edited by RexGrammer: 01 December 2011 - 03:03 PM

Was This Post Helpful? 0
  • +
  • -

#13 morphuisogrady  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 25-April 11

Re: How to use my paint method on other forms. Not working!

Posted 01 December 2011 - 03:04 PM

Sorry about my quoting. Wont happen again.
Was This Post Helpful? 0
  • +
  • -

#14 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5102
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: How to use my paint method on other forms. Not working!

Posted 01 December 2011 - 03:12 PM

View PostRexGrammer, on 01 December 2011 - 03:48 PM, said:

Isn't function in Java, and method in C#? Or can you say function in C#?... @AdamSpeight2008 Just curious...


Function, method...six up, half-a-dozen down. Technically all named functions in C# are methods, since "method" implies that it is bound to a class, and all C# functions are such. But they're not mutually exclusive terms; a function may or may not be a method (in general, not in C#).

Personally, I say "method" far more often than function, unless I'm talking about lambdas or anonymous functions. In which case, they're still bound to classes, but in a somewhat different mechanism.

It's complicated, but it's really just semantics. We know what you mean with either word. Hell, we'll probably even figure out "subroutine".
Was This Post Helpful? 0
  • +
  • -

#15 morphuisogrady  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 25-April 11

Re: How to use my paint method on other forms. Not working!

Posted 01 December 2011 - 03:13 PM

Hi Guys,

I have made an attempt at making my class Painter as described. I have one error I hope you can help me understand.

 class Painter
    {
        public void DrawGraphics (ref Graphics g)
        {
            this._DrawGraphics(ref g);
        }

        private void _DrawGraphics(ref Graphics g)
        {
            Graphics surface = this.CreateGraphics();
            Pen pen1 = new Pen(Color.Black, 1.0F);
            Rectangle rec1 = new Rectangle(710, 2, 80, 568);
            surface.DrawRectangle(pen1, rec1);
            LinearGradientBrush brush1 = new LinearGradientBrush(rec1, Color.Black, Color.DarkKhaki, LinearGradientMode.Vertical);
            surface.FillRectangle(brush1, rec1);

        }


    }
}



The error I get is:
Error 1 'ELTS_Rental_Station_1.Painter' does not contain a definition for 'CreateGraphics' and no extension method 'CreateGraphics' accepting a first argument of type 'ELTS_Rental_Station_1.Painter' could be found (are you missing a using directive or an assembly reference?)

Another thing I do not get:

Why have a private method _Drawgraphics with the code to paint inside it and then have a public method called DrawGraphics calling the _Drawgraphics method?

Why not just have one public method?

This post has been edited by morphuisogrady: 01 December 2011 - 03:15 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2