在运行时使用新类更改按钮/文本字段

本文关键字:按钮 文本 字段 新类更 运行时 | 更新日期: 2023-09-27 18:26:41

我正在显示一个带有按钮和文本字段(名称:button1,textBox1)的winforms。我想在另一个.cs文件(例如cobra.cs)上快速更改按钮和文本框的文本。在我的Program.cs上,我用这个来绘制winform:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace bombtimer
{
    class Program
    {
        /// <summary>
        /// Der Haupteinstiegspunkt für die Anwendung.
        /// </summary>
        //[STAThread]
        public static Form1 formInstance;
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(formInstance = new Form1());
        }
    }
}

和我的眼镜蛇.cs:

using System;
using CSGSI;
public static class Globals
{
    public static Int32 countdown = 0;
}
namespace GSI_Test
{

    class Program_Cobra
    {
        static void Main_cobra(string[] args)
        {
            ... stuff...
            if (GSIListener.Start(10))
            {
                //Change textbox text to: "OK"
            }
            else
            {
                //Change textbox text to: "NOT OK"
            }
        }
        public static void OnNewGameState(object sender, EventArgs e)
        {
            ...stuff...
        }
    }
}

如何更改按钮和文本框?:S

在运行时使用新类更改按钮/文本字段

我不确定这是否是最好的解决方案,但您可以在winform中使用计时器,并从cobra类中的静态字符串变量设置计时器内的按钮文本。(对不起我的英语)