Click to See Complete Forum and Search --> : Design Issue
scathencc
September 4th, 2008, 08:46 AM
Hi All,
I have a scenario for which I am trying to find an elegant design.
The details are as follows:
1. I have a class which defines my data structure within my application. It is serialisable etc.
2. I also have a handful of forms which contain controls which I want to use to read/write to and from my data class.
What I am looking for is a way in which I can link a control to a particular variable within my data class. (ie. I have a TextBox, and whenever the text changes I would like a particular variable in my data class to also change).
Bear in mind that I have multiple forms which all must link to variables within the one data class, and I don't want to have to handle a new OnTextChanged event for every single control.
Thank you in advance for any ideas.
boudino
September 4th, 2008, 10:22 AM
For connecting to your class's properties, use data binding. For ensuring that all form access the one instance of the class, use singleton pattern.
Arjay
September 4th, 2008, 03:17 PM
If you aren't too far with the coding, I would suggest looking into using WPF.
WPF offers more advanced data binding than WinForms.
JonnyPoet
September 5th, 2008, 10:53 AM
...
Bear in mind that I have multiple forms which all must link to variables within the one data class, and I don't want to have to handle a new OnTextChanged event for every single control.
Thank you in advance for any ideas.There is NO magic. Whatever you are doing whatever concept you are using for example observer pattern MCP Pattern MVC or whatelse yout Textbox needs to inform your data when a change is done. THIS NEEDS that a TextChanged event is fired as your data are in the dataclass but the change is done in the textbox so an event needs to be fired if you want to inform the dataclass about ANY changes of the text in the textbox. But all the following changes which are maybe result of the change in the textbox they have to be handled - depending on the used design - for example from the commander in the MVC pattern, or from the observer if you are using observer pattern. So you need to use OnTextChanged to inform the Commander about the changes done and the commander then decides all the changes in the different views depending on the data in the dataModel Layer. Maybe google for this patterns MVC, MCP
scathencc
September 6th, 2008, 08:16 PM
Thanks for all the feedback. With regards to WPF, I agree that it is already setup to perform the solution to the problem above, however due to some other reasons I have to use .NET 2.0 .
My data class is already implemented to be a singleton pattern, I guess the problem I am having is figuring out how to bind that variables from my class to particular controls in the forms. Any explanation of how to do this would be great.
What I had envisioned for example, was to create say a custom Textbox which contains a property that I can set as a link to a variable in my data class. I would then override the textchanged event and update my data link there.
So just to summarise:
If I had a class with variable named Data (which could be set). How would I go about telling a textbox on a particular form that when it changes it's value I would like it to update the Data variable within my class?
Bearing in mind that I have LOTS of these textboxes so I don't want to individually handle the textchanged event for each one, but would rather create a custom control where I can link whatever variable I like.
Thanks again
Arjay
September 6th, 2008, 08:37 PM
Bearing in mind that I have LOTS of these textboxes so I don't want to individually handle the textchanged event for each one, but would rather create a custom control where I can link whatever variable I like.I don't know the design of your UI, but whenever I hear that LOTS of test boxes are being used I have to wonder about the usability of such an app. As an alternative to using so many textboxes, consider using a list view control. The textbox functionality can be per an inline edit of a listview item. The binding should be simpler as well.
scathencc
September 7th, 2008, 08:43 AM
I agree about the design issues, however the UI has been specified by the client. If anybody else has any ideas to how I would go about binding a variable of a class to the text changes of a TextBox that would be fantastic. At the moment my only solutions are to either handles events individually for each control, or to create big and annoying save/load functions within each form.
scathencc
September 7th, 2008, 09:47 AM
Thanks everyone, but I figured it out (and of course it was really simple). You can bind variables of classes much the same way you bind any data source. It was simply TextBox.DataBindings.Add("Text",instanceofclass,"Propertyinsideclass")
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.