c# - 如何访问 Form.cs 类之外的文本按钮(我的意思是在 Program.cs 中)

标签 c# winforms

我在 C# windows 中创建了一个表单。我的表单包含一个按钮和一个文本框,它的名称是 textbox2 ,它在 Form1.Designer.cs 中包含如下代码:

this.textBox2.Location = new System.Drawing.Point(683, 14);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(100, 20);
this.textBox2.TabIndex = 22;

现在我创建了另一个名为 Programss.cs 的类,它将执行一些任务,当任务完成时,我想在表单上打印如下内容:

textBox2.txt= "Task finished"; //it should print on the textbox of my Form1.cs

如何在Programss.cs和我的其他类中访问这个textBox2(当前只能在Form1.Designer.cs中访问)?

编辑:菲利普·斯图克发表评论后: 这是我的 Form1.cs 代码:

namespace S
{
    public partial class Form1 : Form
    {


        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {


        }
         //please see here
        public string TextMessage
        {
            get { return textBox3.Text; }
            set { textBox3.Text = value; }
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            btnStart.Enabled = false;
          //  StartServer();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {

        }
    }
}

Program.cs 是:

 namespace Senter code here
    {
         class Program 
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
               Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
                /////////////////////////////////////////////////////////////////////////////////////
            //    OtherImportantClasses.Programs prgms = new OtherImportantClasses.Programs();
                Form1 theForm = new Form1();
                theForm.TextMessage = "Task finished";
            }
        }
    }

Form1.Designer.cs 是:

namespace Shekhar
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        public void InitializeComponent()
        {
            this.btnClose = new System.Windows.Forms.Button();
            this.btnStart = new System.Windows.Forms.Button();
            this.txtPort = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.textBox2 = new System.Windows.Forms.TextBox();
            this.textBox3 = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // btnClose
            // 
            this.btnClose.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btnClose.Location = new System.Drawing.Point(313, 11);
            this.btnClose.Name = "btnClose";
            this.btnClose.Size = new System.Drawing.Size(75, 23);
            this.btnClose.TabIndex = 21;
            this.btnClose.Text = "Close";
            this.btnClose.UseVisualStyleBackColor = true;
            this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
            // 
            // btnStart
            // 
            this.btnStart.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btnStart.Location = new System.Drawing.Point(227, 12);
            this.btnStart.Name = "btnStart";
            this.btnStart.Size = new System.Drawing.Size(75, 23);
            this.btnStart.TabIndex = 20;
            this.btnStart.Text = "Start";
            this.btnStart.UseVisualStyleBackColor = true;
            this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
            // 
            // txtPort
            // 
            this.txtPort.Location = new System.Drawing.Point(140, 12);
            this.txtPort.Name = "txtPort";
            this.txtPort.Size = new System.Drawing.Size(79, 20);
            this.txtPort.TabIndex = 19;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 11F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label1.Location = new System.Drawing.Point(78, 12);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(55, 18);
            this.label1.TabIndex = 18;
            this.label1.Text = "Port : ";
            OtherImportantClasses.Programs prog = new OtherImportantClasses.Programs();


            //
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 11F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label2.Location = new System.Drawing.Point(400, 20);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(276, 18);
            this.label2.TabIndex = 18;
            this.label2.Text = "Total Number of device connected :";
            // 
            // textBox2
            // 
            this.textBox2.Location = new System.Drawing.Point(683, 14);
            this.textBox2.Name = "textBox2";
            this.textBox2.Size = new System.Drawing.Size(100, 20);
            this.textBox2.TabIndex = 22;
         //   this.textBox2.Text = "testbox2";
            // 
            // textBox3
            // 
            this.textBox3.Location = new System.Drawing.Point(81, 60);
            this.textBox3.Multiline = true;
            this.textBox3.Name = "textBox3";
            this.textBox3.Size = new System.Drawing.Size(702, 243);
            this.textBox3.TabIndex = 23;


            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(818, 315);
            this.Controls.Add(this.textBox3);
            this.Controls.Add(this.textBox2);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.btnClose);
            this.Controls.Add(this.btnStart);
            this.Controls.Add(this.txtPort);
            this.Controls.Add(this.label1);
            this.Name = "Form1";
            this.Text = "Shekhar\'s GT06";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        /*
        public string TextMessage
        {
            get
            {
                return textBox2.Text;
            }
            set
            {
                textBox2.Text = value;
            }
        }
        */
        #endregion



        private System.Windows.Forms.Button btnClose;
        private System.Windows.Forms.Button btnStart;
        private System.Windows.Forms.TextBox txtPort;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.TextBox textBox2;
        private System.Windows.Forms.TextBox textBox3;
    }
}
Form1.Designer.cs is :

    namespace S`enter code here`
    {
        partial class Form1
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;

            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }

            #region Windows Form Designer generated code

            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            public void InitializeComponent()
            {
                this.btnClose = new System.Windows.Forms.Button();
                this.btnStart = new System.Windows.Forms.Button();
                this.txtPort = new System.Windows.Forms.TextBox();
                this.label1 = new System.Windows.Forms.Label();
                this.label2 = new System.Windows.Forms.Label();
                this.textBox2 = new System.Windows.Forms.TextBox();
                this.textBox3 = new System.Windows.Forms.TextBox();
                this.SuspendLayout();
                // 
                // btnClose
                // 
                this.btnClose.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.btnClose.Location = new System.Drawing.Point(313, 11);
                this.btnClose.Name = "btnClose";
                this.btnClose.Size = new System.Drawing.Size(75, 23);
                this.btnClose.TabIndex = 21;
                this.btnClose.Text = "Close";
                this.btnClose.UseVisualStyleBackColor = true;
                this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
                // 
                // btnStart
                // 
                this.btnStart.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.btnStart.Location = new System.Drawing.Point(227, 12);
                this.btnStart.Name = "btnStart";
                this.btnStart.Size = new System.Drawing.Size(75, 23);
                this.btnStart.TabIndex = 20;
                this.btnStart.Text = "Start";
                this.btnStart.UseVisualStyleBackColor = true;
                this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
                // 
                // txtPort
                // 
                this.txtPort.Location = new System.Drawing.Point(140, 12);
                this.txtPort.Name = "txtPort";
                this.txtPort.Size = new System.Drawing.Size(79, 20);
                this.txtPort.TabIndex = 19;
                // 
                // label1
                // 
                this.label1.AutoSize = true;
                this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 11F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.label1.Location = new System.Drawing.Point(78, 12);
                this.label1.Name = "label1";
                this.label1.Size = new System.Drawing.Size(55, 18);
                this.label1.TabIndex = 18;
                this.label1.Text = "Port : ";
                OtherImportantClasses.Programs prog = new OtherImportantClasses.Programs();


                //
                // label2
                // 
                this.label2.AutoSize = true;
                this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 11F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.label2.Location = new System.Drawing.Point(400, 20);
                this.label2.Name = "label2";
                this.label2.Size = new System.Drawing.Size(276, 18);
                this.label2.TabIndex = 18;
                this.label2.Text = "Total Number of device connected :";
                // 
                // textBox2
                // 
                this.textBox2.Location = new System.Drawing.Point(683, 14);
                this.textBox2.Name = "textBox2";
                this.textBox2.Size = new System.Drawing.Size(100, 20);
                this.textBox2.TabIndex = 22;
             //   this.textBox2.Text = "testbox2";
                // 
                // textBox3
                // 
                this.textBox3.Location = new System.Drawing.Point(81, 60);
                this.textBox3.Multiline = true;
                this.textBox3.Name = "textBox3";
                this.textBox3.Size = new System.Drawing.Size(702, 243);
                this.textBox3.TabIndex = 23;


                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(818, 315);
                this.Controls.Add(this.textBox3);
                this.Controls.Add(this.textBox2);
                this.Controls.Add(this.label2);
                this.Controls.Add(this.btnClose);
                this.Controls.Add(this.btnStart);
                this.Controls.Add(this.txtPort);
                this.Controls.Add(this.label1);
                this.Name = "Form1";
                this.Text = "Shekhar\'s GT06";
                this.Load += new System.EventHandler(this.Form1_Load);
                this.ResumeLayout(false);
                this.PerformLayout();

            }

            /*
            public string TextMessage
            {
                get
                {
                    return textBox2.Text;
                }
                set
                {
                    textBox2.Text = value;
                }
            }
            */
            #endregion



            private System.Windows.Forms.Button btnClose;
            private System.Windows.Forms.Button btnStart;
            private System.Windows.Forms.TextBox txtPort;
            private System.Windows.Forms.Label label1;
            private System.Windows.Forms.Label label2;
            private System.Windows.Forms.TextBox textBox2;
            private System.Windows.Forms.TextBox textBox3;
        }
    }

