Although I did this quick project in Visual Studio 2010, you can take the c# files and put them in 2008 or 2005 if you like, anyway, what is in here, is a form that takes the 4 values you want to store, allows you to add them to a list, shows you the list in a textbox (too lazy to do a datagrid view), and allows you to save them to a binary file, or load them from a binary file. There is the windows forms code to see how it works and the class file independent of the windows form that is the class needed to do the serialization.
What I will also do, is here in this thread post the code for the form (minus the form components) and the code for the serialization class.
FORM CODE
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;
using System.IO;
using Inventory;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace RPGExample
{
public partial class frmInventory : Form
{
List<GameItems> _GameItems = new List<GameItems>();
public frmInventory()
{
InitializeComponent();
}
private void btnAddItem_Click(object sender, EventArgs e)
{
GameItems item = new GameItems();
item.Name = tbName.Text.ToString();
item.Damage = Convert.ToInt32(tbDamage.Text);
item.ElementalDamage = Convert.ToInt32(tbElementalDamage.Text);
item.Defense = Convert.ToInt32(tbDefense.Text);
_GameItems.Add(item);
tbData.Text += "Name: " + item.Name + Environment.NewLine;
tbData.Text += "Damage: " + item.Damage.ToString() + Environment.NewLine;
tbData.Text += "Defense: " + item.Defense.ToString() + Environment.NewLine;
tbData.Text += "Elemental Damage: " + item.ElementalDamage.ToString() + Environment.NewLine;
tbData.Text += Environment.NewLine;
}
private void btnLoadItems_Click(object sender, EventArgs e)
{
LoadItems();
tbData.Text = string.Empty;
foreach (GameItems item in _GameItems)
{
tbData.Text += "Name: " + item.Name + Environment.NewLine;
tbData.Text += "Damage: " + item.Damage.ToString() + Environment.NewLine;
tbData.Text += "Defense: " + item.Defense.ToString() + Environment.NewLine;
tbData.Text += "Elemental Damage: " + item.ElementalDamage.ToString() + Environment.NewLine;
tbData.Text += Environment.NewLine;
}
}
private void frmInventory_Load(object sender, EventArgs e)
{
LoadItems();
}
private void btnSaveItems_Click(object sender, EventArgs e)
{
SaveItems();
}
private void SaveItems()
{
Stream stream = File.Open("GameInfo.inf", FileMode.Create);
BinaryFormatter bformatter = new BinaryFormatter();
bformatter.Serialize(stream, _GameItems);
stream.Close();
}
private void LoadItems()
{
if (File.Exists("GameInfo.inf"))
{
_GameItems.Clear();
Stream stream = File.Open("GameInfo.inf", FileMode.Open);
BinaryFormatter bformatter = new BinaryFormatter();
_GameItems = (List<GameItems>)bformatter.Deserialize(stream);
stream.Close();
}
}
}
}
SERIALIZATION OBJECT CODE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace Inventory
{
[Serializable()]
public class GameItems : ISerializable
{
public string ID;
public string Name;
public int Damage;
public int Defense;
public int ElementalDamage;
public GameItems()
{
ID = new Guid().ToString();
Name = string.Empty;
Damage = 0;
Defense = 0;
ElementalDamage = 0;
}
// Deserialization
public GameItems(SerializationInfo info, StreamingContext ctxt)
{
ID = (String)info.GetValue("ID", typeof(string));
Name = (String)info.GetValue("Name", typeof(string));
Damage = (int)info.GetValue("Damage", typeof(int));
Defense = (int)info.GetValue("Defense", typeof(int));
ElementalDamage = (int)info.GetValue("ElementalDamage", typeof(int));
}
// Serialization
public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
info.AddValue("ID", ID);
info.AddValue("Name", Name);
info.AddValue("Damage", Damage);
info.AddValue("Defense", Defense);
info.AddValue("ElementalDamage", ElementalDamage);
}
}
}
355417–12355–$rpgexample_655.zip (64.6 KB)