[問題] 點擊button去改變textbox的文字內容
我現在試著寫一個計算機程式
可是我在訓練自己用程式碼設計GUI
因此,所有的button都是用Controls.Add()加上去的
public Form1()
{
InitializeComponent();
}
我把Controls.Add()都寫在這個裡面
其中有:
TextBox result = new TextBox();
Controls.Add(result);
可是在處理按鈕事件的時候
我不能直接讓result.text="1"
他說result是區域變數
也就是說
在
private void btn1_Click(object sender, EventArgs e)
{
}
在這個區塊裡面,沒有result.text這個東西
請問一下,是我加入控制項的方法有錯嗎?
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 220.130.225.223
※ 文章網址: https://www.ptt.cc/bbs/C_Sharp/M.1421636222.A.737.html
推
01/19 11:00, , 1F
01/19 11:00, 1F
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Button btn1 = new Button();
btn1.Text = "1";
btn1.SetBounds(30, 170, 30, 30);
Controls.Add(btn1);
TextBox result = new TextBox();
Controls.Add(result);
btn1.Click += new System.EventHandler(btn1_Click);
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btn1_Click(object sender, EventArgs e)
{
result.text="1";//找不到result
}
}
}
已補上,拜託大家了
※ 編輯: a0960330 (220.130.225.223), 01/19/2015 11:14:50
推
01/19 11:56, , 2F
01/19 11:56, 2F
不然應該寫在哪裏?想把他寫在外面當全域變數好像也不行......大大給個指點吧
※ 編輯: a0960330 (220.130.225.223), 01/19/2015 12:10:37
→
01/19 12:54, , 3F
01/19 12:54, 3F
→
01/19 12:55, , 4F
01/19 12:55, 4F
推
01/19 12:59, , 5F
01/19 12:59, 5F
推
01/19 16:14, , 6F
01/19 16:14, 6F
→
01/19 16:15, , 7F
01/19 16:15, 7F
謝謝大家,問題解決了!
※ 編輯: a0960330 (36.224.71.227), 01/20/2015 21:35:28