xyx0826
xyx0826

Reputation: 60

How to read a textBox's Text in another window

I have a textBox, textBoxQuery, in window QueryWindow.

I need to access textBoxQuery's Text in another window, MainWindow.

I have the following accessor in QueryWindow:

public string QueryString
{
    get { return textBoxQuery.Text; }
    set { textBoxQuery.Text = value; }
}

And I attempt to use it in MainWindow:

cmdLine += QueryString;

However, I am thrown a CS0120 error. "An object reference is required for the nonstatic field, method, or property."

I have also attempted to implement the following method in QueryWindow:

public string queryString()
{
    return textBoxQuery.Text;
}

Then using the following in MainWindow:

cmdLine += QueryWindow.queryString();

But none of the above worked.

I have searched through Google, but none of the solutions I found seemed to work. What is the correct way of accessing a control's properties from another window/class?

Upvotes: 0

Views: 95

Answers (1)

Gry-
Gry-

Reputation: 176

Oh! The assessor is used to access an instance of a class (an object) of type QueryWindow! Basicly, you could create a bunch of query windows (each would be their own instance) by doing this:

QueryWindow myQueryWindow1 = new QueryWindow();
myQueryWindow1.show()
QueryWindow myQueryWindow2 = new QueryWindow();
myQueryWindow2.show()
// Note, the shows are only needed to make instances visible to the user.

For as long as you have the reference to myQueryWindow1 or myQueryWindow2, you can use an acessor to get the state of the instance:

string myString = myQueryWindow1.queryString();

So QueryWindow.queryString() wouldn't work, because there is no way for the program to tell which instance of QueryWindow you want!

Hope this helps!

Upvotes: 1

Related Questions