Visual Studio中动态创建的textBox的数组/字符串 - C#

时间:2018-05-02 21:04:22

标签: c# arrays visual-studio dynamic textbox

我动态创建了许多textBoxes。用户将在这些文本框中写入输入,然后按下按钮(“保存”)。

我想从数组(或字符串)中保存那些textBox的信息。是否可以实现以下内容:for(i = 0; i < the_number_of_textBoxes; i++) MY_ARRAY_or_MY_STRING += textBox[i].Text; //I know this isn't correct

我确信应该有类似于我的“思维方式”,但我找不到它。

如果我总是拥有相同数量的texbox,让我们说2,我可以像这样实现它:MY_ARRAY_or_MY_STRING = textBox1.Text + textBox2.Text;,但我的textBoxes是动态生成的。

这是我使用的代码(虽然我不认为它是有需要的),我的程序有2个表单(Form1被标记为frm1) - 它与我的问题无关,但也许它有帮助, IDK的。如果你需要更多关于这个程序的信息,请不要问,我不会在这里写,因为它可能是无用的信息。

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;
//Add MySql Library
using MySql.Data.MySqlClient;

namespace Proiect
{
    public partial class Form2 : Form
    {
        public Form1 frm1;
        public int numar_textBox = 5;
        public int numar_eticheta = 5;
        public int numar_groupBox = 0;
        public int LastTextBoxLeft = 15;
        public int LastEtichetaLeft = 15;
        public int LastGroupBoxLeft = 3;


        public Form2()
        {
            InitializeComponent();
            frm1 = new Form1();
        }



        private void Form2_Load(object sender, EventArgs e)
        {
            int i;

            for (i = 0; i < frm1.get_nr_coloane(); i++)
                AddNewGroupBox();

        }

        private void Form2_Click(object sender, EventArgs e)
        {
            Deselect.Focus(); //Deseleceaza orice element, mutand focusul pe un label fara text
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
        /*
        public int verifica_spatii(TextBox New_text_box)
        {
            int i, k=0;

            if (New_text_box.Text == "") return 1;
            else
            {
                for (i = 0; i < New_text_box.TextLength; i++) 
                    if (New_text_box.Text[i] != ' ')
                    {
                        return 1;
                    }
                return 0; // are numai spatii
            }
        }
        */
        public void On_click()
        {

        }

        public event EventHandler Click_event;

        public System.Windows.Forms.GroupBox AddNewGroupBox()
        {
            EventHandler handler = Click_event;
            System.Windows.Forms.GroupBox grp = new System.Windows.Forms.GroupBox();
            this.Controls.Add(grp);
            grp.Width = 133;
            grp.Height = 50;
            grp.Top = 10;
            grp.Left = LastGroupBoxLeft;
            LastGroupBoxLeft += grp.Width;
            grp.Text = frm1.get_nume_coloane()[numar_groupBox];
            numar_groupBox += 1;
            TextBox txt = new TextBox();
            txt.Top = 20;
            txt.Left = 10;
            numar_textBox = numar_textBox + 1;
            grp.Controls.Add(txt);
            txt.Leave += textBoxGeneral_LostFocus;
            /////New_text_box.Click += handler(this,);

            return grp;
        }

        private void textBoxGeneral_LostFocus(object sender, EventArgs e)
        {
            //ToolStripItem item = (ToolStripItem)sender;
           // MessageBox.Show(item.Text);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string valorile_de_adaugat="";
            int i;

            for(i=0;i<numar_groupBox-1;i++)
                valorile_de_adaugat+=textBox[i]


            //MySqlCommand Salveaza_in_baza_de_date = new MySqlCommand("INSERT INTO " + frm1.get_nume_tabel_selectat() + " ("+ frm1.get_unirea_coloanelor + ") VALUES(22056, 15, 2000, 2004));
        }
        /*
Textbox myTxtbx = new Textbox();
myTxtbx.Text = "Enter text here...";

myTxtbx.GotFocus += GotFocus.EventHandle(RemoveText);
myTxtbx.LostFocus += LostFocus.EventHandle(AddText);

public RemoveText(object sender, EventArgs e)
{
myTxtbx.Text = "";
}

public AddText(object sender, EventArgs e)
{
if (String.IsNullOrWhiteSpace(myTxtbx.Text))
myTxtbx.Text = "Enter text here...";
}
*/
    }
} 

感谢您的时间,

维拉德

0 个答案:

没有答案