【问题标题】:How to get Text from Textbox created during runtime如何从运行时创建的文本框中获取文本
【发布时间】:2014-01-07 03:32:11
【问题描述】:

我创建了 WinForm 应用程序,用户可以在其中设置他想要多少个文本框(范围 1-99) 我在运行时使用此代码创建文本框

  for (int i = 0; i < Calculation.Num; i++)
   {
       TextBox txtRun = new TextBox();
       txtRun.Name = "txtBox" + i;
       txtRun.Location = new System.Drawing.Point(35, 50 + (20 * i) * 2);
       txtRun.Size = new System.Drawing.Size(75, 25);
       this.Controls.Add(txtRun);                
   }

假设用户创建了 2 个文本框,然后在每个文本框中输入数据并单击计算按钮 现在我想获取文本框数据并将其除以 100

查看图片我要txtbox1和txtbox2数据

编辑 3:

这是完整的代码

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 GPA_Calculatior__New_
{
    public partial class Form1 : Form
    {

        int j = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //Label + Marks Obtained Textbox
            for (int i = 0; i < Calculation.Num; i++)
            {
                Label lblCount = new Label();
                lblCount.Name = "lblCount" + i;
                lblCount.Location = new System.Drawing.Point(5, 55 + (20 * i) * 2);
                lblCount.Size = new System.Drawing.Size(20, 30);
                lblCount.Text = (i + 1).ToString();
                this.Controls.Add(lblCount);

                TextBox txtRun = new TextBox();
                txtRun.Name = "txtBox" + i;
                txtRun.Location = new System.Drawing.Point(35, 50 + (20 * i) * 2);
                txtRun.Size = new System.Drawing.Size(75, 25);
                this.Controls.Add(txtRun);   

            }

            //Creating Textbox which is for total marks
            for (j = 0; j < Calculation.Num; j++)
            {
                TextBox txtRun = new TextBox();
                txtRun.Name = "TotaltxtBox" + j;
                txtRun.Location = new System.Drawing.Point(160, 50 + (20 * j) * 2);
                txtRun.Size = new System.Drawing.Size(50, 25);
                txtRun.Text = "100";
                txtRun.Enabled = false;
                this.Controls.Add(txtRun);
            }
            // Creating 2 Buttons (Calculate and Back)
            for (int k = 0; k < 2; k++)
            {
                Button Btn = new Button();
                Btn.Name = "btn" + k;
                Btn.Location = new System.Drawing.Point(20 + (k *110), 60 + (20 * j) * 2);
                Btn.Size = new System.Drawing.Size(90, 30);

                if (k == 0)               
                    Btn.Text = "Back";

                else
                    Btn.Text = "Calculate";

                Btn.Click += button_Click;

                this.Controls.Add(Btn);
            }

            //Just for Giving free space in last

            Label lbl = new Label();
            lbl.Name = "lbl" + j;
            lbl.Location = new System.Drawing.Point(30, 90 + (20 * j) * 2);
            lbl.Size = new System.Drawing.Size(90, 30);
            lbl.Text = "";

            this.Controls.Add(lbl);
            //**********************************************
        }

        //Caculate and back button function
        private void button_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            if (btn.Name.Equals("btn1"))
            {
                for (int i = 0; i < Calculation.Num; i++)
                {

                }
            }
            else
            {
                GPA_Calculator mainForm = new GPA_Calculator();
                mainForm.Show();
                this.Hide();
            } 
        }




        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            for (j = 0; j < 10; j++)
            {

            }
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

    }
}

【问题讨论】:

    标签: c# winforms


    【解决方案1】:
    var sum = this.Controls.OfType<TextBox>()
        .Where(t => char.IsDigit(t.Name.Reverse().Take(1).FirstOrDefault())
            && t.Enabled)
        .Select(t =>
        {
            double i;
            if (!double.TryParse(t.Text, out i)) { return 0d; }
            return i / 100d;
        })
        .Sum();
    

    【讨论】:

    • 抱歉,我对上面的代码一无所知,它有点复杂,有什么简单的方法可以做到这一点,而且我想将每个文本框数据除以 100
    • 恕我直言,他应该只存储对文本框的引用。表单上可能会有更多不相关的数字文本框。
    • 它获取网格中的所有数字文本框,然后尝试从中获取双精度,然后将所有文本框的总和除以 100。Res 存储您的答案@UzairAli
    • 最好建立一个 List 来存储它们,这样它们就可以独立于表单的其余部分
    • 无法从 'System.Collections.Generic.IEnumerable' 转换为 'char' 匹配 'char.IsDigit(char)' 的最佳重载方法有一些无效参数(新)跨度>
    【解决方案2】:

    这可能有效,我构建了一个单独的类来存储控件及其值,然后您可以独立于表单的其余部分计算值。您需要触发计算:

        private List<InfoTextBox> activeTextBoxes = new List<InfoTextBox>();
        public Form1()
        {         
            for (int i = 0; i < Calculation.Num; i++)
            {
                TextBox txtRun = new TextBox();
                txtRun.Name = "txtBox" + i;
                txtRun.Location = new System.Drawing.Point(35, 50 + (20 * i) * 2);
                txtRun.Size = new System.Drawing.Size(75, 25);
                this.Controls.Add(txtRun);
                InfoTextBox iBox = new InfoTextBox();
                iBox.textbox = txtRun;
                activeTextBoxes.Add(iBox);
            }
        }
    
        public class InfoTextBox
    {
        private double _textboxValue;
        public TextBox textbox { get; set; }
        public double TextBoxValue { get { return _textboxValue; } set { _textboxValue = setValue(value); } }
    
        private double setValue(double invalue)
        {
            return invalue / 100;
        }
    }
    

    【讨论】:

    • 你不能为 TextBoxValue 使用自动实现的属性吗?
    • 是的,您可以,如果您将按键屏蔽为数值并将更新发送到 TextBoxValue,这将很有用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-23
    • 2011-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-13
    • 2017-01-12
    相关资源
    最近更新 更多