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 ( ) );
}
}
}