Thread: Updating forms

  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
    28,413
    Posts moved to a new thread.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 11:45 PM
  4. C# Game using Forms
    By ejohns85 in forum C# Programming
    Replies: 1
    Last Post: 12-16-2006, 05:35 PM
  5. Which one :MFC, Win32, Windows Forms (.Net)?
    By Robert_Sitter in forum Windows Programming
    Replies: 6
    Last Post: 11-17-2005, 06:15 AM