c# - 如何从 Program.cs main 方法更改 WinForms 标签的内容?

标签 c# winforms

因此,我的主要方法中有一个启动例程,用于检查以确保 .txt 文件中有内容。

 FileInfo fInfo = new FileInfo(DataDir);
 if (fInfo.Length < 64)
 {
     //Do stuff here if file is not long enough
 }

我希望能够在我的 WinForms 应用程序上制作一个标签,显示一些文本,并且我想将一些控件灰显,但我似乎找不到引用所述标签/控件或我的应用程序中的任何对象的方法。就此而言的形式。我是一个初学者,我正在努力解决这个问题。

最佳答案

如果您希望能够通过 Main() 方法使用 Form 对象,则需要传入一个对象,而不是使用 新的关键字。

这就是您通常看到的内容(Visual Studio 生成此代码)。

static class Program
{
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}

如何使用表单对象。

static class Program
{
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Form1 myForm = new Form1(); //create the object here
        //you can work with the form here
        Application.Run(myForm); //pass in the form
    }
}

关于c# - 如何从 Program.cs main 方法更改 WinForms 标签的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30927514/

相关文章:

c# - 我应该如何缓冲绘制的矩形以提高性能(C#/.NET/WinForms/GDI+)

C# Delegate -- 如何首先将具有不同参数的多个函数(相同类型)绑定(bind)到一个委托(delegate),然后再触发/调用它们?

winforms - 如何在 VS2012 中创建 C++/CLI Winforms 应用程序?

c# - 滑动效果不流畅

c# - 从另一个表单的 datagridview 获取值

c# - 如何模仿 Windows 7 用户界面?

c# - Unity 和 Unity.WebApi 包有什么区别

c# - 什么是Windows中的COM

c# - 比较 MySql 中的 EntityFrameworkCore 字符串列无法生成正确的 SQL

c# - 如何在 <img> src 标签中连接两个字符串?