Print a value received in a method on a Form C # [closed]

-1

Inside the oClick method, I have an integer variable that gets a value converted to integer from a textBox . I have a label that within this method prints this received value correctly, but it is not here that I need this value, I need it printed inside public Form1 ( ) { } , but I'm not Can someone help?

Below is the complete code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;

namespace Teste {

    public partial class Form1 : Form {
       public int x = 0;
        public System.Windows.Forms.Button button;
        public System.Windows.Forms.TextBox textBox;
        public System.Windows.Forms.Label label;

        private void okClick ( object sender, System.EventArgs e ) {
             x = Int32.Parse ( textBox.Text );
            //Aqui dentro do método do botão o valor
            //é imprimido corretamente
           // label.Text = x.ToString ( );
        }

        public Form1 ( ) {
            textBox = new System.Windows.Forms.TextBox ( );
            textBox.Location = new System.Drawing.Point ( 100, 40 );
            textBox.Name = "textBox1";
            textBox.Size = new System.Drawing.Size ( 60, 10 );
            textBox.TabIndex = 0;

            button = new System.Windows.Forms.Button ( );
            button.Size = new System.Drawing.Size ( 40, 25 );
            button.Location = new System.Drawing.Point ( 170, 37 );
            button.Text = "&Ok";
            button.BackColor = Color.Blue;
            button.Click += new EventHandler ( this.okClick );

            label = new System.Windows.Forms.Label ( );
            label.Location = new Point ( 40, 80 );
            label.AutoSize = true;
            label.Font = new Font ( "Times New Roman", 20F,
            System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0 );

            //O que eu quero, é que quando o botão for clicado
            //O valor que que x recebeu do textBox seja imprimido aqui.
            label.Text = x.ToString ( );

            this.Controls.Add ( this.textBox );
            this.Controls.Add ( this.button );
            this.Controls.Add ( this.label );
        }
    }

    static class Program {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main ( ) {
            Application.EnableVisualStyles ( );
            Application.SetCompatibleTextRenderingDefault ( false );
            Application.Run ( new Form1 ( ) );
        }
    }
}
    
asked by anonymous 25.10.2018 / 18:42

1 answer

1

You will not be able to get the value in the constructor by clicking the OK button. Since you said that you would need to start another button by clicking the first one, I gave you an example of what might help you:

public partial class Form2 : Form
    {
        public System.Windows.Forms.Button button;
        public System.Windows.Forms.TextBox textBox;
        public System.Windows.Forms.Label label;

        public System.Windows.Forms.Button button2;
        public System.Windows.Forms.TextBox textBox2;
        public System.Windows.Forms.Label label2;

        private void okClick(object sender, System.EventArgs e)
        {
            label.Text = textBox.Text;

            //Criar uma vez só
            if (button2 == null)
               criaBotao2();
        }

        private void okClick2(object sender, System.EventArgs e)
        {
            label2.Text = textBox2.Text;
        }

        public void criaBotao2()
        {
            button2 = new System.Windows.Forms.Button();
            button2.Size = new System.Drawing.Size(40, 25);
            button2.Location = new System.Drawing.Point(12, 65);
            button2.Text = "OK 2";
            button2.BackColor = Color.Green;
            button2.Click += new EventHandler(this.okClick2);

            textBox2 = new System.Windows.Forms.TextBox();
            textBox2.Location = new System.Drawing.Point(105, 65);
            textBox2.Name = "textBox2";
            textBox2.Size = new System.Drawing.Size(60, 10);

            label2 = new System.Windows.Forms.Label();
            label2.Location = new System.Drawing.Point(256, 65);
            label2.AutoSize = true;
            label2.Font = new Font("Times New Roman", 20F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);

            this.Controls.Add(this.textBox2);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.label2);
        }

        public Form2()
        {
            button = new System.Windows.Forms.Button();
            button.Size = new System.Drawing.Size(40, 25);
            button.Location = new System.Drawing.Point(12, 25);
            button.Text = "OK 1";
            button.BackColor = Color.Blue;
            button.Click += new EventHandler(this.okClick);

            textBox = new System.Windows.Forms.TextBox();
            textBox.Location = new System.Drawing.Point(105, 25);
            textBox.Name = "textBox1";
            textBox.Size = new System.Drawing.Size(60, 10);
            textBox.TabIndex = 0;

            label = new System.Windows.Forms.Label();
            label.Location = new System.Drawing.Point(256, 25);
            label.AutoSize = true;
            label.Font = new Font("Times New Roman", 20F,
            System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);

            this.Controls.Add(this.textBox);
            this.Controls.Add(this.button);
            this.Controls.Add(this.label);
        }
    }

It's just a simple example, you'd have to improve it. Obviously there are other ways, here just an alternative.

    
26.10.2018 / 15:28