?
Solved

How to kill a form

Posted on 2006-11-18
5
Medium Priority
?
212 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 63

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 143

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 70

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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

771 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