5 Replies - 1243 Views - Last Post: 04 January 2013 - 03:25 AM Rate Topic: -----

#1 inkey  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 26-June 12

C# Open form instances how to close.

Posted 04 January 2013 - 01:39 AM

I have a program that i am working on. The user presses some shortcut keys this they opens a form with a list of suppliers in a datagrid View. User clicks them and it passes back across to the original form. The problem with my code is the original form there is 3 instances of it open 3 copies of form.

Code For the shortcut keys to open the list
   if (e.KeyCode == Keys.B && e.Modifiers == Keys.Control)
            {
                OpenCloseForms OpenSupplierList = new OpenCloseForms();
                OpenSupplierList.OpenListSupplierForm();
                OpenSupplierList.CloseSupplierForm();
                
            }


Code for the datagrid view click that passes data to the form
   OpenCloseForms form2 = new OpenCloseForms();
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > 0 && e.ColumnIndex > 0)
            {
                
               form2.OpenSupplierForm();
               string supplierCode1 = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
               form2.SetSupplierCode(supplierCode1);
               
                
                //form2.CloseSupplierForm();
            }
            else MessageBox.Show("Value wrong");
        }

I have tried a few things including this.close(); on the instances of the form with no success. Anyone have any ideas on how i could correct this and point me in he right direction any help at all appropriated.

Is This A Good Question/Topic? 0
  • +

Replies To: C# Open form instances how to close.

#2 Anthonidas  Icon User is offline

  • D.I.C Head

Reputation: 30
  • View blog
  • Posts: 218
  • Joined: 25-April 11

Re: C# Open form instances how to close.

Posted 04 January 2013 - 01:58 AM

do you get any errors when using this.Close()? Did you try with this.Hide()?
Was This Post Helpful? 0
  • +
  • -

#3 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 312
  • View blog
  • Posts: 488
  • Joined: 13-June 12

Re: C# Open form instances how to close.

Posted 04 January 2013 - 02:11 AM

Are you sure you have references to the correct form when using Close()? Remember, it's not by the class name. So, if you had a Form1 object open, you cannot close it with the code Form1 form1 = new Form1(); form1.Close(); because that creates a new one.

If you're going to close a form from another, you need the form that's calling the close method to hold reference: Consider either passing the form as a parameter to the new one, or possibly having a static collection of all forms that you can access from anywhere.
Was This Post Helpful? 1
  • +
  • -

#4 inkey  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 26-June 12

Re: C# Open form instances how to close.

Posted 04 January 2013 - 02:15 AM

View PostAnthonidas, on 04 January 2013 - 01:58 AM, said:

do you get any errors when using this.Close()? Did you try with this.Hide()?

No errors just doesn't close the form.
Was This Post Helpful? 0
  • +
  • -

#5 inkey  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 26-June 12

Re: C# Open form instances how to close.

Posted 04 January 2013 - 03:00 AM

View PostMrShoes, on 04 January 2013 - 02:11 AM, said:

Are you sure you have references to the correct form when using Close()? Remember, it's not by the class name. So, if you had a Form1 object open, you cannot close it with the code Form1 form1 = new Form1(); form1.Close(); because that creates a new one.

If you're going to close a form from another, you need the form that's calling the close method to hold reference: Consider either passing the form as a parameter to the new one, or possibly having a static collection of all forms that you can access from anywhere.


i was thinking it was going to be something to do with how i was creating instances. Have you any links where i can read up on the static collections?
Was This Post Helpful? 0
  • +
  • -

#6 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 312
  • View blog
  • Posts: 488
  • Joined: 13-June 12

Re: C# Open form instances how to close.

Posted 04 January 2013 - 03:25 AM

Well, there's a quick explaination of a static List<T> on dotnetperls you could look at: but it's not really the way I'd go about it. I'd probably do something like this:

StartForm
public partial class StartForm : Form
    {
        public StartForm()
        {
            InitializeComponent();
            buttonOpenOtherForm.Click += new EventHandler(buttonOpenOtherForm_Click);
        }

        void buttonOpenOtherForm_Click(object sender, EventArgs e)
        {
            ClosingForm closingForm = new ClosingForm(this);
            closingForm.Show();
        }
    }


ClosingForm

public partial class ClosingForm : Form
    {
        Form formSentFrom;
        public ClosingForm(Form formToClose)
        {
            InitializeComponent();
            formSentFrom = formToClose;
            this.Activated += new EventHandler(ClosingForm_Activated);
        }

        void ClosingForm_Activated(object sender, EventArgs e)
        {
            formSentFrom.Close();
        }
    }


So, you see, what happens here is that the ClosingForm takes a Form in its constrcutor and stores it in formToClose. When activated, it then closes the form sent from. You'll see that the StartForm sends a reference to itself (using the this keyword) to the constructor of the ClosingForm.

This post has been edited by MrShoes: 04 January 2013 - 03:25 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1