Solved

How to kill a form

Posted on 2006-11-18
5
205 Views
Last Modified: 2010-04-16
HI,
If i have a couple of forms open and i would like them to close when the user clicks on a button on the main form, what the best way to do this?
0
Comment
Question by:schenkp
5 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 17971890
On the main form keep a reference to the forms you wish to close then when the user clicks on the button do ReferenceToForm.Close(); Where the ReferenceToForm is the variable name used to reference the form you created.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17971904
vb.net 2003 does not seem to have a forms collection like vb6, while vb.net 2005 does have it:
http://www.dotnet247.com/247reference/msgs/21/107856.aspx
http://www.vbforums.com/showthread.php?t=387971

in depending on the version you are, you will have to manage all your forms, or be able to use
My.Application.OpenForms

0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 17971961
Hi schenkp,

Are your forms children? In this case, you can go through the MDIChildren collection:
        For Each frmChild As Form In Me.MdiChildren
            frmChild.Close()
        Next

Cheers!
0
 

Author Comment

by:schenkp
ID: 17972712
Cant get this to work, here is the code i am using, its get launched when a user selects the menu option...

                EC45 ec4 = new EC45();
                ec4.Show();
                ec4.PlayMedia(@"C:\\music.mp3");
                ec4.Owner = this;

There is a timer in the program and when it counts down to zero I am trying to close EC45 form and its not working?
0
 
LVL 20

Accepted Solution

by:
Daniel Van Der Werken earned 500 total points
ID: 17975169
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        Form2 fm2;  <== the key is to declare the form outside of the function
                                   =================================

        public Form1()
        {
            InitializeComponent();
            comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;
            comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;

            comboBox1.DrawMode = DrawMode.OwnerDrawVariable;
            comboBox1.Items.Add("Hello World");
            comboBox1.Items.Add("Wee Willy Wanka");
            comboBox1.Items.Add("Tiffany Aiken");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            fm2 = new Form2();
            fm2.Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            fm2.Close();
        }
    }
}
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C# TextBox 11 30
Memory Usage 2 50
Class object 2 27
Where can I ask about my ASP.NET MVC project? 6 23
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now