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

时间:2015-02-16 05:57:40

标签: 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中访问)?

编辑:在Philip Stuyck的评论之后: 这是我的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代码我单击一个按钮,当我单击按钮时,将运行一些进程,这将进一步打印文本框中的文本。 (我觉得我的文本消息并没有被传递给我的消息初衷化。)

4 个答案:

答案 0 :(得分:1)

确保textBox2是公共的。(可以在设计器中完成) 在创建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将使您的应用程序进入消息循环,使应用程序接收窗口消息。

答案 1 :(得分:0)

您可以这样做的一种方法是在Form1中访问一个方法,将textBox名称设置为您想要的任何名称。例如:

public static void SetText (string message)
{
     TextBox2.Text = message;
}

然后,您将从Program.cs类中调用它:

Form1.SetText("Whatever text you want the text on the Form1 - TextBox2 to say");

这是穷人改变文字的方式。有一个更详细的解释on this other post if you need more details.

答案 2 :(得分:0)

据我所知,你想在textBox2.txt文本框上打印最终结果,对吧?所以你想在Programss.cs上执行一些方法,一旦执行完毕,你想在textBox2.txt上显示“Task finished”。

如果我没错,你需要做的是从Programss.cs类的方法返回一个值。您必须从Form1.Designer.cs类中调用此方法。根据返回值,您可以随心所欲。

希望这会有所帮助。

答案 3 :(得分:0)

您正在创建两个单独的表单,在第二个表单上设置文本,并希望它出现在第一个表单上。

这样做:

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