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