5 Replies - 418 Views - Last Post: 06 March 2012 - 01:25 PM Rate Topic: -----

#1 nbarten  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 162
  • Joined: 30-April 07

Managing application

Posted 05 March 2012 - 08:38 AM

Hi all,

I'm currently building a new application... it's still little, only 2 forms, but i wonder, what's the best way to separate my GUI code with my other code?

So basically, i want to create a group (in visual studio, easy) where all the views are located (forms). Normally, when you want a click event of a button you doubleclick on the button... however, the event is created in the same code file where the form is created. Isn't there a way to have some kind of separate file (controller?) for a view(s) where all events for that view are located?

I feel like it's going to be a mess if i continue like this... i know how to make separate classes for handling data (for example in my application, a mysql class), but i think i need a 'root' file which puts all the pieces (forms, events, etc) together.

This post has been edited by nbarten: 05 March 2012 - 08:41 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Managing application

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5571
  • View blog
  • Posts: 11,908
  • Joined: 02-June 10

Re: Managing application

Posted 05 March 2012 - 08:57 AM

I wrote a tutorial specifically for that.

See FAQ # 9, first answer link. (Click the SHOW button below)


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Form1 talk to Form2


FAQ (Frequently Asked Questions - Updated Feb 2012
Spoiler



Was This Post Helpful? 3
  • +
  • -

#3 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • Posts: 3,002
  • Joined: 30-January 11

Re: Managing application

Posted 05 March 2012 - 08:57 AM

I don't think that the event handlers should be a problem as normally the code that you put in them is pretty short.

Take a look at this tutorial Separating Data From GUI - PLUS - Serializing The Data

If you are making loads of event handlers, then split them up into their own regions. That way you can collapse 'views' event handlers that you aren't working on -

#region View1

//have handlers in here

#endregion

Was This Post Helpful? 2
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5571
  • View blog
  • Posts: 11,908
  • Joined: 02-June 10

Re: Managing application

Posted 05 March 2012 - 09:25 AM

Tip #4 below would also also apply pretty closely to your situation.

I commonly hand-off the actual work to another class, keeping the gui just for user interaction

void btnCameraShutter_Click(object sender, eventargs e)
{
    myCameraController.ShootPicture();
}



Let me also throw in a few tips:
  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.

  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)

  • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like index, timeOut, row, column and so on. You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.
    You might want to look at some of the naming guidelines. Its a lot easier to start with good habits than to break bad habits later and re-learn.



  • Try to avoid having work actually take place in GUI control event handlers. It is usually better to have the GUI handler call other methods so those methods can be reused and make the code more readible.
    Spoiler


  • Don't replace lines of code that don't work. Instead comment them out and put your new attemps below that. This will keep you from re-trying the same ideas over and over. Also, when you come back to us saying "I've tried this 100 different ways and still can't get it", we can actually see what you tried. So often a failed attempt is very very close and just needs a little nudge in the right direction. So if we can say "See what you did in attempt 3... blah blah" it helps a lot

    Spoiler

    If you are using Visual Studio you can select a block of lines and hit control+k control+c (Kode Comment) to comment it out. control+k control+u (Kode Uncomment) to uncomment a selected block.


  • I strongly suggest installing VMware or some other virtualization technology on your development PC so you can create a couple virtual computers for testing. This would allow you to debug and test inside: WinXP32, XP64, Vista, Win7x32, Win7x64... etc. without having to actually have 5 physical PC's. Visual Studio will let you send the debug directly into one of these virtual machines so you can watch it operate, check its variables, see the crashes and so on just as if it were debugging on your real machine.

Was This Post Helpful? 1
  • +
  • -

#5 nbarten  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 162
  • Joined: 30-April 07

Re: Managing application

Posted 06 March 2012 - 12:39 AM

Thank you, that's a very useful FAQ. I'll also look into using UserControls, as i have 2 area's which controls can be combined together.

I'm creating a mysql client, with a left side and a right side. The right side starts off with a 'connect-to-host' screen. When succesfully connected to a mysql server, i let the login screen dissappear and instead show a datagrid with data from the first table. I also show all the tables in the database in a list on the left side.

Then i would have 3 UserControls:

- left side: Table Overview (just a list, can be later expanded with a search textfield)
- right side: Connect to mysql server screen
- right side: Data overview (will be expanded with much more)

Is this the right way to do it? And should i group the UserControls together? (by putting them in an apart folder/group)
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5571
  • View blog
  • Posts: 11,908
  • Joined: 02-June 10

Re: Managing application

Posted 06 March 2012 - 01:25 PM

Its hard to advice on exactly *how* you should organize your user controls. Everyone's situation is different.

If you work with a team, maybe the user controls should be their own project because Bob makes those, and you just drop them on the forms.

Or maybe these controls are going to be used through-out an entire family of various software packages, so they would be best in a controls library.

You do know that one Visual Studio solution can house multiple projects, right? In the example below there are four total projects for one solution. Two of which are in a folder because they are both class libraries.

Attached Image


So the right thing for your situation *could* be to go beyond just a separate folder, but into another project. That's a call for you.

This post has been edited by tlhIn`toq: 07 March 2012 - 10:39 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1