Hi,

Is it possible for a text box to always get its contents (text) from a particular variable?

In C terms I guess I'd say "can I have the contents of a text box point to a pointer?" or something like that.

Basically, I want the text box to always be displaying the latest value that the variable holds.

Thanks for any and all help.

Andrew.

Recommended Answers

All 6 Replies

How is the variable getting changed?

One simple way I can think of is just to make a class to encapsulate your variable as a property. Then, you could make an event trigger every time the variable was changed, and have that update the value of the textbox.

I have an application with data in different windows, but it's based on the same core text.

Can you please explain how I would achieve what you have said? Or point me in the direction of a tutorial?

Thank you for your help :)

Andrew.

Hi,

You can make your controls databound to a field on a dataset but if you just need to bind it to a variable declare your variable as private in your class and encapsulate it as a property with get {...} and set{...} accessors. VS2005 does this automatically for you if you select
"refactoring->encapsulate as property" from context menu. Then on the generated set { FVar = ....; /* add here */} part of the code add an assignement from FVar to your control on the add here part.

Loren Soth

This advice sounds better than mine, actually.

Soth, could you go into a little more detail on this approach? I'll be the first to admit I'm not a C# expert, and I'm always interested in learning more.

Argh, I didn't see you'd added a reply!

Can you elaborate for me too please? If I go to the DataBindings section in VS2005 it seems to only want to bind to a SQL datasource :(

Hi,

The second part is almost the same solution alc6379 suggested (encapsulate in a property) I just reminded that VS 2005 has a feature to make this get/set code generated automatically via right clicking on a variable then chosing "refactoring->encapsulate as property". You can directly put your code to change editbox contents in get/set accessors instead of an event (in case you ment an event with delegates).
To make a control databound is even easier you can put a Dataset object on your form and if you want it to work stand alone (not connected to an actual DB) like a memory DB you can add tables and columns (DataFields) to those tables and set other schema details using the properties of Dataset object. You can bind your controls to that dataset's tables' columns. You can even make DataGrid work with that Dataset and save the data as XML file to load it later (like an XML DB).

Loren Soth

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.