Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use separate .cs files in C#?

Tags:

c#

Forum; I am a newbie working out a bit of code. I would like to know the best way to use separate .cs files containing other classes and functions. As an example of a basic function would would like to click on a clear button and have it clear all fields on a form. The code is split up into three .cs files. Main.cs, MainForm.Designer.cs and btClear.cs.

MainForm.Designer.cs contains the designers automatically created code including the Clear Button, and the text boxes I would like to clear.

I would like to have the code to clear the text boxes contained in btClear.cs. I understand it would be easy to simply to place this code in MainForm.Designer.cs however I would like to use this as a template in order to use separate .cs files as a standard practice.

Can someone give me an example of how to do this please?

like image 286
tejas_grande Avatar asked Feb 21 '09 05:02

tejas_grande


Video Answer


2 Answers

The way most .net programmers would do it is:

  • Leave all your code inside MainForm.cs, but declare a new method for separating the code that does the clearing.

I always leave in the event handler method (the one VS generates when you double-click the button) code to change the mouse cursor (in case the code I'm calling takes a couple of seconds or more to run) and catch all unhandled exceptions, and put my logic in a separate private method:

partial class MainForm : Form // this is MainForm.cs
{
    // This is the method VS generates when you double-click the button
    private void clearButton_Click(object sender, EventArgs e)
    {
        this.Cursor = Cursors.WaitCursor;
        try
        {
            ClearForm();
        }
        catch(Exception ex)
        {
            // ... have here code to log the exception to a file 
            // and/or showing a message box to the user
        }
        finally
        {
            this.Cursor = Cursors.Default;
        }
    }

    private void ClearForm()
    {
        // clear all your controls here
        myTextBox.Clear();
        myComboBox.SelectedIndex = 0;
        // etc...
    }
}

In my opinion, if you want to follow the conventional way of doing things in .net, you should stick with this first example.

Moving code out of the form .cs files is recommended when the code is not part of the form logic, for example, data access code or business logic. It this case I don't think that clearing the form controls qualifies as business logic. It is just part of the form code and should stay in the MainForm.cs.

But if you really want to put the ClearForm method in another .cs file, you could create a new class and move the ClearForm method there. You would also need to change the Modifiers property of each control to Public so your new class can have access to them from outside MainForm. Something like this:

public class FormCleaner // this is FormCleaner.cs
{
    public void ClearForm(MainForm form)
    {
        // clear all your controls here
        form.myTextBox.Clear();
        form.myComboBox.SelectedIndex = 0;
        // etc...
    }
}

You would have to change the main form code to:

partial class MainForm : Form // this is MainForm.cs
{
    // This is the method VS generates when you double-click the button
    private void clearButton_Click(object sender, EventArgs e)
    {
        this.Cursor = Cursors.WaitCursor;
        try
        {
            FormCleaner cleaner = new FormCleaner();
            cleaner.ClearForm(this);
        }
        catch(Exception ex)
        {
            // ... have here code to log the exception to a file 
            // and/or showing a message box to the user
        }
        finally
        {
            this.Cursor = Cursors.Default;
        }
    }
}

But note that your FormCleaner class would have to know about your MainForm class in order to know how to clear each of the controls of the form, or you would need to come up with a generic algorithm that is able to loop through the Controls collection of any form to clean them:

public class FormCleaner // this is FormCleaner.cs
{
    // 'generic' form cleaner
    public void ClearForm(Form form)
    {
        foreach(Control control on form.Controls)
        {
            // this will probably not work ok, because also
            // static controls like Labels will have their text removed.
            control.Text = "";
        }
    }
}

And as others have said, MainForm.Designer.cs is machine-generated and you should never put your own code there.

like image 101
Sergio Acosta Avatar answered Oct 05 '22 17:10

Sergio Acosta


You can use a partial class for your MainForm class, since that's already being done in MainForm.cs and MainForm.Designer.cs.

btnClear.cs

public partial class MainForm
{
   private void clearForm(object sender, EventArgs e)
   {
     // ...
   }
}

And register for the event in MainForm.cs or MainForm.Designer.cs

this.btnClear.click += clearForm;

Edit:

If you want a generic way of doing it, you could set the Tag property of your controls to be the default value. And with an extension method, you could do something like formGroup.Reset();

using System.Windows.Forms;

public class ControlExtensions
{
  public void Reset(this Control control)
  {
    if (control.Tag != null)
    {
      if (control is TextBoxBase && control.Tag is string)
      {
        control.Text = control.Tag as string;
      }
      else if (control is CheckBox && control.Tag is bool)
      {
        control.Checked = control.Tag as bool;
      }
      // etc
    }

    foreach (Control child in control.Children)
      child.Reset();
  }
}
like image 40
Samuel Avatar answered Oct 05 '22 18:10

Samuel