Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Make textbox value available to other forms 3

Status
Not open for further replies.

SQLWilts

Programmer
Feb 12, 2001
651
GB
OK, so a complete (ish) newbie to c# here.

I have a form, MainForm. On this I have a text box, txtTable that is declared as

Code:
public TextBox txtTable

When I go into another form (that is called from this one) I want to be able to refer to MainForm.txtTable.text to get the value - but of course I can't.

I have read something about having to put a get and return statement somewhere in the code, but I don't know where, and I have tried quite a few places :)


Any - and the more verbose the better - as I said, complete newbie here - help greatly appreciated as there are 3 txt boxes I want to be able to refer to from the main form.

thanks

 
The problem is when you run your main form, it contains and calls the other forms you are using, so making the text box public only makes it available when something calls your application. What you want to do is pass the string in as a ref so that when its changed in other forms, it will be changed in the main form. For you other forms add a "ref string" as a parameter and when you call the constructor for that form pass in "ref this.txtTable.text".
 
hearing that solution makes me sad....

In a properly layered application, your GUI shouldn't know about any other GUI object. Form1 should not know about Form2. How do we do this then?

You need to look at the Model-View-Controller design pattern. Basically you have a controller that knows how to work with your data. Your controller needs to be available to both forms and each one can make the changes it needs.

Do some googleing on MVC. It will take a bit to get your head around but it will significantly improve your code in the future.

 
I'd listen to JurkMonkey, and not listen to me.
 
Gave both of you guys a star - thanks for replying!
JurkMonkey - will do
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top