tags:

views:

58

answers:

3

Hello everyone,

I am new to windows.forms programming. I started making an application that has the following flow of events:

  • click a button that opens a file - extract its contents in some list of strings - visit some websites - parse their content - etc.

So because everything in my app happens after i click a button to open a file, I have put all my code on the click event of the button. However I do know this is bad coding practice, because I realised I ended up having ALL the program flow inside that click event. I know that the event should only contain code related to the button, but where to place the code that follows, if not inside the event? Is there another event that I should use instead of just writing all in the button click?

I hope I've made my question clear. If not then I'll retry to explain my problem. I simply don't know where to write the code that follows the click event. If I put it in:

public Form1()
{
    InitializeComponent();
}

..then it executes before the click event which is wrong.

Thank you in advance.

A: 

You should look into the Model View Presenter pattern. http://msdn.microsoft.com/en-us/magazine/cc188690.aspx

Robert S.
thank you I will have a look.
Andrei
+3  A: 

The typical way to do this is to write one or more methods that perform the action, and call those from the click event. For any long-running actions, do them in a background worker thread.

For example:

public void myButton_OnClick(EventArgs e, object sender)
{
    VisitWebSites();
}

private void VisitWebSites()
{
    var webSiteList = GetWebSitesFromFile();
    foreach (var w in webSiteList) {
        StartVisitingWebSite(w);
    }
}

private IEnumerable<string> GetWebSitesFromFile()
{
    // whatever
}

private void StartVisitingWebSite(string url)
{
    backgroundWorker1.RunWorkerAsync(url);
}

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    string url = (string)e.Argument;
    e.Result = VisitWebSite(url);
}

private string VisitWebSite(string url)
{
    // This is called in background thread.  Do whatever you do to return data.
}

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    if (e.Error || e.Cancelled)
        return;

    string result = e.Result.ToString();

    // Do whatever you do with the result
}

Look at the BackgroundWorker documentation to see how to perform actions in a background thread and then handle their results.

Kristopher Johnson
excellent answer - you explained it to me clearly, helped me understand the snippet by correctly naming the methods, and also provided me a lesson into multithreading.thank you so much!
Andrei
+1  A: 

You can encapsulate all of the work that you want to do into another function in the form's class. Its modification access would be PRIVATE of course (unless you want another class to be able to access the method). From inside of your button_click event handler, you can call this new function. That is simplest way to do this. Otherwise, you can use the example provided in the link above by @Robert S.

AndHeCodedIt