Visual C# - Access instance of object created in one class in another

前端 未结 4 815
Happy的楠姐
Happy的楠姐 2020-12-08 16:54

I apologize in advance with what will probably be a fairly easy/quick answer based on scope, but I\'ve looked everywhere and am surprised to not come up with an answer.

4条回答
  •  孤街浪徒
    2020-12-08 17:27

    In your other class, create a method with objects as parameters.

    public class TryMe (TextBox newTextbox) {
    newTextbox.Text = "Hello this is a text."
    //You can also get the value of textbox of another form
    var textString = newTextbox.Text;
    }
    

    And then in your main form, call that method with your objects as parameters. In this case, add textbox1 to your method's parameter.

    Your code in form:

    TryMe(textbox1);
    

提交回复
热议问题