来自另一个类的C#controls.add [英] C# controls.add from another class

查看:156
本文介绍了来自另一个类的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?

推荐答案

如果您对其他类"有引用,并且PictureBox对象是公共的(或者可以公共检索),则可以使用.

If you have a reference to your "other class", and the PictureBox object is public (or otherwise publicl retrievable), you can use Controls.Add.


PictureBox的保护级别是否公开?其他类只能看到您允许它们访问的对象.

[添加]

这样吧.

Is the protection level of the PictureBox public? Other classes can only see objects that you allow them to.

[ADDED]

How about this.

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


按如下所示修改您的代码

modify your code as given below

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