Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
I'm a newbie at this so bare with me. Trying this in VS 2k8 WPF
 
I'm trying to copy the user's input string from one text box to another. Below works fine when I do this:
       private void buttonChange_Click(object sender, RoutedEventArgs e)
        {
    
 
            string userInput = boxTop.Text;
            boxBottom.Text = userInput;
 
            
        }
 
Below doesn't work. I'm missing something very basic and would like some help understanding this.
 
      private void buttonChange_Click(object sender, RoutedEventArgs e)
        {
 
            string userInput = boxTop.Text;
            string userOutput = boxBottom.Text;
            userOutput = userInput;
      
 
            
        }
Posted 11-Jul-13 10:34am
PR3542
Comments
ThePhantomUpvoter at 11-Jul-13 15:41pm
   
In the second snippet you are setting two string variables equal to each other, not setting anything in textbox text.
ThePhantomUpvoter at 11-Jul-13 16:02pm
   
An easier way to do this is to simply
boxBottom.Text = boxTop.Text;
PR3 at 11-Jul-13 16:06pm
   
I originally was trying to do an "If" statement and basically didn't get no error, warning, etc.
 
This was within the buttonChange_Click that didn't work.
string userOutput = boxBottom.Text;
if (boxTop.Text == "42")
{
userOutput = "Good.";
}
else
{
userOutput = "Bad.";
}
ThePhantomUpvoter at 11-Jul-13 16:13pm
   
Yes and all you are doing is setting userOutput to be some value. You are not setting the textbox text to anything. You would need to boxBottom.Text = userOutput at some point.
PR3 at 11-Jul-13 16:26pm
   
That was it. Thank you very much. Something very simple I was missing.
MuhammadUsman1 at 11-Jul-13 16:05pm
   
Your didn't set value to any text box.
if you want to save values in String Variables and also want to exchange values.
then check my solution.
PR3 at 11-Jul-13 16:28pm
   
Thanks.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

 private void buttonChange_Click(object sender, RoutedEventArgs e)
        {
 
            string userInput = boxTop.Text;
            string userOutput = boxBottom.Text;
            // Values saved in variables know you can change as you have done in first step.
            boxTop.Text = userOutput;
            boxBottom.Text = userInput;    
            
        }
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web01 | 2.8.1411022.1 | Last Updated 11 Jul 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100