user1444829
user1444829

Reputation: 1

PictureBox won't appear C#

I am trying to do a logical gates program. I'm trying to create a PictureBox with the class NOT, the problem is that it doesn't appear when I call the create method inside form1 and the PictureBoxwon't appear when I click the list item. The problem is (I think) that it doesn't know that it is in form1 even though I use the FindForm() method. And call it from forms

---Source Code for NoT class---

class NOT: Shape
{
    PictureBox px = new PictureBox();    
    Image img = Image.FromFile(@"C:\NOT.png");
    public NOT(int x, int y) : base(x,y)
    {
        px.FindForm();
        px.Visible = true;
        px.Enabled = true;

    }

    public override void CreatePicture()
    {
        Point p1 = new Point(xx, yy);
        px.Image = img;
        px.Location = p1;

        px.Show();      
    }
}


---Source code for the SHape Class---
abstract class Shape
{
    protected int xx, yy;    //private Point location;

    public Shape(int X, int Y)
    {
        xx = X;
        yy = Y;
    }

    public abstract void CreatePicture();
}
private void nOTToolStripMenuItem_Click(object sender, EventArgs e)
    {
        nt.CreatePicture();


    }
NOT nt = new NOT(12,23);

Upvotes: 0

Views: 1267

Answers (3)

Ignacio Gómez
Ignacio Gómez

Reputation: 1637

You must add the pictureBox. For example, if the PictureBox is in a panel:

panel.Controls.Add();

if it is in the form you just put Controls.Add();

Hope it helps.

Upvotes: 1

Vsevolod
Vsevolod

Reputation: 375

You must place PictureBox to the form for draw it:

PictureBox px = new PictureBox();
....
px.Parent = YouFormForExample;//Component who is draw this picture box

Upvotes: 0

shf301
shf301

Reputation: 31404

You need to associate the picture box with a form by adding it to the forms Controls collection. Calling FindForm() only returns the currently assigned form; in your case it will be returning null.

public override void CreatePicture(Form form)
{
    Point p1 = new Point(xx, yy);
    px.Image = img;
    px.Location = p1;

    form.Controls.Add(px);

    px.Show();      
}

Upvotes: 2

Related Questions