sách gpt4 ai đã đi

c# - 在动态创建的事件处理程序中访问动态创建的控件(文本框)

In lại 作者:行者123 更新时间:2023-11-30 22:08:14 33 4
mua khóa gpt4 Nike

我正在尝试在 Button 的事件处理程序中访问我在 C# 中动态创建的 TextBox。

     void MainFormLoad(object sender, EventArgs e)
{
this.Width=600;
this.Height=400;

this.FormBorderStyle= FormBorderStyle.FixedDialog;
TextBox t=new TextBox();
this.Controls.Add(t);
t.Location = new Point(60,40);
Label Mylable=new Label();
this.Controls.Add(Mylable);
Mylable.Location=new Point(15,43);
Mylable.Text="string : ";
t.Width=200;
t.Name="MyText";
t.Refresh();
Button Myb=new Button();
Myb.Location=new Point(270,40);
this.Controls.Add(Myb);
Myb.Text="Reverse it!";
Myb.Name="Mybo";
Myb.Click += new EventHandler(this.Myb_Clicked);
this.Refresh();
}

void Myb_Clicked(object sender, EventArgs e) {

// HOW SHOULD I GAIN ACCESS to MyText.Text HERE
MessageBox.Show();

}

1 Câu trả lời

为您的动态 TextBox 指定一个tên:

 TextBox t=new TextBox();
t.Name = "MyTextBox";
this.Controls.Add(t);

然后:

void Myb_Clicked(object sender, EventArgs e) {

string text = this.Controls["MyTextBox"].Text;

}

关于c# - 在动态创建的事件处理程序中访问动态创建的控件(文本框),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22439955/

33 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com