Jump to content
  • Advertisement
Sign in to follow this  
Loendal

Sending data from one form to another

This topic is 3881 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello there, I have two forms that I'm working with. frmMain and frmCreate In frmMain, I have instatiated (I think that's the right term) a Player object, thePlayer. When you go through the menu selections at the top of the screen, you are given an option to choose various creation methods, I've only got the first one done, but hey.. Got to start somewhere, right? :) Anyway... frmCreate has various fields to enter data into and then I have the almighty Accept button. My goal is that when the Accept button his clicked, it would send information back to a method I've created in frmMain to setup the player's stats, closing down frmCreate and turning focus back to frmMain, but now with thePlayer holding the stats the user has chosen. Essentially pulling ints out of the various text boxes on frmCreate and sending that through into the method below (which is in frmMain) public void initPlayerStats(string fn, string ln, int atk, int def, int wpn, int shd) { thePlayer.FName = fn; thePlayer.LName = ln; thePlayer.BaseAttack = atk; thePlayer.BaseDefense = def; thePlayer.BaseWeapon = wpn; thePlayer.BaseShield = shd; } However, frmCreate doesn't seem to see "thePlayer" or "initPlayerStats" at all (They aren't coming up in the drop down menu for auto-completion). I have tried frmMain.initPlayerStats as well, and that doesn't work either. Either I have my "thePlayer" and Init method in the wrong place or I've done them the wrong way. Any ideas? This question brought to you by your latest Beginner programmer, Loendal. :)

Share this post


Link to post
Share on other sites
Advertisement
I've no idea if this is the accepted standard way of doing it, but you could create a class with static members acting as an intermediary between the two forms.

Something like:


public static class Player
{
public static string firstName;
public static string lastName;
public static int baseAttack;
public static int baseDefense;
public static string baseWeapon;
public static string baseShield;
}




Obviously there's no encapsulation in this example since I didn't really add any complexity just to get my point across. Instead of instantiating a player object in this example, you'd simply set "Player.firstName" to the first name that you retrieve from the frmCreate fields, and then access "Player.firstName" from frmMain to obtain the value that you want. You can expand my example to include other things that you see fit, but do you understand how it works?

Share this post


Link to post
Share on other sites
Quote:
Original post by Loendal
However, frmCreate doesn't seem to see "thePlayer" or "initPlayerStats" at all (They aren't coming up in the drop down menu for auto-completion). I have tried frmMain.initPlayerStats as well, and that doesn't work either. Either I have my "thePlayer" and Init method in the wrong place or I've done them the wrong way.

Any ideas?
This question brought to you by your latest Beginner programmer, Loendal. :)


It sounds like thePlayer and whatever else is not "in scope" there. Where are they declared, and how does the code in question know about it?

In other words... post more source (use source tags, also: check the FAQ).

What language are we using here? Is this C# or java?

Share this post


Link to post
Share on other sites
Quote:
Original post by Justin Rebuilt
I've no idea if this is the accepted standard way of doing it, but you could create a class with static members acting as an intermediary between the two forms.

Something like:

*** Source Snippet Removed ***

Instead of instantiating a player object in this example, you'd simply set "Player.firstName" to the first name that you retrieve from the frmCreate fields, and then access "Player.firstName" from frmMain to obtain the value that you want. You can expand my example to include other things that you see fit, but do you understand how it works?


I think so, but Static classes weren't something that we dove too deeply into in the course I took on this a few months back (The old saying is true, if you don't use it, you lose it). So are you suggesting that my Player class should be static or that I should create some new class for this situation?

My eventual goal is to have multiple players at a time, so woudln't that prevent me from using Static? If I want thePlayer and theOtherPlayer, I can't use Static, can I?

Quote:
What language are we using here? Is this C# or java?


This is C#. C# Express 2008, to be more exact.


private void frmMain_Load(object sender, EventArgs e)
{
Player thePlayer = new Player();
}



The initPlayerStats function is just out there in the partial class frmMain : Form section.


public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}

public void initPlayerStats(string fn, string ln, int atk, int def, int wpn, int shd)
{
thePlayer.FName = fn;
thePlayer.LName = ln;
thePlayer.BaseAttack = atk;
thePlayer.BaseDefense = def;
thePlayer.BaseWeapon = wpn;
thePlayer.BaseShield = shd;
}
}

Share this post


Link to post
Share on other sites
Your Player is going out of scope. You need to move the declaration into the class itself. Then "OnLoad" can create the instance.

If you wanted to, you could make thePlayer static and public which would allow you to access it in your other form. A better way to do this would be to setup the other form as a dialog box and test for close. After the dialog box is closed you test to see why, and if it's accept you call a method that can populate thePlayer from the dialog.

I'm a little tired right now, so I'm sorry if this sounds weird, but I'm betting someone else here will understand what I'm getting at and can help you more.

Share this post


Link to post
Share on other sites
I tried redefining my Player object as Static and got a pile of errors. I can't create protected strings inside a Static class, it seems. When I change them to Private, I get another collection of errors. There's 57 different attributes inside my Player object and a collection of get/set methods to adjust them after the Player class gets created, so perhaps need to find another way around this.

I'm going to try and find a way to create a Player object over on frmCreate and then see if I cas pass it back to frmMain somehow. I've tried instancing Player thePlayer = new Player(); in each section of my frmMain class without success yet.

I will be doing some reading up on Static classes, perhaps I'm just thinking of them incorrectly.

Your assorted helps is appriciated, thank you and keep the help coming! ;)

EDIT: After a little bit of Experimentation, I managed to get it to work (Kind of, but if this is the way it has to work, so be it). Firstly, I changed my aPlayer instancing to public and static:
public partial class frmMain : Form
{
public static Player thePlayer = new Player();

public frmMain() blah blah blah blah
}



Meanwhile, over in frmCreate, I did this:
private void btnAccept_Click(object sender, EventArgs e)
{
if (checkNames)
{
lblErrors.Visible = true;
}
else
{
lblErrors.Visible = false;
frmMain.thePlayer.FName = txtFirstName.Text;
frmMain.thePlayer.LName = txtLastName.Text;
frmMain.thePlayer.BaseAttack = Convert.ToInt32(txtAttack.Text);
frmMain.thePlayer.BaseDefense = Convert.ToInt32(txtDefense.Text);
frmMain.thePlayer.BaseShield = Convert.ToInt32(txtShield.Text);
frmMain.thePlayer.BaseWeapon = Convert.ToInt32(txtWeapon.Text);
this.Close();
}
}



When I closed my frmCreate and it went back to frmMain, I hit my Test button and it spat out the new name I had chosen, so I'm assuming all the others came over as well (easily verified, no worries). Thanks for the help folks, I'm sure I'll be back for more later.

Now if there's a more efficient way of doing the above, I'd love to hear it, but since it's working, it's working, right? :)

[Edited by - Loendal on February 7, 2008 2:01:25 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!