views:

45

answers:

2

Hi All,

I have a an Winform application with 2 forms. In one form I have a Tab Control with 3 Tabs and navigation buttons to switch between tabs.

On the first tab the user selects a file and on navigating to next tab i want to do some processing on the file selected in the first tab,and show the result in the 3rd tab.

The other form just invokes this form(start app.)

How can i do this using MVC/MVP ?

Currently i have created nested forms. Starting application form creates instance of tab form and on SelectedIndexChanged on the tab control and matching the selected tab property I'm doing the processing in the starting application form.and On closing on the tab form setting the result in the starting application form.(which isn't ideal).

Edit : Also each tab contains a User Control which i have to initialize on tab change (refereeing to the data selected in the previous tab.)

Simple example is selecting a .zip file in the first tab , clicking next will show the list of files within the zip file and in the third tab do processing with the file selected in the 2nd tab.(there are better ways to do the same..just for sake of example.)

EDIT 2 : Basically I'm confused on how to get values from first tab via controller, do processing, and pass it to the next tab( via controller) and set the user control properties on the 2nd tab (via controller).Also the Tab titles are removed ..please see ..so the Tab form looks more like a wizard form. that's why i was using the SelectedIndexChanged property.

Basically i need to separate view and processing logic from the Winform.

Thanks All.

+1  A: 

Odd choices for a UI. Anyhoo, there is no reason whatsoever to wait for SelectedIndexChanged to process the file. You might as well do it as soon as the file is selected. It will work better, the tab control becomes more responsive. If you wait until the event then the control will be frozen for a while as your UI thread is busy iterating the .zip file. The user will not consider this desirable.

Makes the MVC implementation a lot simpler too, whatever it might look like. Extra bonus is that you now no longer depend on the TabControl and can use whatever controls are best for the job.

Hans Passant
What if the user selects a different file .. then will have to again do the processing part ? I want to delay the processing till the next button/next tab is clicked,and where should the processing be done?in the controller?
Amitd
What if she doesn't, the 90% case? I just don't see why you'd force your user to click the 3rd tab. I would auto-select it, now it doesn't matter anymore. Processing would normally be in the Model if it cares about the files inside the archive. But I don't know what your model looks like.
Hans Passant
Basically I want to transform it into an MVx pattern.But I'm confused on how to get values from first tab via controller do processing, and pass it to the next tab( via controller) and set the user control properties on the 2nd tab .(i have added this part as "Edit2" .)
Amitd
Hmya, I don't know what "MVx" means. There's nothing in Windows Forms that encourages any kind of particular design (like WPF's MVVM pattern). Sounds to me that you are better off with the RAD pattern and get this relatively simple task working in the next hour or so.
Hans Passant
I agree with @Hans Passant (despite my long-winded answer to the original question) - forget MVC or MVP and get it working minus the pattern.
Peter Kelly
yes I have it kind of working ... but most of the code is coupled to form and i want to avoid writing code on forms. Its part of learning patterns etc not exactly production/dev kind of thing etc :)
Amitd
+1  A: 

Your Model will deal with your zip file in this case, e.g. methods like Print(), Extract() etc. and events like PrintCompleted and ExtractCompleted etc.

Your IView will expose methods and events that abstract your interaction with the UI behind an interface. So perhaps some methods such as DisplayFolderContents() and events like FileSelected() etc.

Your presenter will hook up to the Model and IView events and control which methods are called on each etc. The Form that you have a TabControl on is just an implemenation of the IView interface. You could have a different view just by injecting an implementation of IView into the Presenter, including a Mock IView for testing.

Values can be passed around the MVP pattern through the EventArgs you use.

/// <summary>
/// The event args for a selected file.
/// </summary>
public class FileSelectedEventArgs : EventArgs 
{
    public string FileName { get; private set; }

    public FileSelectedEventArgs(string fileName)
    {
        this.FileName = fileName;
    }
}

When the user selects a file, the FileSelected event is raised in your View, with the FileName available in the FileSelectedEventArgs. The Presenter listens for this event, and calls a method on the Model - maybe ExtractFile(string fileName), passing in the fileName from the FileSelectedEventArgs from the View.

The Presenter then listens to the ExtractCompleted event to be fired from the Model (which also has whatever custom eventargs you want) and calls the appropriate method on your View, passing in the parameters from the Model. The method in the View can do whatever you want in terms of displaying the data in a TabControl or in another way.

This is just one way of doing it anyway.

Peter Kelly