在C#中使用动态创建的控件

在C#中使用动态创建的控件

本文介绍了在C#中使用动态创建的控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个应用程序,用户将输入的等级,程序将输出的加权平均值。在负载,它会要求类别的分配数量。该方案将随后动态为用户输入信息创建文本框。问题是,我无法弄清楚如何阅读后,我创建文本框的便被输入的文字。这里是我的代码:

I am creating an application where a user will input grades and the program will output the weighted average. On load, it will ask for the number of categories for the assignments. The program will then dynamically create textboxes for the user to input information. The problem is that I can not figure out how to read the text that is inputed after I create the textboxes. Here is my code:

            TextBox txtbx = new TextBox();
            txtbx.Text = "";
            txtbx.Name = "txtbx1";
            txtbx.Location = new Point(10, 10);
            txtbx.Height = 20;
            txtbx.Width = 50;
            Controls.Add(txtbx);



我怎样才能改变这种代码,以便我能找到的时候,用户提交的框中当前文本?

How can I change this code so I can find the current text in the box when the user submits?

推荐答案

如果您是动态生成控件,接着显然,你不能有一个字段为每一个。但是,如果你试图访问控件集合名为控件,可以通过名称索引。使用指定的名称添加文本框后,您可以简单地做:

If you are dynamically generating controls then obviously you won't be able to have a field for each one. But if you are trying to access the Controls collection for a named control, the ControlCollection can be indexed by name. After adding the text box with the specified name, you can simply do:

TextBox txtbx = (TextBox)Controls["txtbx1"];

这篇关于在C#中使用动态创建的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 16:26