C# 将字符串转换为文本框名称

发布于 2025-01-07 20:11:50 字数 387 浏览 3 评论 0原文

可能的重复:
C# 通过名称获取控制

我现在一直在寻找答案有一段时间了,想知道是否有人可以帮助我。

我想将 String 类型转换为 TextBox 类型,如下所示。

String text = "textBox" + number;

TextBox tb = text;

我想这样做,这样我就能收到号码,然后知道要写入哪个文本框。

任何帮助将不胜感激! :)

Possible Duplicate:
C# Get control by name

I have been looking around for an answer for this now for a while and was wondering if someone would help me please.

I would like to convert a type String into type TextBox so say below.

String text = "textBox" + number;

TextBox tb = text;

I would like to do this so I receive the number and then I know which textBox to write to.

Any help would be most appreciated! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

洛阳烟雨空心柳 2025-01-14 20:11:50

好吧,您可以使用反射来获取变量,但我认为您真正想要的是找到实际的控制。

使用 FindControl 方法获取对实际文本框的引用:

TextBox tb = FindControl("textBox" + number.ToString()) as TextBox;

Well, you could get the variable using reflection, but what I think that you really want to is to just find the actual control.

Use the FindControl method to get a reference to the actual textbox:

TextBox tb = FindControl("textBox" + number.ToString()) as TextBox;
嗼ふ静 2025-01-14 20:11:50
String text = "textBox" + number;
TextBox tv = (TextBox)FindControl(text);
String text = "textBox" + number;
TextBox tv = (TextBox)FindControl(text);
梦中楼上月下 2025-01-14 20:11:50

您有一系列文本框吗?
那是Windows窗体/Web吗?

基本上,您可以找到遍历容器的 Controls 集合并找到具有 ID 的元素。

请注意,我的答案相当广泛,并非在所有情况下都准确。如果您提供更多详细信息,我将进一步帮助您。

Do you have an array of text boxes?
Is that windows form / web?

Basically what you can to is find to go through the Controls collection of the container and find an element with the the ID.

Note that my answer is pretty wide and not accurate on all cases. If you'll provide more details I'll help you further.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文