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 func
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();
}
}