|
-
Oct 30th, 2010, 12:42 PM
#1
[RESOLVED] Where to setup dialog forms from
Hi Guys,
Just a little question that doesn't really matter that much but I'm just curious how everyone else does this.
The question is essentially: Where do you setup your dialog forms from? Do you set properties and populate items from the form that calls the dialog form or do you do it in the dialog form's Load event?
Lets say for example I have a main form and a dialog form that lets the user add/remove some strings from a listbox that the main form then uses (and therefore the current items in the listbox should be remembered next time the dialog is shown). Would you populate the listbox with the existing strings from the main form and then show the dialog window, or would you pass the strings in to a property of the dialog window and then let that window put them into the listbox when it loads?
I realise its not the best example because you could avoid having to do either if you just didnt dispose the dialog window, but there are numerous situations in my programs where I need to setup items on a dialog window (and dispose it when it has closed) and I currently tend to do it all from the form that is showing the dialog window. This often means I end up with a ton of code in my 'main' form if it calls several dialog windows though so it just got me wondering if other people tend to let the dialog windows do all the work themselves.
How do you guys handle this?
Cheers
Chris
-
Oct 30th, 2010, 03:05 PM
#2
Re: Where to setup dialog forms from
I pass the data in ... that way if I want to re-use the dialog, it works the same everywhere... if that made sense. The parent form shouldn't care what the child form does with the data. I look at it as being the same as scribbling a message, tying it to a rock, and throwing it over a wall... on the other side, you don't care what's done with the data. Same thing with data... just pass the data off to the form and let it take care of displaying the data. Besides, if you then change it from a list view to a grid... you only change the child form. If your parent form adds data to the listview, then you have to change that too.
Make sense?
-tg
-
Oct 30th, 2010, 05:11 PM
#3
Re: Where to setup dialog forms from
Thanks TG, yeah that makes sense and seems like a good idea. So you would just have a property on your dialog form that accepts a string list/array (using the example from my original post) and then in the dialog form's Load event you would add those items to the listbox?
-
Oct 30th, 2010, 06:46 PM
#4
Re: Where to setup dialog forms from
That's exactly right. Quite frankly, no form should ever access a control on another form. One of those VB "make it easy for the beginners" things is setting the access level of anything you add in the designer to Friend by default. In C#, all components added in the form designer have their access level set to 'private', and I always change it to Private in VB too.
It's a fundamental tenet of OOP: separate interface and implementation. When dealing with any object, you should deal with its interface only without caring about the implementation. That's basically what tg was saying. If your main form creates a dialogue that displays a list of strings then all the main form has to care about is the list of strings. What the dialogue does with that list is an implementation detail that the main form doesn't care about.
-
Oct 30th, 2010, 07:18 PM
#5
Re: Where to setup dialog forms from
I would do it one of three ways... parameterized constructor... Or by a SetData (or some other method) or... by exposing properties. I personally use the constructor for required information, stuff that has to be there everytime for the form, and properties for optional items.
-tg
-
Nov 3rd, 2010, 08:21 AM
#6
Re: Where to setup dialog forms from
Ah ok thanks both of you for the comments, I didn't realise C# set each control to private. Will definitely take these suggestions into account in future Cheers
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
|