2
I need to monitor the opening of Forms within a C#application. For example, every time you open a Form within the application, the application must execute a method or trigger an event with information from the open form. I searched and found this code:
public class BaseForm : Form
{
public BaseForm()
{
if (LicenseManager.UsageMode == LicenseUsageMode.Designtime) return;
this.Load += BaseForm_Load;
this.FormClosed += BaseForm_FormClosed;
}
private IEnumerable<Control> GetAllControls(Control control)
{
var controls = control.Controls.Cast<Control>();
return controls.SelectMany(ctrl => GetAllControls(ctrl)).Concat(controls);
}
void BaseForm_FormClosed(object sender, FormClosedEventArgs e)
{
Log(string.Format("{0} Closed", this.Name));
}
void BaseForm_Load(object sender, EventArgs e)
{
Log(string.Format("{0} Opened", this.Name));
GetAllControls(this).OfType<Button>().ToList()
.ForEach(x => x.Click += ButtonClick);
}
void ButtonClick(object sender, EventArgs e)
{
var button = sender as Button;
if (button != null) Log(string.Format("{0} Clicked", button.Name));
}
public void Log(string text)
{
var file = System.IO.Path.Combine(Application.StartupPath, "log.txt");
text = string.Format("{0} - {1}", DateTime.Now, text);
System.IO.File.AppendAllLines(file, new string[] { text });
}
According to the author, all Forms must derive from this BaseForm
.
This approach would be the most viable?
There are ways but appropriate to monitor the opening of Forms within an application?
Thanks for the comments, they helped me solve the problem.
– Robss70