Author Topic: How to pass a variable from child to parent form  (Read 4715 times)

SmartBoy16

  • Contributor
  • Fanatic
  • **
  • Posts: 587
  • Looking for inspiration.....
    • View Profile
    • Email
How to pass a variable from child to parent form
« on: 2009-12-16, 05:26:10 AM »
Hey you guys, i'm having trouble with this. this is for a college assignment.

I'm making a "Jeopardy" game for my final in a programming class and i having trouble passing a variable from the child from to the parent form. so far i got:
Code: [Select]
//parent form

public partial class ParentForm : Form
    {
        string playersname;
        public ParentForm(string strParam) //i have it set up to set this as "Enter Name Here"
        {
            InitializeComponent();
            if (strParam == "Enter name here")
            {
                Form Child1 = new Child1(strParam);
                Child1.ShowDialog();
                playersname = Child1.PlayersName;
                label1.Text = playersname;
            }
        }
//the rest of the code
}


//child form


public partial class Child1 : Form
    {
        public string PlayersName
        {
            get;
        }

        public Form1(string strParam)
        {
            InitializeComponent();
            textBox1.Text = strParam;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //i'm betting that something goes here
            this.Close();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            SetPlayersName(textBox1.Text);
        }

        private void textBox1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "Enter name here")
            {
                textBox1.Text = "";
            }
            else
            {
                textBox1.SelectAll();
            }
        }

        public string SetPlayersName(string Name)
        {
            Name = PlayersName;
            return PlayersName;
        }
    }


the child form contains a Name textbox and a submit button (which closes the window)

this is all done in Visual Studio 2008
« Last Edit: 2009-12-16, 05:32:00 AM by SmartBoy16 »
Looking to the skies.....

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: How to pass a variable from child to parent form
« Reply #1 on: 2009-12-17, 04:05:19 PM »
Where "I'm betting something goes here", how about you have PlayersName = textBox1.Text?

tprime

  • Fanatic
  • ***
  • Posts: 395
    • View Profile
    • Email
Re: How to pass a variable from child to parent form
« Reply #2 on: 2009-12-17, 10:57:22 PM »
I posted on another post, but I think I might have to bump this up, since it seems urgent.  :)  :educated:

SmartBoy16

  • Contributor
  • Fanatic
  • **
  • Posts: 587
  • Looking for inspiration.....
    • View Profile
    • Email
Re: How to pass a variable from child to parent form
« Reply #3 on: 2009-12-18, 04:08:14 AM »
@ bluemonk, I figured this out a while ago, but thanks for the tip.

i might let you know how this turns out. i got the game to the point where you can answer the questions, but i still have to make the questions and, if i have time, put some "pretty" effects on it. let ssee how it turns out :)
Looking to the skies.....