Click here to Skip to main content
15,890,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Inside the Paint event I want to be able to add some sort of methods? that i can evolve them as much as i want elsewhere in code.
I did some tests some time ago, and it implied PaintEventArgs (i think), but i don't remember exactly how i did it. e(MenuTop) or something i did. And on the other side, PaintEventArgs MenuTop () or something. I guessed a lot until i find the solution. That is why I am asking for help now.
void Screen_Paint(object sender, PaintEventArgs e)
     {
         //these methods must be rendered in a separate class(this one)
         MenuTop();
         MenuDown();
         Background();
         Player();
         Enemy();
         Treasure();
      }

Thank you.

What I have tried:

.........................................................................................................................................is just an idea.
Posted
Updated 2-Aug-18 12:08pm
v4
Comments
Eric Lynch 1-Aug-18 23:54pm    
Its a bit difficult to follow your terminology. By syntax (in your example), the following would be methods (NOT classes): MenuTop, MenuDown, Background, Player, Enemy, and Treasure. Though, by name, they do sound more like classes.

As an example, if you intend them to be classes, elsewhere you would typically instantiate them, something like the following:

private MenuTop menuTop = new MenuTop();

And, then perhaps in your ScreenPaint method, you might do something like the following:

menuTop.Screen_Paint(sender, e);

This would assume, that you had declared a method Screen_Paint, in the MenuTop class.

So, assuming you are clear on the terms, can you please improve your question so that it uses proper terms and matching syntax.

Alternatively, if you are unclear on the distinction between class and method, I suggest you might need to read a bit more about C# syntax and object oriented design in general before you try to tackle this problem. In the unlikely event this is the case, I'd be happy to provide some links to decent reading material.
_Q12_ 2-Aug-18 12:31pm    
I don't know why I wrote 'classes', but i was thinking on 'methods'. I was tired. Sorry about it. I was thinking way too high level i suppose. :) I repaired the mistake in the main question.

I did a test now, and i got what i wanted from first try - wow for me. But surely, after I look over _again_ over your answer, I've seen the resolve given from you too "menuTop.Screen_Paint(sender, e);" , so... Again, good job to you. :)

--------Please make a Solution-------------

Here is the _working_ test:
Sprite cat = new Sprite(WindowsFormsApplication2.Properties.Resources.catBlack);
void MenuTop(object sender, PaintEventArgs e)
{
e.Graphics.DrawImage(cat.Image, cat.Rectangle);
}

void Screen_Paint(object sender, PaintEventArgs e)
{
MenuTop(sender, e);
}

1 solution

To add to what Eric has said you seem to be confused as to how to "do things" in C# - that code is "procedural" rather than event driven.

In C# everything is inside a class - there is no concept of anything "outside classes", not even global variables, much less events. So there is no way to have a "outside classes" paint event - that would actually be the Paint event for the form as a whole, and you don;t create class instances in Paint events anyway (except of "on the fly" cFont and Brush items anyway) - you certainly wouldn't create major screen objects in a Paint event, because that could occur a hundred time a second giving you a hundred MenuTop items, a hundred Players, and so on.

Instead, your form would create a single instance of each, place it on the form, and let it be responsible for drawing itself in it's own Paint event.

This is complicated, and really needs a lot more than we can fit in a little text box - I'd suggest that you go back and read your course notes / recommended book again before you go any further!
 
Share this answer
 
Comments
_Q12_ 2-Aug-18 12:57pm    
Sorry, the mistake is mine. I was tired and i wrote 'classes' instead of methods. But is interesting your answer nevertheless.

I was curious actually from your answer, and i did try "outside classes" to make a field variable. I got this error: "Error 1 Expected class, delegate, enum, interface, or struct ". So... the system is working as it should. :) heh.
On the problem itself: i find the answer after all - with the help from Eric Lynch answer too. Which i encourage him to write direct Solutions from now on. :) Good job guys and thank you for your support!
I am also curious about what you said: "that code is "procedural" rather than event driven." Can you give me some examples what it means? I imagine that procedural is like those assemblers for MCU(microcontroler) and Procesors in general. Or i am wrong? I know about them because i did use MASM i think was called, for programming PIC16f84 in the 2000's. I also start programming in Pascal and c and c++(v1) - also to command those MCU or PIC's how they are properly called.
Eric Lynch 5-Aug-18 9:44am    
Procedural vs event driven programming is essentially acting vs reacting. You can find a bit more about it, plus other paradigms here: https://en.wikipedia.org/wiki/Comparison_of_programming_paradigms

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