C Board  

Go Back   C Board > General Programming Boards > C# Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 06-04-2009, 08:54 AM   #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
geek74 is offline   Reply With Quote
Old 06-04-2009, 09:35 AM   #2
Registered User
 
Join Date: Mar 2009
Location: england
Posts: 76
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);
}
theoobe is offline   Reply With Quote
Old 06-04-2009, 09:38 AM   #3
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,362
Posts moved to a new thread.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 06-04-2009, 10:54 AM   #4
Confused
 
Magos's Avatar
 
Join Date: Sep 2001
Location: Sweden
Posts: 3,122
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.
Magos is offline   Reply With Quote
Old 06-05-2009, 01:53 PM   #5
Registered User
 
Join Date: Mar 2009
Location: england
Posts: 76
Quote:
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
        }
    }

}
theoobe is offline   Reply With Quote
Old 10-15-2009, 03:17 PM   #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
DrGuitar is offline   Reply With Quote
Old 10-16-2009, 06:43 PM   #7
Registered User
 
valaris's Avatar
 
Join Date: Jun 2008
Location: RING 0
Posts: 462
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.
valaris is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Good introduction to Windows Forms? jcafaro10 C# Programming 1 05-19-2009 06:11 PM
insert differnt forms in a single form under different tabs? and a textBox question arian C# Programming 2 04-02-2009 04:40 AM
Httpd forms with? |HBO| Networking/Device Communication 1 02-22-2008 11:45 PM
C# Game using Forms ejohns85 C# Programming 1 12-16-2006 05:35 PM
Which one :MFC, Win32, Windows Forms (.Net)? Robert_Sitter Windows Programming 6 11-17-2005 06:15 AM


All times are GMT -6. The time now is 05:14 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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