Button3을 클릭하면 새로운 TextBox를 만드는 프로그램을 작성하고 싶습니다. 어떤 이유로 C#이 txtRun을 인식하지 못합니다. txtRun이라는 이름이 현재 컨텍스트에 존재하지 않는다고합니다. 여기 내 코드 :버튼을 클릭 한 후 새 텍스트 상자를 만드는 방법은 무엇입니까?
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;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text += "a";
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text += "b";
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
txtRun = new TextBox();
txtRun.Name = "txtDynamic";
txtRun.Location = new System.Drawing.Point(20, 18);
txtRun.Size = new System.Drawing.Size(200, 25);
// Add the textbox control to the form's control collection
this.Controls.Add(txtRun);
}
}
}
}
}
또한 각 새 TextBox에 ** Location() **을 부여해야합니다. 그렇지 않으면 button3을 클릭 할 때마다 새 TextBox가 이전 텍스트 상자 위에 놓이게되고 큰 텍스트 스택. –
FlowLayoutPanel 또는 TableLayoutPanel과 같은 것을 사용하여 다른 위치를 할당하는 것이 좋습니다. 매우 귀찮은 작업이 될 수 있고 특히 그리드 형식과 같은 그리드에서 원하는 경우 시간 낭비가 될 수 있습니다. – CodeCamper