简体   繁体   中英

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
  • GET the first 3 letters of HELLO: HEL (using substring)
  • Textbox 3 will get HEL so the input value is : HEL
  • Now is it possible to get the textbox 2 value without overwriting the first one? expected output: 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 .

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.

sure it is possible. Just take care that the textbox has some string longer than 3 characters

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

;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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