473,473 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Refreshing a parent form

9 New Member
Hi,

I'm a new programmer, and have a small problem. I've been trying to get a form populated with lots of buttons and tabs to be refreshed when its child form is closed. The routine I have is this:

Expand|Select|Wrap|Line Numbers
  1. NewForm_View show_it = new NewForm_View();
  2. show_it.ShowDialog();
  3. Form.ActiveForm.Refresh();
If I understand correctly, the parent form code should pause until NewForm_View is Closed (or should it be Disposed?), and when it does it should be refreshed and the buttons updated - but it isn't. Do I need to run an Invoke routine?

*edit: Just to be clear, the buttons are populated within the parent form's private void Parent_form_Load(object sender, EventArgs e) command.

Hope you can help, many thanks, Howard Parker
Feb 13 '12 #1
9 13031
GaryTexmo
1,501 Recognized Expert Top Contributor
All the Refresh method does is invalidate the form it was called on. This triggers a new WM_PAINT event, which in turn causes the form to immediately redraw.

If you're gathering information from a dialog and want to populate your main form with it, you will need to do that yourself. You can do that one of two ways... you can have the dialog form update the calling form itself, or you can simply update the calling form after you finish calling the dialog (likely preferable). For example, you might have this hypothetical situation...

Expand|Select|Wrap|Line Numbers
  1. // Inside a class called MainForm
  2. // Assuming a TextBox on the form called m_tbUserName
  3. private Hashtable m_userDB = new Hashtable();
  4.  
  5. private void MainForm()
  6. {
  7.   m_userDB.Add("admin", "12345");
  8. }
  9.  
  10. private void LogInButton_Click(object sender, EventArgs e)
  11. {
  12.   LoginForm loginForm = new LoginForm();
  13.   if (loginForm.ShowDialog() != DialogResult.Ok)
  14.   {
  15.     MessageBox.Show("Login attempt was cancelled!");
  16.     // Do whatever
  17.   }
  18.   else
  19.   {
  20.     if (m_userDB.ContainsKey(loginForm.UserName))
  21.     {
  22.       if (m_userDB[loginForm.UserName].ToString() == loginForm.Password)
  23.       {
  24.         m_tbUserName.Text = loginForm.UserName;
  25.         MessageBox.Show(string.Format("Welcome, {0}!", loginForm.UserName), "Login Successful!");
  26.         // Do whatever
  27.       }
  28.       else
  29.       {
  30.         MessageBox.Show("Password Incorrect!");
  31.         // Do whatever
  32.       }
  33.     }
  34.     else
  35.     {
  36.       MessageBox.Show("Unknown user!");
  37.       // Do whatever.
  38.     }
  39.   }
  40. }
Then you had a class called LoginForm that allowed you to input a user name and a password. You would then want the following bit of code in there...

Expand|Select|Wrap|Line Numbers
  1. // Assumes textboxes exist named m_tbUserName and m_tbPassword
  2. private string m_userName = "";
  3. private string m_password = "";
  4.  
  5. public string UserName { get { return m_userName; } }
  6. public string Password { get { return m_password; } }
  7.  
  8. public void LoginForm_FormClosing(...)
  9. {
  10.   m_userName = m_tbUserName.Text;
  11.   m_password = m_tbPassword.Text;
  12. }
Note, it's important that you store the values of your dialog form's options in member variables and expose them with read only access in the FormClosing event. This is because when you close the dialog all the controls on it will dispose and you will be unable to access their values. This is why you couldn't simply do...

Expand|Select|Wrap|Line Numbers
  1. public String UserName { get { return m_tbUserName.Text; } }
Hopefully that helps you out :)
Feb 13 '12 #2
MrFlabulous
9 New Member
Hi, thanks. I'm not sure this is what I want, as I say I'm rather new to this and I don't want a messagebox particularly, I want the entire parent form to be refreshed, redrawn, reloaded, whatever it is.

Basically, if the child form changes a parameter from "X" to "Y", when that child form is closed I want the parent to express that change directly, not via a messagebox. I have the code in place to have the parent filled properly when it's first loaded, I just don't know how to update it in realtime.

Again, thanks, and I'm sorry if I'm being a bit vague and woolly.

Howard
Feb 14 '12 #3
GaryTexmo
1,501 Recognized Expert Top Contributor
Those message boxes were just there for the example to do user feedback, and that example I made up completely. It has nothing to do with anything other than to show you how to access a property from a child form.

In the code you originally posted, your opened your child form with the ShowDialog. This means your child form is modal; that is, you will be unable to interact with any other form until you address the modal form. Since you're calling it in this way, you know the method in which you called the child form is waiting for the child form to complete. So you can do a simple check on the return result from ShowDialog and then update your main form accordingly. See line 24 of the example...

I've detected that the credentials are acceptable and the I update the text property of a control on the main form, a textbox with the name of m_tbUserName with the string value of my child form. Typically, your child form doesn't have access to the parent form. It only collects information and then the parent updates itself based on what was set on the child.

Additionally, if you have code to update the parent properly when it's first loaded, can you not just call this after your child form returns?

Anyway, give my example another look over and try to implement that code yourself in a sample project to see how it works. If it's still not what you're looking for and I'm not understanding you correctly (for which I apologize!) please post back with more of your own code so I can better get a feel for what you're trying to do.
Feb 14 '12 #4
MrFlabulous
9 New Member
Hi Gary, thanks once again. I think I understand now, so I'll give this a try.

