Form Game

Started by
2 comments, last by ndssia 11 years, 8 months ago
Hello, I posted here today because i need some help with forms in c#.
What i'm trying to do is a simple rpg with forms. Like buttons for: Arena, Inventory, shop etc.
The biggest problem is i have no ideea how to send data from a form to another. Let's say I have MainForm and CreateForm.
On the MainForm I have the button "Create new character". I click it, the new form appears. I have to put a name and select a class. When the user clicks submit i must send data to the MainForm or just another form, like the GameForm.
I need someone to make a very clear example of what I must do. I'm not that advanced in programming.
Advertisement
Depending on the complexity/amount of data you wish to send through, you can either do something like:

[From CreateForm]
MainForm.InitializeGame(String name, PlayerClass class, int difficultyLevel() {
...
}

Or you can pass through an entire data structure if you need to.
That's the problem. I can't just write MainForm.Methodname . It won't let me. It's like non existent for the CreateForm. I'll create a clean project soon to show you my problem
--Main Form
[source lang="csharp"]using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace FormGame
{
public partial class MainForm : Form
{
Player player = new Player();
public MainForm()
{
InitializeComponent();
}
private void btn_Create_Click(object sender, EventArgs e)
{
CreateForm cForm = new CreateForm();
cForm.ShowDialog();
}
public void CreatePlayer(string Name, int Health)
{
player.Initialize(Name, Health);
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(player.Name);
}
}
}[/source]
--Create Form
[source lang="csharp"]using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FormGame
{
public partial class CreateForm : Form
{
public CreateForm()
{
InitializeComponent();
}
private void btn_Submit_Click(object sender, EventArgs e)
{
MainForm.CreatePlayer(tb_Name.Text, 100);
this.Close();
}
}
}[/source]
Following error shows up: "Error 1 An object reference is required for the non-static field, method, or property 'FormGame.MainForm.CreatePlayer(string, int)' C:\Users\Kheyas\AppData\Local\Temporary Projects\FormGame\CreateForm.cs 20 13 FormGame"

Maybe not the best organization but... I still don't know what the problem is... Maybe the acces modifiers?
Woops,

If your MainForm is also the form that contains your GameElements, you can either pass through a reference to that form to CreateForm, and call it as a non-static method there, or you can make MainForm a singleton, and call it that way.

Creating a seperate form for your game is better however, and you could do something like:

// CreateForm.cs
Game.NewGame(options);

// Game.cs
public static void NewGame(Options options) {
var gameForm = new GameForm();
gameForm.Initialize(options); // Can't remember if you can have a custom constructor for forms...
gameForm.Show();
}

This topic is closed to new replies.

Advertisement