3 Replies - 2373 Views - Last Post: 01 December 2011 - 09:43 AM Rate Topic: -----

#1 morphuisogrady  Icon User is offline

  • New D.I.C Head

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

How call a method on one form/class in another

Posted 01 December 2011 - 06:57 AM

Hi all,

I'm pretty new to C# so forgive me for the stupid question. I want to draw some graphics on my form when it loads. The graphics should be identical on each form.

My thoughts were to create a public method DrawGraphics on Main form and then call it on OnPaint Event on each form. It works fine on MainForm but it is not accessible on any other form.

I'm thinking in order for it to be used I would have to instanitate the mainform class. As I require having only a single instance(or is there any other reason I would not?) I tried to make the Main Form static.

This gives the errors:

Error 1 'ELTS_Rental_Station_1.frmMainScreen.frmMainScreen()': access modifiers are not allowed on static constructors

Error 2 An object reference is required for the non-static field, method, or property 'ELTS_Rental_Station_1.frmMainScreen.InitializeComponent()'

Any advice? Is these even the correct approach for drawing identical grpahics across forms in a project?

Is This A Good Question/Topic? 0
  • +

Replies To: How call a method on one form/class in another

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9198
  • View blog
  • Posts: 34,553
  • Joined: 12-June 08

Re: How call a method on one form/class in another

Posted 01 December 2011 - 08:12 AM

Just a heads up - the C# programmers sub-forum is to discuss advanced and non-help related C# topics, so I bumped this up to the general help area.
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5513
  • View blog
  • Posts: 11,820
  • Joined: 02-June 10

Re: How call a method on one form/class in another

Posted 01 December 2011 - 09:36 AM

Welcome to C# programming. Its a fun realm to work in.

But it is fairly obvious that you aren't familiar with how to do form-to-form (class-to-class) communications properly. To start with we DON'T directly call the method of one form/class from another.

See FAQ #2 for some tutorials on doing this the right way. (That's how often this comes up)


FAQ (Frequently Asked Questions - Updated Nov 2011

Spoiler

Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5513
  • View blog
  • Posts: 11,820
  • Joined: 02-June 10

Re: How call a method on one form/class in another

Posted 01 December 2011 - 09:43 AM

Quote

My thoughts were to create a public method DrawGraphics on Main form and then call it on OnPaint Event on each form. It works fine on MainForm but it is not accessible on any other form.


Something a little closer would be to create your form with all the drawing bits. Then make more instances of that form, or inherit from that form. That way all your other forms know how to do the drawing.

Have the main form raise an event, probably with some arguments about what to draw.

AlertDoCustomDraw(myCustomDrawArguments);


When all the child forms 'hear' this event being raised they will then each do their own drawing. Its just like listening for a Button.Clickevent but in your case it would be a myMainForm.AlertDoCustomDraw event.

The idea being to isolate each form/class not to tightly bind them together in the way your approach will do.

There are a couple tutorials listed under beginner in the section below (Making your first application parts 1 & 2) that should help you a lot.


Resources, references and suggestions for new programmers. - Updated Nov 2011
Spoiler

This post has been edited by tlhIn`toq: 01 December 2011 - 09:44 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1