Do not Exit if Form is Open

Lanc1988

Contributor
Joined
Nov 27, 2003
Messages
508
I have my main form and I have this other form called frmGame.

Is there a way that if the user has the frmGame window opened then if they try to close the main form it will give a message that says "Please make sure you are logged out of Game before closing".

So in other words how can I have my program do certain events if a form is open?
 
I tried this but it isn't working..:
Visual Basic:
        If frmRuneScape.Show Then
            If (MessageBox.Show("You seem to have the Play RuneScape form open. Are you sure you want to exit Rssk?", "Are you sure?", MessageBoxButtons.YesNo) = DialogResult.No) Then
                e.Cancel = True
            End If
        End If
 
hmm.. i downloaded the attachment you posted and tried looking at how to fix my problem but I only got more confused.. isn't there a simplier way to do what i need? or is what you posted the most basic simple way to do what i need?
 
I dont know how simpler it could be. . . Well yeah I do, do it in C# as VB sux as an OO language but heres the explanation -

Main form has an array list to hold child forms. . .
Visual Basic:
[font=Courier New][size=2][color=#0000ff]Private[/color][/size][size=2] childForms [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]New[/color][/size][size=2] System.Collections.ArrayList

[/size][/font]Main form has a sub with an EventHandler delegate signature. . .
Visual Basic:
[font=Courier New][size=2][color=#0000ff]Private [/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2] ChildCloseHandler([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As [/color][/size][size=2][color=#0000ff]Object[/color][/size][size=2], _[/size][/font]
[font=Courier New][size=2][color=#0000ff]      ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] EventArgs)[/size][/font]
[font=Courier New][size=2][color=#0000ff]  If[/color][/size][size=2] childForms.Contains(sender) [/size][size=2][color=#0000ff]Then[/color][/size][size=2] childForms.Remove(sender)[/size][/font]
[font=Courier New][size=2][color=#0000ff]End [/color][/size][size=2][color=#0000ff]Sub[/color][/size][/font]
If childForms contains the sender, ChildCloseHandler removes it

Now, ChildForm has a constructor that takes an EventHandler delegate as an argument and assigns it to the ChildForms Closed Event
Visual Basic:
[font=Courier New][size=2][color=#0000ff]Public [/color][/size][size=2][color=#0000ff]Sub [/color][/size][size=2][color=#0000ff]New[/color][/size][size=2]([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] OnClosed [/size][size=2][color=#0000ff]As[/color][/size][size=2] EventHandler)[/size][/font]
[font=Courier New][size=2][color=#0000ff]  Me[/color][/size][size=2].New()[/size][/font]
[font=Courier New][size=2][color=#0000ff]  AddHandler [/color][/size][size=2][color=#0000ff]Me[/color][/size][size=2].Closed, OnClosed[/size][/font]
[size=2][font=Courier New][color=#0000ff]  Me[/color][/font][/size][size=2][font=Courier New].Show()[/font]
[/size][font=Courier New][size=2][color=#0000ff]End [/color][/size][size=2][color=#0000ff]Sub[/color][/size][/font]
When a ChildForm closes, it will send itself to the event handler ala a call like

OnClosed Me, EventArgs.Empty

In the Closing Event of Mainform this code is added:
Visual Basic:
[font=Courier New][size=2][color=#0000ff]Private[/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2] MainForm_Closing([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Object[/color][/size][size=2], _[/size][/font]
[font=Courier New]     [size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As [/color][/size][/font][font=Courier New][size=2]System.ComponentModel.CancelEventArgs) _[/size][/font]
[font=Courier New]                                 [size=2][color=#0000ff]Handles [/color][/size][size=2][color=#0000ff]MyBase[/color][/size][/font][size=2][font=Courier New].Closing[/font][/size]
[font=Courier New][size=2][/size][/font]
[font=Courier New][size=2][color=#0000ff]  If[/color][/size][size=2] childForms.Count = 0 [/size][size=2][color=#0000ff]Then[/color][/size][/font]
[font=Courier New][/font][size=2][font=Courier New]e.Cancel = [/font][/size][size=2][color=#0000ff][font=Courier New]False[/font][/color][/size]
[color=#0000ff][size=2][font=Courier New]Else[/font][/size][/color]
[font=Courier New][/font][size=2][font=Courier New]e.Cancel = [/font][/size][size=2][color=#0000ff][font=Courier New]True[/font][/color][/size]
[font=Courier New][color=#0000ff]    [size=2]Dim[/size][/color][size=2] s [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String[/color][/size][/font]
[font=Courier New][/font][size=2][font=Courier New]s = "Close child forms before exiting!"[/font][/size]
[size=2][font=Courier New]    MessageBox.Show(s, "Unable to Exit", _ [/font][/size]
[size=2][font=Courier New]                 MessageBoxButtons.OK, _[/font][/size]
[size=2][font=Courier New]                 MessageBoxIcon.Exclamation)[/font][/size]
[font=Courier New]    [size=2][color=#0000ff]Dim[/color][/size][size=2] ofrm [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Object[/color][/size][/font]
[font=Courier New][color=#0000ff]    [size=2]Dim[/size][/color][size=2] frm [/size][size=2][color=#0000ff]As[/color][/size][/font][size=2][font=Courier New] Form[/font][/size]
[font=Courier New]    [size=2][color=#0000ff]For[/color][/size][size=2][color=#0000ff]Each[/color][/size][size=2] ofrm [/size][size=2][color=#0000ff]In[/color][/size][/font][size=2][font=Courier New] childForms[/font][/size]
[font=Courier New][size=2]       frm = [/size][size=2][color=#0000ff]DirectCast[/color][/size][/font][size=2][font=Courier New](ofrm, ChildForm)[/font][/size]
[size=2][font=Courier New]       frm.BringToFront()[/font][/size]
[size=2][color=#0000ff][font=Courier New]Next[/font][/color][/size]
[font=Courier New][color=#0000ff]  [size=2]End[/size][/color][size=2][color=#0000ff]If[/color][/size][/font]
[font=Courier New][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Sub[/color][/size][/font]
[font=Courier New][size=2][color=#0000ff]
[/color][/size][/font]When the MainForm closes, it makes sure that childForms is empty. If empty, it closes, if not, it cancels the close and brings all the open child forms to the front

Finally, when ChildForms are instanced, passing MainForms ChildCloseHandler sub as an argument and added to the childForms list:
Visual Basic:
[size=2][color=#0000ff][size=2][color=#0000ff][font=Courier New]Private[/font][/color][/size][font=Courier New][size=2][/size][size=2][color=#0000ff]Sub[/color][/size][size=2][color=#000000] Button1_Click( [/color][/size][size=2][color=#0000ff]ByVal[/color][/size][size=2][color=#000000] sender [/color][/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#000000] System.Object, _ [/color][/size][/font]
[font=Courier New] [size=2][color=#0000ff]ByVal[/color][/size][size=2][color=#000000] e [/color][/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#000000] System.EventArgs) [/color][/size][size=2][color=#0000ff]Handles[/color][/size][/font][size=2][font=Courier New][color=#000000] Button1.Click[/color][/font][/size]
[font=Courier New][size=2]childForms.Add([/size][size=2][color=#0000ff]New[/color][/size][size=2] ChildForm([/size][size=2][color=#0000ff]AddressOf[/color][/size][/font][size=2][font=Courier New] ChildCloseHandler))[/font][/size]
[font=Courier New][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Sub[/color][/size][/font]
[/color][/size]

here it is in c#
MainForm:
PHP:
public class MainForm : System.Windows.Forms.Form
{
 // Extraneous Windows Code Removed
 private ArrayList childForms = new ArrayList();
 private void MainForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
 {
  e.Cancel = childForms.Count != 0 ? true : false; 
  if (e.Cancel)
  {
   string s = "Close child forms before exiting!";
   MessageBox.Show( s, "Unable to Exit", 
	MessageBoxButtons.OK, 
	MessageBoxIcon.Exclamation);
   foreach (object ofrm in childForms)
	(ofrm as ChildForm).BringToFront();
  }
 }
 private void ChildClosed(object sender, EventArgs e)
 {
  if (childForms.Contains(sender)) childForms.Remove(sender);
 }
 private void button1_Click(object sender, System.EventArgs e)
 {
  childForms.Add(new ChildForm(new EventHandler(ChildClosed)));
 }
}

ChildForm:
PHP:
public class ChildForm : System.Windows.Forms.Form
{
 // Extraneous Windows Code Removed
 public ChildForm(EventHandler OnClosed): this()
 {
  Closed += OnClosed;
  Show();
 }
}
 
Back
Top