简体   繁体   English

PictureBox不会出现C#

[英]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 PictureBox won't appear when I click the list item. 我正在尝试使用NOT类创建一个PictureBox ,问题是当我在form1内调用create方法时,它不会出现,并且当我单击列表项时, PictureBox也不会出现。 The problem is (I think) that it doesn't know that it is in form1 even though I use the FindForm() method. 问题是(我认为)即使我使用FindForm()方法也不知道它在form1中。 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);

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; 调用FindForm()仅返回当前分配的表单; in your case it will be returning null . 在您的情况下,它将返回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();      
}

You must add the pictureBox. 您必须添加pictureBox。 For example, if the PictureBox is in a panel: 例如,如果PictureBox在面板中:

panel.Controls.Add();

if it is in the form you just put Controls.Add(); 如果采用的形式,则只需放入Controls.Add();

Hope it helps. 希望能帮助到你。

You must place PictureBox to the form for draw it: 您必须将PictureBox放置在窗体上以进行绘制:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM