Reputation: 23
Okay, so I needed to make a simple animation in c# to use as a loading icon. This worked all fine and good so lets take this square as an example
PictureBox square = new PictureBox();
Bitmap bm = new Bitmap(square.Width, square.Height);
Graphics baseImage = Graphics.FromImage(bm);
baseImage.DrawRectangle(Pens.Black, 0, 0, 100, 100);
square.Image = bm;
So with that I made my animation and everything here worked, but then I realized that I need my animation to be in a class so I can call it from my co workers programs to use the animation. This is where the problem came in, I made my class and I did everything the same way but in a class instead of the form I then called my class from my form but the screen was blank and there was no animation. Is there something that needs to be passed in order to do this?
namespace SpinningLogo
{//Here is the sample of my class
class test
{
public void square()
{
PictureBox square = new PictureBox();
Bitmap bm = new Bitmap(square.Width, square.Height);
Graphics baseImage = Graphics.FromImage(bm);
baseImage.DrawRectangle(Pens.Black, 0, 0, 100, 100);
square.Image = bm;
}
}
}
private void button1_Click(object sender, EventArgs e)
{//Here is how I call my class
Debug.WriteLine("11");
test square = new test();
square.square();
}
Upvotes: 1
Views: 72
Reputation: 11025
Pass your test
class a reference to the PictureBox
that is on the form:
namespace SpinningLogo
{
class test
{
public void square(PictureBox thePB)
{
Bitmap bm = new Bitmap(thePB.Width, thePB.Height);
Graphics baseImage = Graphics.FromImage(bm);
baseImage.DrawRectangle(Pens.Black, 0, 0, 100, 100);
thePB.Image = bm;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
test square = new test();
square.square(myPictureBox); //whatever the PictureBox is really named
}
You could also pass the Form
itself (using this
), but then you'd still have to ID the PictureBox
control (I assume).
Upvotes: 1
Reputation: 146
You should pass to your test class Form instance, and not define PictureBox in test class. PictureBox should be field of Form, and by Form instance u will get access to your PictureBox.
Upvotes: 0