I'm taking my first class in C# development for winforms. I'm using VS Premium 2013.
We've been working on an application all semester. For our final project one of our tasks is to convert it from an SDI to an MDI.
The mainform is structured as follows:
menustrip
toolstrip
split panel with a treeview control in its own class, a listview control in its own class, and a rich text box in its own class (these are all then displayed on the mainform)
user control that functions as a status bar docked at the bottom of the form
The code for all the click events and business logic is already written. This is already a robust application.
Is there a "best practice" for converting an SDI like this to an MDI? There's a ton of code here, and unless I get better guidance, I assume I'm going to have to create a new project, move over the classes that I can and then rewrite all the supporting event handlers and associated logic.
Is there a more time-efficient option that someone can recommend?
There is no single answer because the specifics of the migration depend on the specifics of your current application. If you have written it well then there should be almost no change at all. It should be mostly just setting the MdiParent of each document form before showing it. There would still be a few details to consider but that would be the bulk of.
So that changes like this are relatively painless is one of the main reasons that design patterns are used in development. Also, the less coupled the types are in your project, the easier they move around in these cases. For instance, if you have two types that each rely on the other to be exactly as they are then any change is going to have significant impact on both. If those types are decoupled though, a change in one may have absolutely no effect on the other.
Related
Our application has a central main form. Since we are at the start of the project a lot of buttons and controls are being added to this form. This, as expected, creates conflicts when merging our code together using VCS. It is also almost impossible to solve the conflicts since the designer part of a form is generated automatically.
I was wondering if there is a practice or workflow strategy to minimize conflicts in a form where several people work on.
Remove as much logic as you can so your form becomes simply a view. This minimizes the conflicts - but it doesn't resolve them entirely.
Your best strategy is to make sure people pull from and push to your master/trunk/branch as often as possible so the conflicts are minimal.
If your team mainly works on feature branches, then have someone dedicated (or a couple of developers working closely together) do the feature merges back to your master/trunk. The conflicts will be many, but this alleviates the need for individual developers to track merge conflicts and attempt to fix them in isolation. The developer(s) responsible for the merging back to master/trunk will know what needs to come in/be left out and can manage it as part of their process.
Again though - your best bet is to make your form literally be a dumb view that does nothing but render controls. Everything else should be handled elsewhere.
The only techniques I've seen are:
Refactor to controls so that impact is minimal
Limit your UI changes to be just the designer or developer with best ux intuition
Use the "lock" feature of the VCS system if your VCS system supports it. That will prevent others from checking it out if you're working on it.
.NET has a tool for that, it is called a UserControl :
Separate your form in multiple custom UserControls, each of which can be developped independently. Eventually your MainForm would be the assembly of a few high level UserControls, each being itself an assembly of UserControl etc...
Creating UserControl will also allow you to reuse them in other parts of your appliction, making it much more fleible and easier to maintain
You can follow MSDN tutorial about creating a UserControl.
Do not work with a single Form.
I have a windows form class that has a tab control with many tabs. The problem is as the visual user interface is growing ever larger due to the number of tabs, the code that handles all the controls event is also becoming very large.
I am looking for the best technique in how you would organise this.
Currently I have split much of the code into separate:
#region
#endregion
To help organise it. I also had the idea of possibly using form inheritance but I dont think this will solve my problem exactly. The other idea I had was to separate the events of each tab into partial classes.
Is there an easier or more practical way that I am missing? Thank you.
What I'd do, is separate each of the tabs controls, into specific user controls, which will allow you to have the user controls maintain their own methods etc. It's quite possibly going to be quite a lot of work, but in the long run, you'll find it easier to maintain. Also, while building the user controls, why not see if there is any commonality between functions and merge, within a new user control?
In general, I am wondering when to use custom controls vs integrating the control directly into your form.
In particular, I have a form that contains a tab page, with 6 tabs each containing a number of .net framework controls. Currently, I have in my project 6 user defined controls that I dock onto each of those tab pages.
I.e. there is no reuse of controls, I just use them to manage the complexity of a form that would otherwise contain 10 gridviews, 20 buttons, 6 date controls, etcetera. Would you consider this a good way to manage this complexity, or would other alternatives be better (in enabling the programmer to
understand what is going)?
I think this kind of division of page to custom/usercontrols may prove to be useful even without reusing the parts. This gives you:
Encapsulation - you can hide the boring plumbing and fragile internal state from higher level code, leaving you a much cleaner set of controls to work with and more business-oriented code on page level.
Separation of concerns - you can make pieces which are simple, yet logically whole.
Readiness for reuse - may never become useful but it does make you feel more powerful ;)
But be aware that this can backfire if your components are not properly named and grouped, badly designed, you use lots of dynamic loading, do not provide good API or nesting goes too deep. I have seen it happen in a legacy app and it wasn't helpful that way.
If done well, worth it. With inexperienced/sloppy team members, better don't.
What is complexity for you? If you ask me, you are making it more complex if you use usercontrols as the way you use. Usercontrols should be used to prevent code duplication. Basically what you are doing is moving your logic to your usercontrol and deviding your logic into multiple pages so you have less code on your main page. This shouldn't be your main concern if you ask me.
If you have complex logic in a view model or controller for the sections of screen, then this can be a beneficial approach. It is making your overall form more of a composite. You can develop the functionality behind the sections/controls in the view models and/or controllers separately and unit test them separately. As ImreP says, this is along the lines of separation of concerns.
However, if there's a lot of interaction between the separate controls on your form, then this might not be the right way. If you can find separate areas of behaviour/responsibility for different areas of the form, then the split may be a good idea, given your premise that there is quite a lot of UI happening.
If it's simply for the sake of having fewer basic controls in each package, and there's no chance of reuse of any of the components, then it might be muddying the waters somewhat.
A good test might be to show it to someone else on your team and see how long it takes to explain it to them. You'll get a good idea if it's intuitive or not by trying to show it to someone who hasn't come across it yet.
I can't decide if it is good or bad to make many user controls. I am only doing it cause I find it easier to work on a control where there are not a lot of components. If something needs to be fixed it is also easier. Kind of like how you split your program up in a lot of classes.
However multiple controls adds a bit more complexity when it comes to passing data around. I guess my question is more if it is normal to create a 'god' class when it comes to GUI programming in winforms.
Almost every video tutorials I see, they only work on one form! While I can use like 5 controls before I have a form.
Reasons to create User Controls in WinForms:
Reuse of functionality.
Encapsulation and data hiding.
Readability and maintainability.
Single responsibility principle.
Design-time editor integration for assignable properties.
Ability to refactor/enhance/reuse in the future.
Have you heard about encapsulation and components? It is just your case.
Well from a Web Developers perspective -- no, I don't believe so. In fact I believe in the NerdDinner book for ASP.NET MVC there's a section where the author(s) creates a partial (similar to usercontrol) for the sake of readability purposes. And these are the top guys at MS who wrote this book.
I have a c# windows form app in which we did a rapid prototype for the client. The extent of my c# experience has been with ASP.NET/C# so its a bit different when it comes to the actual structure of the software. Time is a factor in this project since it is a school project and we have a little under 2 months to be done.
The software will be mainly database driven.
Current Project Design
Currently there is 1 main windows form and a bunch of class files.
One idea we had is that since most of the actual code of the program will be in classes, should we just split the windows form up into a bunch of tiny windows forms with one main driver so to speak? or just leave it as one large form and try to have as little code as possible in the main win form?
What are your suggestions on the design of a c# database driven windows form app?
Please give pro's and con's to each.
Personally I favor an MDI (Multiple Document Interface) over a single form because a single form can become cluttered without careful planning. Also most clients like the idea of a MDI for database driven apps because it reminds them of Microsoft Access. I also like the idea of breaking everything into classes. The rule of thumb that I use for deciding weather or not to make a class is "Will I ever in my programing career need to do X again?". In this case X is what you would put in that class (e.g. Determine Leap Year). If you think that your form won't become cluttered then totally go for a Single form. Hope this helps you decide.
While I agree that you almost certainly want more than one form, I'm not at all a fan of MDI. Rather, I prefer to think about the common tasks that users will perform and design application around the user's expected work-flow, such that there is a natural progression from piece to piece. Of course, that also means providing a way for power users to get past this expected flow when they really want to.
A monolithic form that provides "kitchen sink" functionality is every bit as bad as a monolithic class that provides "kitchen sink" functionality.
Since it's a school project, I'll assume you're still learning this stuff.
Break the application down into smaller forms that do less. Each form should do one thing and do it VERY well. Its functionality should be well defined, cohesive, and as loosely coupled as you can make it. Try to keep the database code out of it (move that code into your classes, and invoke them from the form).
A form is nothing more than a way to display the information that is returned by the classes (in other words, a view on the data). Keep it simple like that and you will find it much easier to add new forms, change the existing forms, and extend your application down the road if you need to.