它不会给出任何错误,但不会在 textBox3 中打印任何内容

注意:请注意,首先我运行 winform 代码,我单击一个按钮,当我单击该按钮时,将运行一些进程,该进程将进一步打印文本框中的文本。 (我觉得我的短信没有被传递给它的消息初始化)。

最佳答案

确保textBox2是公共(public)的。(可以在设计器中完成) 创建 Form1 时保留对 Form1 的引用,并通过该引用访问 textBox2。

Form1 theForm = new Form1();
... //do stuff
theForm.textBox2.Text = "Task finished";

这不是最优雅的方法,但它应该可行。 另一种选择是这样的:

public partial class Form1{
   public string TextMessage{
        get{
            return textBox2.Text;
        }
        set{
            textBox2.Text = value;
        }
   }
}

以及program.cs中

Form1 theForm = new Form1();
... //do stuff
theForm.TextMessage = "Task finished";

你的代码将无法工作,因为

Form1 theForm = new Form1();
theForm.TextMessage = "Task finished"; 

永远不会被执行。

Application.Run(new Form1());

仅当表单关闭时返回,然后应用程序将结束。

以下内容将起作用:

Form1 theForm = new Form1();
theForm.TextMessage = "Task finished";
Application.Run(theForm);

但我感觉你不太了解这个框架。 Application.run,将使您的应用程序进入消息循环,从而使应用程序接收窗口消息。

关于c# - 如何访问 Form.cs 类之外的文本按钮(我的意思是在 Program.cs 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28535321/

相关文章:

c# - Xamarin studio 中没有(IOS Bundle Signing)选项

c# - 如何使用 SOAP 操作和 HttpClient 向经过授权的网络摄像头发送 ONVIF 请求

c# - 运行时类型转换

.net - 在缩放时如何避免平移超过嵌套在 Panel 中的 PictureBox 控件的边缘?

c# - vs2008/vs2010 在 TextBox 中是否有插入符位置更改事件?

c# - 如何为自包含控制台应用程序 Visual Studio C# 创建 .msi 安装程序

c# - 如何在 Visual Studio 2017 ASP.NET Core Web 应用程序中集成 Bootstrap 4?

c# - EF 代码优先 : separate creation of objects in db with TFT inheritance

c# - 数据绑定(bind)到对象 - 如何取消数据源更改

.net - Arial Unicode MS是WinForms UI的正确字体吗?