C# parent child

This is a discussion on C# parent child within the C# Programming forums, part of the General Programming Boards category; Hi to all I need your help! I have an assignement i must create an apllication that when opened opens ...

  1. #1
    Registered User BraneMxm's Avatar
    Join Date
    Apr 2007
    Location
    Croatia
    Posts
    20

    C# parent child

    Hi to all I need your help!

    I have an assignement i must create an apllication that when opened opens 3 forms and wenn I click on form1 form1 should be the parent form of thw two other forms.

    I have created 3 forms form1, fomr2,form3, and showed them on load.

    I have two problems:

    1. I dont know how to creatae an function for the event of the left click.
    So i tried it with a button so i can try to set the parent-child relation.
    and then i encotered the second problem:
    2:
    When i say frm1.Owner=this ; then logicaly it says that frm1 doesnt exist because it is out of scope. So i was forced to do Form2 frm2 = new Form2(); and then frm2.Owner = this; but that isnt the same form that i created on load.

    Thank you
    Here is the code:
    Code:
     private void Form1_Load(object sender, EventArgs e)
            {
                Form2 frm2 = new Form2();
                Form3 frm3 = new Form3();
                frm2.Show();
                frm3.Show();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Form2 frm2 = new Form2();
                frm2.Owner = this;
                frm2.Show();
            }
    I have managed my app to show a messagebox on a clock on the form. But i cant set the parent-child relationship because: The name frm2 doesnt exist in the curent context
    Code:
    void Clicked(object sender, EventArgs e)
            {
                MessageBox.Show("a");
                /* frm2.Owner = this;
                frm3.Owner = this;*/
            }
    Last edited by BraneMxm; 06-02-2007 at 05:53 AM. Reason: Update

  2. #2
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109
    Code:
     private void Form1_Load(object sender, EventArgs e)
            {
                Form2 frm2 = new Form2();
                Form3 frm3 = new Form3();
                frm2.Show();
                frm3.Show();
            }
    Instead of doing it like this, make a private field called frm2 and frm3 in the form1 class (declaration). Then in the Form1_load function, you initialise these objects , by doing something like this.frm2 = new Form2();.

    This way you can access these forms troughout the whole form1 class.

  3. #3
    Registered User BraneMxm's Avatar
    Join Date
    Apr 2007
    Location
    Croatia
    Posts
    20

    Is this ok?

    It looks like this now it works but is this what the task is?
    And i am not sure about the question how the ownership of the windows affects their appereance.

    Code:
    namespace vjezba4dio2
    {
    
        public partial class Form1 : Form
        {
            private Form2 frm2 = new Form2();
            private Form3 frm3 = new Form3();
            
            public Form1()
            {
                InitializeComponent();
                this.Click += new EventHandler(this.Clicked);
                
                frm2.Show();
                frm3.Show();
                
            }
            
            private void Form1_Load(object sender, EventArgs e)
            {                     
            }
    
            void Clicked(object sender, EventArgs e)
            {
                frm2.Owner = this;
                frm3.Owner = this;
    
            }
           
            
            
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. inter process communcation, parent - child
    By tallan in forum C Programming
    Replies: 5
    Last Post: 02-28-2009, 04:04 AM
  2. Replies: 3
    Last Post: 10-15-2008, 10:24 AM
  3. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 08:53 AM
  4. pipe
    By smart girl in forum C Programming
    Replies: 4
    Last Post: 04-30-2006, 10:17 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM

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