C# Windows Forms App: Separate GUI from Business Logic

Happy Go Lucky picture Happy Go Lucky · Jul 18, 2012 · Viewed 8.2k times · Source

I would like some advice on how to separate the UI and business logic in a simple C# Windows Forms Application.

Let's take this example:

The UI consists of a simple textbox and a button. The user enters a number between 0 and 9 and clicks the button. The program should add 10 to the number and update the text box with that value.

enter image description here

The business logic part should have no idea of the UI. How can this be accomplished?

Here's the empty Process class (Business Logic):

namespace addTen
{
    class Process
    {
        public int AddTen(int num)
        {
            return num + 10;
        }
    }
}

The requirement is:

  1. When the user clicks the button, somehow, the Process::AddTen gets invoked.
  2. The Textbox must be updated with the return value of Process::AddTen.

I just don't know how to connect these two.

Answer

David picture David · Jul 18, 2012

First, you need to change your class name. "Process" is name of a class in the Class Library and will likely cause confusion for anyone reading your code.

Let's assume, for the rest of this answer that you changed the class name to MyProcessor (still a bad name, but not a well-known, often-used class.)

Also, you're missing the code to check to ensure that the user input is, indeed, a number between 0 and 9. That's appropriate in the Form's code rather than the class code.

  • Assuming the TextBox is named textBox1 (The VS generated default for the first TextBox added to the form)
  • Further assuming the button's name is button1

In Visual Studio, double-click on the button to create the button click event handler, which will look like this:

protected void button1_Click(object sender, EventArgs e)
{

}

Within the event handler, add code so it looks like this:

 protected void button1_Click(object sender, EventArgs e)
 {
   int safelyConvertedValue = -1;
   if(!System.Int32.TryParse(textBox1.Text, out safelyConvertedValue))
   {
     // The input is not a valid Integer value at all.
     MessageBox.Show("You need to enter a number between 1 an 9");
     // Abort processing.
     return;
   }

   // If you made it this far, the TryParse function should have set the value of the 
   // the variable named safelyConvertedValue to the value entered in the TextBox.
   // However, it may still be out of the allowable range of 0-9)
   if(safelyConvertedValue < 0 || safelyConvertedValue > 9)
   {
     // The input is not within the specified range.
     MessageBox.Show("You need to enter a number between 1 an 9");
     // Abort processing.
     return;
   }

   MyProcessor p = new MyProcessor();
   textBox1.Text = p.AddTen(safelyConvertedValue).ToString();
 }

The class, with the access modifier set properly, should look like this:

namespace addTen       
{       
    public class MyProcessor
    {       
        public int AddTen(int num)       
        {       
            return num + 10;       
        }       
    }       
}