Thanks for your time, I'll let you know how I get on.

Howard
Feb 14 '12 #5
MrFlabulous
9 New Member
Hi Gary, I've tried to re-run the populating routine separately but I've come up with another problem, still related. The issue may be due to drawing the form in visual c# then writing the code separately. The code I have starts out as:

Expand|Select|Wrap|Line Numbers
  1.  private void ParentForm_Load(object sender, EventArgs e)
  2.         {
  3.             int ButtonCount = 0; // a counter, nothing more
  4.             Routines.BuildCurrentDatabase(); // Calls the data which will populate the Controls
  5.             foreach (Control cages in T7_Layout.Controls)
  6.             {
  7.                 if (cages is Button)
  8.                 { //text added, colours changed, etc
  9.                 }
  10.             ButtonCount++
  11.        }
  12.  
The issue appears to be that I cannot put this code outside the ParentForm_Load() method. And since Refresh() will not work I cannot get the form to update once the child ShowDialog() has completed. I've changed it to Show() instead but still nothing. Can I physically run ParentForm_Load() again or should I close ParentForm and re-open it from another form?

Sorry to be a pain, but this has been niggling me for some time now.

Thanks

Howard
Feb 15 '12 #6
GaryTexmo
1,501 Recognized Expert Top Contributor
I suppose you could call ParentForm_Load again, just give it a sender and new EventArgs. You could even give it a null sender since you don't appear to be using it...

That said, you mentioned you can't put this code anywhere else? How did you do that... did you put it in another method or just copy/paste it? What errors did you get? You're going to need to provide more information before I can help you, sorry.

Also, is this code a copy/paste from your program? There's two things I'm wondering about...
1) I don't see a close brace for your foreach loop.
2) You check to see if a control is a button and then do specific stuff, but you increment ButtonCount on every iteration of the foreach loop. Did you mean to have ButtonCount++ inside the if block?

Finally, I don't see anything funny about this code outright... but things might be going wrong in your Routines.BuildCurrentDatabase call? Post some more details... more code might be helpful, as well as any errors that are generated when you call this code again.
Feb 15 '12 #7
MrFlabulous
9 New Member
Hi, yeah, sorry it's abridged, just to give you a feel. Foreach is closed, and the counter is inside the block. Instead of tidying it up I've made it more confusing!

So: By moving the code to a separate method I can only call it from ParentForm_Load() rather than another class, unless I make it a public static void method. And when I make it static, I get the message:

Error 3 An object reference is required for the non-static field, method, or property 'dry_run_1.ParentForm.T7_Layout' C:\Users\howpar\Documents\Visual Studio 2010\Projects\dry run 1\dry run 1\ParentForm.cs 392 39 dry run 1

T7_Layout becomes inaccessible, but then if I remove the static command then the method isn't available anywhere else in the code. I guess I shoudl go with your suggestion of calling ParentForm_Load again.

I think Routines.BuildCurrentDatabase is kosher, I've used it across several other forms and methods with no problems.

Thanks

Howard
Feb 15 '12 #8
GaryTexmo
1,501 Recognized Expert Top Contributor
You could make it a public, non-static method, but here's a better question... why does any form except your main form need this? Your main form is the one triggering the dialog, right? It should just be able to update itself with the dialog information when the ShowDialog method returns.
Feb 15 '12 #9
MrFlabulous
9 New Member
Hi Gary - this makes more sense now, and (perhaps unsurprisingly) everything now works perfectly! Many, many thanks!

Howard
Feb 20 '12 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Omar Llanos | last post by:
I have Form1 and Form2 (which is inherited from Form1), and I created a button in Form2 that will fill up a textbox in Form1. What code would do that? I tried the simplest way: //from child...
4
by: Thorsten Ottosen | last post by:
Dear all, Is it possible to close or hide a parent form? In particular, is it possible to do from the child form itself? Thanks Thorsten
1
by: Sylvain | last post by:
Hi, I am developping a Visual C++ application. In my application, I created a Mdi parent form with a main menu. I also created a "menuItem" click to display a Mdi child form. I want in this case...
2
by: Paul | last post by:
Hi this is related to a previous post, hopefully just a bit clearer description o the problem. I have a parent form that opens a new form (child form) while still leaving the parent form open....
2
by: Mike L | last post by:
The child form can be dragged out of the Parent form. I set the child form to IsMdiContainer = False and the Parent Form IsMdiContainer = True. I also coded in the Parent Form on load, Dim f As...
4
by: eBob.com | last post by:
I have a "parent" form (if that's the right terminology), Form1. I declare two public values in the parent form : Public Class Form1 Inherits System.Windows.Forms.Form Public CurDir As String =...
2
by: dynamictiger | last post by:
I am building a wizard and have an issue with one sub form. Whilst all others are about 5 fields tall this one is 15 fields. My choices are either to resize the parent form ridiculously large for...
1
by: nupuragr82 | last post by:
I have a parent form and on button click I am calling a child page where i have a textbox and a button. On button click of child form I am passing the value of the Textbox to the Textbox in parent...
2
by: Jeff | last post by:
I have a Form parent window with a list view on it. When I click on an item I open another form to edit the details - this is not and cannot be a dialog window. When I close the child window I...
3
by: gsuns82 | last post by:
Hi all, I am using modal window for some update purpose, the issue i am facing is,i am not able to refresh parent after closing modal window. The code i used: ...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.