|
-
September 4th, 2008, 08:46 AM
#1
Design Issue
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.
-
September 4th, 2008, 10:22 AM
#2
Re: Design Issue
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.
- Make it run.
- Make it right.
- Make it fast.
Don't hesitate to rate my post. 
-
September 4th, 2008, 03:17 PM
#3
Re: Design Issue
If you aren't too far with the coding, I would suggest looking into using WPF.
WPF offers more advanced data binding than WinForms.
-
September 5th, 2008, 10:53 AM
#4
Re: Design Issue
 Originally Posted by scathencc
...
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
 Jonny Poet
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
My latest articles :
Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
-
September 6th, 2008, 08:16 PM
#5
Re: Design Issue
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
-
September 6th, 2008, 08:37 PM
#6
Re: Design Issue
 Originally Posted by scathencc
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.
-
September 7th, 2008, 08:43 AM
#7
Re: Design Issue
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.
-
September 7th, 2008, 09:47 AM
#8
Re: Design Issue
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")
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|