2

i built an Winform application with Pcapdot.Net DLL's that take Pcap file and play all the file packets into the machine network card.

recently i have notice that all my main form (where all the buttons clicks\events) is a mess, i putt all the method inside those function and the code looks complicated and it's hard to understand so i started to rearrange this code.

because my application has Listbox where all the played files is inside i put all this play function inside the Play button for example: i have loop over my Listbox.Items.Count and inside i am handle this files. now i want to do something different and this is my question:

is it common way to define class that handle this Play function and from the main form every time that file added to my Listbox just fire up the event ControlAdded or after remove file ControlRemoved and put this files into my class who has List that hold this file ?

user2214609
  • 4,713
  • 9
  • 34
  • 41

1 Answers1

2

I think you would be better off rearranging it more like this (to separate the business logic from your UI):

  • Have a public method inside your Form class that returns an IEnumerable<string> which is the list of files, called something like SelectedFiles().

  • Make a public event property for the "Play" button which is raised when the user clicks the "Play" button, called something like PlayClicked.

  • Make a "controller" class which is responsible for creating and showing the form.

Your controller class would attach to the PlayClicked event. The controller's handler for PlayClicked would call the form's SelectedFiles() method to get the list of files, and would then do whatever it needs to do.

I would even consider wrapping the handling code for the selected files into another class called something like SelectedFileHandler and put the logic for handling the files into that, perhaps in a method called HandleFiles(IEnumerable<string> files).

Then the "controller" class would still be hooked up to the PlayClicked event, but it would use the SelectedFileHandler.HandleFiles() method to handle the files.

Does this make some sense to you? I could be misunderstanding what you're looking for.

I'm basically saying that you should use a Model View Controller or a Model View Presenter architecture.

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
  • @user2214609 Unfortunately I don't have time to write a code sample just now, but have a look at [my answer to this stack overflow question](http://stackoverflow.com/questions/15605161/how-to-make-form1-label-text-change-when-checkbox-on-form2-is-checked/15605436#15605436) - I posted some sample code which should show you how to create the public button clicked event and respond to it in a controller. – Matthew Watson May 22 '13 at 10:33