2012-06-08 34 views
0

我想要做一个逻辑门程序。我试图用类NOT创建一个PictureBox,问题是当我在form1中调用create方法时,它不会出现,并且当我单击列表项时,PictureBox不会出现。问题是(我认为)即使使用FindForm()方法,它也不知道它是在form1中。 而从调用它的形式PictureBox不会出现C#

---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); 

+1

但你永远不会告诉图片框,它应该住在窗体内!所以它位于空间中(或者在程序的记忆中,但没有人画出它).. – gbianchi

回答

2

你需要的图片框形式将其添加到窗体控件集合关联。拨打FindForm()只会返回当前分配的表单;在你的情况下,它将返回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();  
} 
0

您必须将图片框的形式画出来:

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

您必须添加图片框。例如,如果在PictureBox是在面板:

panel.Controls.Add(); 

,如果它是你只要把Controls.Add();

希望它可以帮助形式。