简体   繁体   English

C# 是否可以在不覆盖文本框的第一个输入值的情况下在一个文本框中获取两个输入文本框值?

[英]C# Is it possible to get the two input textbox values in one textbox without overwriting the first input value of textbox?

I've been searching this for a while but i didn't get any information about this, so i don't know if it is possible or not.我已经搜索了一段时间,但我没有得到任何有关此的信息,所以我不知道是否可能。 Here's what i'm trying to achieve.这就是我想要实现的目标。 Any suggestions there ?有什么建议吗? Thank you谢谢

Example:例子:

  • Textbox 1 input value is: HELLO and Textbox 2 input value is: WORLD文本框 1 输入值为:HELLO,文本框 2 输入值为:WORLD
  • GET the first 3 letters of HELLO: HEL (using substring)获取 HELLO 的前 3 个字母:HEL(使用子字符串)
  • Textbox 3 will get HEL so the input value is : HEL文本框 3 将获得 HEL,因此输入值为:HEL
  • Now is it possible to get the textbox 2 value without overwriting the first one?现在是否可以在不覆盖第一个值的情况下获取文本框 2 的值? expected output: HELWORLD预期输出:HELWORLD

当然,这是可能的:

Textbox3.Text = Textbox1.Text.Substring(0,3)+Textbox2.Text

Another way to do it if there's a possibility that textBox1.Text is less than 3 characters is to use the Take method to take up to 3 characters from the string (treating is as an array of characters), then use Concat to put those characters back into a string, and finally concatenate it with textBox2.Text .如果textBox1.Text可能少于 3 个字符,另一种方法是使用Take方法从字符串中提取最多 3 个字符(处理为字符数组),然后使用Concat放置这些字符回到一个字符串,最后将它与textBox2.Text连接起来。

textBox3.Text = string.Concat(textBox1.Text.Take(3)) + textBox2.Text;

It's a little "wordy", but will prevent exceptions from being thrown if textBox1 contains fewer than 3 characters.这有点“罗嗦”,但如果textBox1包含少于 3 个字符,则会防止抛出异常。

sure it is possible.肯定有可能。 Just take care that the textbox has some string longer than 3 characters请注意文本框有一些长度超过 3 个字符的字符串

string processedText;
            int NoOfChars = 3;
            if (textBox1.Text.Length >= NoOfChars)
                processedText = textBox1.Text.Substring(0, NoOfChars);
            else
                processedText = textBox1.Text;

            textBox3.Text = processedText + textBox2.Text

; ;

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

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