如何从运行时创建的文本框中获取文本 [英] How to get Text from Textbox created during runtime

查看:28
本文介绍了如何从运行时创建的文本框中获取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have created WinForm App in which user can set how many textboxes he want (Range 1-99) I am using this code to create Textboxes during runtime

  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

Suppose user create 2 textboxes and then enter data in each textbox and click calculate button Now i want to get the textboxes data and divide it by 100

看图片我要txtbox1和txtbox2数据

See The Picture I want txtbox1 and txtbox2 data

编辑 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();
        }

    }
}

推荐答案

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();

这篇关于如何从运行时创建的文本框中获取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