Click here to Skip to main content
15,894,646 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to make a Life bar.
I can make a basic one in Form1, no problem -see the code below.
But I want now to make a Class that is holding some properties for this Life bar, like value,position, size, color, isfull, ishalf, isalmostdead,is dead, but the most important, I want it to be drawn in a paint event too.
and in Form1_Paint to paint it from that Class... somehow.
--------
in pseudocode it should look like this:
private void Form1_Paint(object sender, PaintEventArgs e)
{
Class1.PaintLifeBar(); //and thats it!
//(or something similar) here i give you an idea how it should look.)
}
--------
It is possible?
If yes, how?

What I have tried:

private void Form1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.FillRectangle(new SolidBrush(Color.LawnGreen), new Rectangle(10, 10, 55, 5));
        }
Posted
Updated 15-Mar-17 21:18pm
v4

1 solution

At first :
if you have additional Information you should use the function "Improve Question" and add it at this Point ...

To your Question :
You only need to create a new class which derives from Control. Now you could create your own Control with own (or customized) Properties which could be used like (for example) a Button or a Label ...

VB
Public Class LifeBar
   inherits Control

' now put your Properties in here

' Override the Controls OnPaint-Method to do your painting
    Protected Overrides Sub OnPaint(e As PaintEventArgs)
        ' here you could do your painting
        ' you can see, that the control gives you it's own PaintEventArgs with which you could work



        MyBase.OnPaint(e)
    End Sub

End Class
 
Share this answer
 

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