简体   繁体   English

当我从多个文本框中插入单个值时,如何将值返回到多个文本框中

[英]How to get values back to multiple text boxes as i inserted from multiple text boxes to single value

I'm developing an application which gets the Mobile no. 我正在开发一个获取移动电话号码的应用程序。 through a multiple text boxes, 1 digit per text box, I have inserted values successfully by cancatination, now the problem is that how I can get these values back to multiple text boxes 1 digit per text box? 通过多个文本框(每个文本框1位数),我已经通过归类成功插入了值,现在的问题是如何将这些值返回到每个文本框1位数的多个文本框?

    private[enter image description here][1] void button2_C[enter image description here][1]lick(object sender, EventArgs e)
    {
            string btn2 = ""+textBox1.Text+ textBox2.Text + textBox3.Text + textBox4.Text + textBox5.Text + textBox6.Text + textBox7.Text + textBox8.Text + textBox9.Text + textBox10.Text + textBox11.Text + "";
        label1.Text = btn2;

    }

    private void button1_Click(object sender, EventArgs e)
    {
       //??
    }

It looks a bit like this: 它看起来像这样:

private void button1_Click(object sender, EventArgs e)
{
   textBox1.Text = yourConcatString[0].ToString();
   textBox2.Text = yourConcatString[1].ToString();
   //... and so on
   textBox11.Text = yourConcatString[10].ToString();
}

Why does it work? 为什么行得通?

Well, a string can be thought of as an array of char 好吧,字符串可以认为是一个char数组

If you index a string like this: myString[7] you'll get the 8th (remember- indexing is 0 based) char 如果索引这样的字符串: myString[7]您将获得第8个(记住,索引基于0)

string myString = "abcdefghijklmno";
char theEighth = myString[7]; //variable theEighth now contains 'h'

You then only need to turn the char into a string in order to assign it to a textbox Text property. 然后,您只需要将char转换为字符串即可将其分配给文本框Text属性。 .ToString() on a char will turn the char into a string: 字符上的.ToString()会将字符转换为字符串:

char c = 'a';
string s = c.ToString(); //s now contains "a"

The only thing you have to remember in your code is that you concatenate into btn2 but that variable is only existing inside the scope of the button2_Click method. 您在代码中唯一需要记住的是,您将其串联到btn2但是该变量仅存在于button2_Click方法的范围内。 You cannot access it in button1_Click . 您无法在button1_Click访问它。 You'll have to put a class-wide variable if you want to access it in both methods 如果要在两种方法中都访问它,则必须放置一个类范围的变量

Please name your buttons/textboxes before you double click on them to start adding events etc, otherwise your code will fill up with meaningless controlnames like comboBox27 - "hmm, is that the city, or the country combo? hmm..." 请先命名按钮/文本框,然后再双击它们以开始添加事件等,否则您的代码将填充无意义的控件名,如comboBox27 “嗯,是城市,还是国家(地区)组合?嗯...”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM