本文介绍了来自另一个类的C#controls.add的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!

我创建了一个图片框,但是在另一个类中,而不是在主类中,所以我不能使用Controls.Add,因此在窗体上看不到它.

有任何想法吗?

Hello!

I made a picturebox, but in another class, not in the main, so I can''t use Controls.Add, therefore I don''t see it on my form.

Any ideas?

推荐答案


public class players
{
    public PictureBox pb;

    public players()
    {
        this.pb = new PictureBox();
        this.pb.Image = Image.FromFile(@"D:\asd.jpg");
    }
}
public partial class Form1 : Form
{
    public Form1()
    {
            InitializeComponent();
            players p1 = new players();
            //Add control here. Controls.Add(p1.pb)
    }
}


public class players  
{
public PictureBox pb;
    public players()
    {
         pb = new PictureBox();
        pb.Image = Image.FromFile(@"D:\asd.jpg");
        //There I would like to add the control
    }
}
public partial class Form1 : Form
{
players  p1  // creating object of class because of this you can access any where inside this class(Form1)
    public Form1()
    {
            InitializeComponent();
            p1 = new players();
    }
    private void Form1_Load(object sender, EventArgs e)
       {
       p1.pb.Controls.Add(yourcontrol);  // Control.Add is accessible
       
      }



它将使您能够访问所需的内容.



it will enable you to access what do you want.


这篇关于来自另一个类的C#controls.add的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 21:14