-1

My application has a textbox and a label on different forms. I want to copy textbox text to the label.

I tried the following code:

//creating the variable
string vInput = textbox1.Text;

//set label text on the other form
label1.Text = vInput

How do I solve the problem?

Emma
  • 27,428
  • 11
  • 44
  • 69
  • 1
    How are the two forms related? Are you sure that it's actually 2 *forms*? Your code looks like you're trying to access 2 *controls* on 1 form. Can you clarify? – MikeH Aug 01 '19 at 17:39
  • 3
    We don't know what “it just said that vInput didn't work” means. If you're getting an error message you need to [add the message to your question](http://idownvotedbecau.se/itsnotworking/). – Dour High Arch Aug 01 '19 at 17:40

1 Answers1

0

You have a couple of options, if I'm understanding you correctly. To access a variable from another class, you either need to pass it as a parameter or make it a class property. Broots Waymb's duplicate question link explains class properties.

To pass as a parameter would look something like this.

public class Form2 {
   //Form2 class constructor
   public Form2(string vInputFromForm1) {
      //your code using passed in string
   }
}

public class Form1 {
   //...
   public void someMethod() {
      string vInput = textbox1.Text;
      Form2 form2 = new Form2(vInput); 
   }
}

If neither of these options solves your problem, you may want to edit your question to add more information. As Dour High Arch mentioned, providing error messages can go a long way towards understanding the problem in the first place.

NerdyGinger
  • 406
  • 7
  • 13
  • this is the error message that i got : Severity Code Description Project File Line Suppression State Error CS0103 The name 'vInput' does not exist in the current context – Agent20054 Aug 02 '19 at 16:01