C#从另一个类添加控件到窗体 [英] C# Adding controls to a form from another class

查看:155
本文介绍了C#从另一个类添加控件到窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我每次创建播放器的新实例我想要做下面的代码

Every time I create a new instance of Player I want to do the following code

private void button1_Click(object sender, EventArgs e)
{
   Player Player1 = new Player();
}

Player class
{
    public Player()
    {
        Form1.AddControls(someControl)
    }
}

我似乎无法做任何与 Form1中例如: textbox1.text =测试。我想这是一个作用域的问题,但我不能在互联网上找到答案。有谁知道我怎么能访问+控件添加到我的 Form1中通过类?

I can't seem to do anything to do with form1 e.g. textbox1.text = "Test". I assume this is a scope issue but I can't find an answer on the internet. Does anyone know how I can access + add controls to my form1 through a class?

感谢您的时间

推荐答案

这不是完全清楚你想要做什么。这听起来像你想从Player类控件添加到您是从这样的调用形式:

It's not entirely clear what you're trying to do. It sounds like you want to add controls from the Player class into the form that you're calling from like this:

public class Form1 : Form
{
    public void SomeMethod()
    {
        Player player1 = new Player(this);
    }
}

public class Player()
{
    public Player(Form form)
    {
        Textbox tb = new Textbox();
        form.Controls.Add(tb);
    }
}

这篇关于C#从另一个类添加控件到窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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