Scrolling Game Development Kit Forum
Welcome, Guest. Please login or register.
2010-09-08, 07:01:17 PM

Login with username, password and session length
Search:     Advanced search
8920 Posts in 1007 Topics by 168 Members
Latest Member: mogzdogz
* Home Help Search Login Register
+  Scrolling Game Development Kit Forum
|-+  General
| |-+  Off-Topic
| | |-+  How to pass a variable from child to parent form
« previous next »
Pages: [1] Print
Author Topic: How to pass a variable from child to parent form  (Read 288 times)
SmartBoy16
Contributor
Fanatic
**
Posts: 554


Current Project: RANAT: BtT

joelag_2009@hotmail.com
View Profile WWW Email
« 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:
//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 » Logged

SmartBoy16


The RANAT Foundation: Before the Truth http://www.box.net/RANAT-BtT
bluemonkmn
SGDK Author
Administrator
Fanatic
*****
Posts: 2146


2678251 BlueMonkMN@gmail.com
View Profile WWW Email
« Reply #1 on: 2009-12-17, 04:05:19 PM »

Where "I'm betting something goes here", how about you have PlayersName = textBox1.Text?
Logged

TonyX
Fanatic
***
Posts: 353


Games Developed: 4 Developers: 8 || Fans: 28

tonyxna@live.com
View Profile WWW
« 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.  Smiley  educated
Logged

SmartBoy16
Contributor
Fanatic
**
Posts: 554


Current Project: RANAT: BtT

joelag_2009@hotmail.com
View Profile WWW Email
« 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 Smiley
Logged

SmartBoy16


The RANAT Foundation: Before the Truth http://www.box.net/RANAT-BtT
Pages: [1] Print 
« previous next »
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!