Jump to content

  • Log In with Google      Sign In   
  • Create Account

Form Game


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Kheyas   Members   -  Reputation: 179

Like
0Likes
Like

Posted 19 August 2012 - 07:03 AM

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.

Sponsor:

#2 ndssia   Members   -  Reputation: 172

Like
0Likes
Like

Posted 19 August 2012 - 07:33 AM

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.

#3 Kheyas   Members   -  Reputation: 179

Like
0Likes
Like

Posted 19 August 2012 - 10:27 AM

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?

Edited by Kheyas, 19 August 2012 - 11:00 AM.


#4 ndssia   Members   -  Reputation: 172

Like
0Likes
Like

Posted 19 August 2012 - 09:13 PM

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();
}

Edited by ndssia, 19 August 2012 - 09:13 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS