#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    36
    Rep Power
    11

    launching a child form ?


    i have 2 forms .. my main application form, and another search form. ( frmSearch.cs )

    I would like to have the search form pop up when the search button is clicked, but I have no idea how.

    searching the net hasn't done much good.

    Please advise or point me in the right direction on utilizing multiple forms in a project.

    Thanks!
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    3
    Rep Power
    0
    Its fairly simple, (however communicating between the two in less pretty), just use

    Form2 a = new Form2();
    a.ShowDialog(this); //to be a dependant


    Regards,
    John
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    36
    Rep Power
    11
    thanks for the reply .. and yes, i've discovered the 'less pretty' part. i'm trying to figure out delegates and other ways of doing this now.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    109
    Rep Power
    12
    The BEST way is simply to pass a reference from the parent form through the child’s constructor then make any variable you want to be able to access from either from a public property or function. It's very very rare that you'll ever need delegates, though they are useful for other things.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    36
    Rep Power
    11
    hah! that is pretty cool and simple =)

    how about once the new form is open .. and you want to send variables back to the original form that it was launched from?

    the original form already exists .. that confuses me.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    109
    Rep Power
    12
    I don't think you quiite follow me, maybe this will help:

    Code:
    // Main Form
    
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    
    namespace App.Forms
    {
    	/// <summary>
    	/// Summary description for frmAbout.
    	/// </summary>
    	public class frmParentClass : System.Windows.Forms.Form
    	{
    		private frmSubClass MySubClass;
    
    		// Variable in ParentClass form you want access to.
    		public String Name
    		{
    			get
    			{
    				return Name;
    			}
    			set
    			{
    				Name = value;
    			}
    		}
    
    		public frmParentClass()
    		{
    			InitializeComponent();
    
    			MySubClass = new frmSubClass(this);
    		}
    
    		private int GetSubClassAge()
    		{
    			return MySubClass.Age;
    		}
    
    		private void SetSubClassAge(int Age)
    		{
    			MySubClass.Age = Age;
    		}
    	}
    }
    Code:
     
    // Sub Class
    
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    
    namespace App.Forms
    {
    	public class frmSubClass : System.Windows.Forms.Form
    	{
    		private frmParentClass MyParentClass;
    
    		public int Age
    		{
    			get
    			{
    				return Age
    			}
    			set
    			{
    				Age = value;
    			}
    		}
    
    		public frmSubClass(frmParentClass MyParentClass)
    		{
    			this.MyParentClass = MyParentClass;
    		}
    
    		private String GetParentClassName()
    		{
    			return MyParentClass.Name;
    		}
    
    		private void SetParentClassName(String Name)
    		{
    			MyParentClass.Name = Name;
    		}
    	}
    }
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    36
    Rep Power
    11
    thank you nem. that is a big help. i didn't know you had to re-define the parent class in the subform.

    so this would make it so that i could access any variable from eithe form with [form name].[variable]

    i think. i will work with this today.

    thanks!
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Brisbane, Australia
    Posts
    50
    Rep Power
    11
    Originally posted by sde
    hah! that is pretty cool and simple =)

    how about once the new form is open .. and you want to send variables back to the original form that it was launched from?

    the original form already exists .. that confuses me.
    you can just access properties of your 2nd form with out pointing objects to each other, as long as yoru 2nd form object is not disposed (after closed, the object is still alive, you can still access it's properties and fucntions)

    frmSearch a = new frmSearch();
    a.ShowDialog(this);

    // since you have called ShowDialog,
    // 'a' will be displayed as a modal dialog
    // so when you reach next line, dialog 'a' should be closed,
    // but not disposed

    this.Search(a.SearchingString);
    however, if you are trying to write something like the find dialog from notepad, that you want to keep the 2nd form as an open form while your main form searches for matches, you should use Nem's method.

IMN logo majestic logo threadwatch logo seochat tools logo