I have created a system tray application with 3 forms:
- Login form
- Main form
- History form.
The main form has a NotifIcon with a ContextMenuStrip property of it.
this.PopupMenu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.Login,
this.Logout,
this.History});
this.MainFormNotifyIcon.ContextMenuStrip = this.PopupMenu;
In that way, the user can right-click the system tray and get a popup menu in order to login. After the user logged in, he can click on History from the popup menu.
In case the user has opened the history form and he clicked on logout, I want to close the History and hide the Main forms as well to the system tray.
Since the Logout is directly accessible from the Main form then hiding the Main form is not a problem. However, I am wondering what is the correct way to close the History form in case the user is logging out.
In order to make it, I have passed the Main form as a reference to the History Form and subscribed to the Main form LoggedOutEvent:
// Main Form
public partial class MainForm : Form
{
public event EventHandler LoggingOutEvent;
private HistoryForm _historyForm;
private void ShowHistory_Click(object sender, EventArgs e)
{
_historyForm = new HistoryForm(this);
_historyForm.Show();
}
private void Logout_Click(object sender, EventArgs e)
{
if (LoggingOutEvent != null)
LoggingOutEvent(null, null);
Hide();
}
}
// History Form
public partial class HistoryForm : Form
{
public HistoryForm(MainForm form)
{
InitializeComponent();
form.LoggingOutEvent += Form_LoggingOut;
}
private void Form_LoggingOut(object sender, EventArgs e)
{
Close();
}
}
So, I have implemented it in a way that I am passing the Main form as reference to the History form in order to subscribed to the Logout event. What if the History form will have a button which opens a History Details Form, will I have to pass the Main form reference to it as well and subscribed to the LogoutEvent in a similar way?
Is there a better way to implement it?
Aucun commentaire:
Enregistrer un commentaire