Updating forms

This is a discussion on Updating forms within the C# Programming forums, part of the General Programming Boards category; I have two forms one of them I use it to show the contents of a data base in a ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    1

    Updating forms

    I have two forms one of them I use it to show the contents of a data base in a DataGridView , the other one do I use it to add , to edit and to eliminate the data that I show in the first form , my question is , who I do than when you close second form update the data of the first form

  2. #2
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    Geek74, I'm confused as to how your question relates to this thread.

    Anyway, you've not given much details of your project, therefore I will assume that your main form class is called Form1, and that your form which you want to close is called Form2.

    First thing to do is create the event FormClosing in your Form2:
    Code:
    protected override void OnFormClosing(FormClosingEventArgs e)
    {
        base.OnFormClosing(e);
    }
    Next have a method in Form1 which, when called, will perform the updates you require:
    Code:
    public void UpdateData()
    {
        // do update stuff here
    }
    Lastly, go back to your FormClosing event in Form2, and call the UpdateData method from there:
    Code:
    protected override void OnFormClosing(FormClosingEventArgs e)
    {
        ((Form1)this.Parent).UpdateData();
        base.OnFormClosing(e);
    }

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,409
    Posts moved to a new thread.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    If the subform is a dialog (modal form) then you could do this:
    Form1 - Main form
    Form2 - Sub form (dialog)
    Code:
    //This code is called when you want to open the sub form
    
    //Create the sub form
    var Dialog = new Form2();
    
    //Show the sub form
    if(Dialog.ShowDialog() == DialogResult.Ok)
    {
      //If the user pressed Ok, update the data
      //(don't update if he pressed Cancel or closed it)
      UpdateData();
    }
    You generally should avoid dependencies, like touching Form1 from Form2.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    You generally should avoid dependencies, like touching Form1 from Form2.
    OK, while taking Magos' point into consideration, it's still possible to have an event based solution:

    Code:
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            private Form2 f2;
    
            public Form1()
            {
                InitializeComponent();
                this.f2 = new Form2();
                this.f2.FormClosing += new FormClosingEventHandler(this.Form2Closing);
                this.f2.Show();
            }
    
            private void Form2Closing(object sender, FormClosingEventArgs e)
            {
                // do update stuff here
            }
        }
    
    }

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    1
    Is there a way to add the OnformClosing event through th UI instead of writing the code

  7. #7
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Open the form in editor. Look at your properties window, click on the lighting bolt (events), find your event, type an event name, select a pre-existing one, or double click to have it generate one for you.

    Again I would handle updates how magos suggested. Handle input verification on the form.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Good introduction to Windows Forms?
    By jcafaro10 in forum C# Programming
    Replies: 1
    Last Post: 05-19-2009, 06:11 PM
  2. Replies: 2
    Last Post: 04-02-2009, 04:40 AM
  3. Httpd forms with?
    By |HBO| in forum Networking/Device Communication
    Replies: 1
    Last Post: 02-22-2008, 10:45 PM
  4. C# Game using Forms
    By ejohns85 in forum C# Programming
    Replies: 1
    Last Post: 12-16-2006, 04:35 PM
  5. Which one :MFC, Win32, Windows Forms (.Net)?
    By Robert_Sitter in forum Windows Programming
    Replies: 6
    Last Post: 11-17-2005, 05:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21