我是编码新手。我无法弄清楚为什么我的文本框不会使用C#在Windows窗体中显示文本。这是我的代码。
public partial class Form1
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
Form1 myTestObject = new Form1();
myTestObject.myTextPanel("hello");
}
public void myTextPanel(string myText)
{
// Windows Forms textBox1
textBox1.Text = myText;
}
}
您正在运行的实例,Form1
然后创建相同的新实例(请记住,两者都是不同的实例)并分配值。您可以尝试以下方法:
Form1 myTestObject = new Form1();
myTestObject.myTextPanel("hello");
Application.Run(myTestObject);
它将使用的相同实例,Form1
因此您可以看到正在显示的文本;您也可以使用构造函数,而对于myTextPanel
方法,构造函数将如下所示:
public void Form1(string myText)
{
// Windows Forms textBox1
textBox1.Text = myText;
// Do something
}
如果是这样,则主要内容将发生如下变化:
Form1 myTestObject = new Form1("hello");
Application.Run(myTestObject);
或像这样:
Application.Run(new Form1("hello"));
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句