Generic way of updating UI's with objects...

  • Thread starter Thread starter Michael
  • Start date Start date
M

Michael

I have a simple situation, where I have a textbox (myTextBox) which
represents the value of say an integer (myInt). Now, whenever I change the
value of myTextBox, I would like myInt to be updated. I was wondering what
the best approach to do this would be, without having to explicitly write
code such as:

myInt = (int)myTextBox.Text

This may be trivial for one such case, but when I have like 40 text boxes, I
don't want 40 lines of code similar to the line above (plus another 40 in
other direction, populated the text box).

So if anyone knows of a tricky way to do this using Tag's or something, or
just the most common practice, it would be much appreciated.
Thanks,



Michael
 
You could use an array of myTextBox and myInt and then use their index to
'set' all of them with that one line
myInt = (int) myTextBox.Text

Yves
 
Michael,

In this case, I would use data binding. I would create an object with a
property (any will do) that exposes your value. Then, I would bind the
textbox Text property to that property. Then, when the textbox is changed,
your value should change as well. However, you have to watch for invalid
values passed to the textbox.

Additionally, if you want the textbox to update when the value of the
integer changes, make sure to place an event with the name
<propertyname>Changed, and make it of type EventHandler. In the property,
when the value is changed, make sure to fire the event. Databinding will
automatically hook up to the event, and change the contents when the value
in the object changes.

Hope this helps.
 
Put all your int vars into an array and tie their index to the Tag property
of their respective TextBox. Then create an event handler for the text
changing on a TextBox. Within the event code change the appropriate int var
value.
 
Hi Michael

Does the community's suggestion resolve your problem?
If you still have anything unclear, please feel free to tell me, I will
work with you.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi Michael,

Thanks for your feedback.

I am glad community's reply makes sense to you. If you have any further
concern, please feel free to post, we will work with you.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top