10 min solution:
Ok, following is out of 5 mins 'quick-effort' what I really meant; I hope this would solve the problem.
- STEP 1: Get a canvas - Canvas object
- STEP 2: Add Drawings/controls to it
- STEP 3: Serialize, save, and reload the object
See following.
STEP 1: The canvas class
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Binary;
namespace SaveControls
{
[Serializable()]
public class CCanvas
{
List<CDrawing> _listControls;
public List<CDrawing> Controls
{
get { return _listControls; }
}
public CCanvas()
{
_listControls = new List<CDrawing>();
}
public void AddControls(CDrawing theControls)
{
_listControls.Add(theControls);
}
public void ReloadControl(Form frm)
{
//foreach (CDrawing draw in _listControls) -- requires IEnumerable implementation.
for (int i = 0; i < _listControls.Count; i++)
{
CDrawing d = (CDrawing)_listControls[i];
d.Draw(frm);
}
}
public void Save()
{
try
{
using (Stream stream = File.Open("data.bin", FileMode.Create))
{
BinaryFormatter bin = new BinaryFormatter();
bin.Serialize(stream, this);
}
}
catch (IOException)
{
}
}
public CCanvas Open()
{
CCanvas LoadedObj = null;
using (Stream stream = File.Open("data.bin", FileMode.Open))
{
BinaryFormatter bin = new BinaryFormatter();
LoadedObj = (CCanvas)bin.Deserialize(stream);
}
return LoadedObj;
}
}
}
STEP 2: Add drawings
using System;
using System.Collections.Generic;
using System.Text;
using System;
using System.Data;
using System.Collections.Generic;
using System.Collections;
using System.IO;
using System.Xml.Serialization;
using System.Windows.Forms;
using System.Drawing;
namespace SaveControls
{
[Serializable()]
public class CDrawing
{
public enum ControlTypes { Label, TextBox, None };
private ControlTypes _controlType;
public ControlTypes ControlType
{ get { return _controlType; } }
private string _strControlText;
public string Text
{ get { return _strControlText; } }
private int _xPosition;
public int X
{ get { return _xPosition; } }
private int _yPosition;
public int Y
{ get { return _yPosition; } }
private string _strFontName;
public string Font
{ get { return _strFontName; } }
double _fFontSize;
public double Size
{ get { return _fFontSize; } }
string _strStyle;
public string Style
{ get { return _strStyle; } }
decimal _dForegroundColor;
public decimal Color
{ get { return _dForegroundColor; } }
public CDrawing(ControlTypes controlType, string strControlText, int xPosition, int yPosition,
string strFontName, double fFontSize, string strStyle, decimal dForegroundColor)
{
_controlType = controlType;
_strControlText = strControlText;
_xPosition = xPosition;
_yPosition = yPosition;
_strFontName = strFontName;
_fFontSize = fFontSize;
_strStyle = strStyle;
_dForegroundColor = dForegroundColor;
}
public void Draw(Form frm)
{
if (_controlType == ControlTypes.Label)
{
Label lbl = new Label();
lbl.Text = _strControlText;
lbl.Location = new Point(_xPosition, _yPosition);
System.Drawing.FontStyle fs = (_strStyle == System.Drawing.FontStyle.Regular.ToString()) ? System.Drawing.FontStyle.Regular : System.Drawing.FontStyle.Bold;
lbl.Font = new System.Drawing.Font(_strFontName, (float)_fFontSize, fs);
lbl.ForeColor = SystemColors.Control;// _dForegroundColor;
lbl.Visible = true;
frm.Controls.Add(lbl);
}
}
}
}
STEP 3: Usage, serialize, save, reload
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void Save()
{
//Create a canvas object
CCanvas Canvas1 = new CCanvas();
//Add controls
Canvas1.AddControls(new CDrawing(CDrawing.ControlTypes.Label, "This is text on a label1", 10, 100, "Tahoma", 7.5, "Regular", -778225617));
Canvas1.AddControls(new CDrawing(CDrawing.ControlTypes.Label, "This is text on a label11", 20, 200, "Verdana", 7.5, "Regular", -778225617));
Canvas1.AddControls(new CDrawing(CDrawing.ControlTypes.Label, "This is text on a label111", 30, 300, "Times New Roman", 7.5, "Regular", -778225617));
//Save the object
Canvas1.Save();
}
private void button1_Click(object sender, EventArgs e)
{
Save();
}
private void btnLoad_Click(object sender, EventArgs e)
{
Load();
}
public void Load()
{
//Retrieve
CCanvas Canvas2 = new CCanvas();
//opens the binary file
Canvas2 = Canvas2.Open();
//loads control to this form.
Canvas2.ReloadControl(this);
}
}
Let us know about the reason, if you plan to hate this solution. Meanwhile I am trying to look for someplace to upload. Googlecode, but I dont have subversion client installed with me. :0(