Click here to Skip to main content
15,917,059 members
Home / Discussions / C#
   

C#

 
GeneralRe: How to show a bookmarked position in default browser? Pin
mav.northwind10-Jul-07 3:34
mav.northwind10-Jul-07 3:34 
GeneralRe: How to show a bookmarked position in default browser? Pin
Luc Pattyn10-Jul-07 4:00
sitebuilderLuc Pattyn10-Jul-07 4:00 
GeneralRe: How to show a bookmarked position in default browser? Pin
mav.northwind10-Jul-07 4:24
mav.northwind10-Jul-07 4:24 
QuestionGeneric parameter/reflection problem Pin
Bekjong10-Jul-07 1:46
Bekjong10-Jul-07 1:46 
AnswerRe: Generic parameter/reflection problem Pin
Pete O'Hanlon10-Jul-07 1:59
mvePete O'Hanlon10-Jul-07 1:59 
GeneralRe: Generic parameter/reflection problem Pin
Bekjong10-Jul-07 2:08
Bekjong10-Jul-07 2:08 
GeneralRe: Generic parameter/reflection problem Pin
Pete O'Hanlon10-Jul-07 2:32
mvePete O'Hanlon10-Jul-07 2:32 
GeneralRe: Generic parameter/reflection problem Pin
Bekjong10-Jul-07 2:41
Bekjong10-Jul-07 2:41 
The thing is, I'm working in a MDI application and want to store and retrieve all of the forms that were open when the application last closed. Here's my code, maybe not so pretty, but I think you'll get it...


using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Drawing;
using System.Diagnostics;

namespace Tools
{
[Serializable]
public class FormsPool
{
/// <summary>
/// This dictionary will store the form's properties that we want to save to file.
/// </summary>
private Dictionary<string, FormData> dataPool = new Dictionary<string, FormData>();

/// <summary>
/// And this one is used to store forms at runtime.
/// </summary>
[field: NonSerialized]
private Dictionary<string, Form> formPool = new Dictionary<string, Form>();

/// <summary>
/// Get the form with the specified id.
/// </summary>
/// <param name="form_id">Unique string identifying the form to get.</param>
/// <returns></returns>
public Form GetForm(string form_id)
{
return GetForm<Form>(form_id);
}

/// <summary>
/// Returns the form with the given id from the pool. If it's not there, it'll be constructed.
/// </summary>
/// <typeparam name="formType">The form class you want to be returned.</typeparam>
/// <param name="form_id">The id for the form to return.</param>
/// <returns></returns>
public formType GetForm<formType>(string form_id) where formType : Form
{
if (formPool == null) // make sure our dictionaries are there
formPool = new Dictionary<string, Form>(); // (deserialization can set them to be null)

if (dataPool == null)
dataPool = new Dictionary<string, FormData>();

if (this.formPool.ContainsKey(form_id)) // check out if we have a reference to the form
{
if (formPool[form_id] != null)
return formPool[form_id] as formType; // if so, return it
else formPool.Remove(form_id);
}

formType ret = Activator.CreateInstance<formType>(); // not there, so create it

if (dataPool.ContainsKey(form_id)) // check for past references to this form
{ // and get the data used in that case
ret.StartPosition = FormStartPosition.Manual;
ret.Size = dataPool[form_id].size;
ret.Location = dataPool[form_id].location;
this.formPool.Add(form_id, ret);
}
else // otherwise, reset all parameters
{
FormData data = new FormData();
data.type = typeof(formType);
this.dataPool.Add(form_id, data);
this.formPool.Add(form_id, ret);
}


// the form that was asked for has been newly instantiated, so we'll need to track movement and sizechanges
ret.LocationChanged += delegate(object sender, EventArgs e)
{
foreach (string key in formPool.Keys)
{
if (formPool[key].Equals(sender))
{
if (dataPool.ContainsKey(key))
{
Form form = sender as Form;
FormData data = dataPool[key];
data.location = form.Location;
dataPool[key] = data;
}
}
}
};

ret.SizeChanged += delegate(object sender, EventArgs e)
{
foreach (string key in formPool.Keys)
{
if (formPool[key].Equals(sender))
{
if (dataPool.ContainsKey(key))
{
Form form = sender as Form;
FormData data = dataPool[key];
data.size = form.Size;
dataPool[key] = data;
}
}
}
};

return ret;
}

/// <summary>
/// Save all formdata to a file. Look out, no errorhandling.
/// </summary>
/// <param name="filename">The filename to store our data in.</param>
public void Save(string filename)
{
Stream stream = File.Open(filename, FileMode.Create);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, this);
stream.Close();
}


/// <summary>
/// Return a formspool from file that was saved using the Save method.
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
public static FormsPool FromFile(string filename)
{
Stream stream;
try
{
stream = File.Open(filename, FileMode.Open);
}
catch (FileNotFoundException)
{
return new FormsPool();
}

BinaryFormatter formatter = new BinaryFormatter();
FormsPool pool = null;

try
{
pool = formatter.Deserialize(stream) as FormsPool;
}
catch { }
finally
{
if (pool == null) pool = new FormsPool();
stream.Close();
}
return pool;
}
}

[Serializable]
public struct FormData
{
public Type type;
public string form_id;
public Point location;
public Size size;
}
}


Standards are great! Everybody should have one!
AnswerRe: Generic parameter/reflection problem Pin
Chintan.Desai10-Jul-07 2:08
Chintan.Desai10-Jul-07 2:08 
GeneralRe: Generic parameter/reflection problem Pin
Bekjong10-Jul-07 2:22
Bekjong10-Jul-07 2:22 
GeneralRe: Generic parameter/reflection problem Pin
Le centriste10-Jul-07 2:38
Le centriste10-Jul-07 2:38 
AnswerRe: Generic parameter/reflection problem Pin
AFSEKI10-Jul-07 6:26
AFSEKI10-Jul-07 6:26 
GeneralRe: Generic parameter/reflection problem Pin
Bekjong10-Jul-07 22:05
Bekjong10-Jul-07 22:05 
AnswerRe: Generic parameter/reflection problem Pin
AFSEKI11-Jul-07 22:04
AFSEKI11-Jul-07 22:04 
GeneralRe: Generic parameter/reflection problem Pin
Bekjong11-Jul-07 22:38
Bekjong11-Jul-07 22:38 
AnswerRe: Generic parameter/reflection problem Pin
AFSEKI11-Jul-07 23:58
AFSEKI11-Jul-07 23:58 
QuestionOPC communication in C# Pin
k reddy10-Jul-07 1:38
k reddy10-Jul-07 1:38 
AnswerRe: OPC communication in C# Pin
AlessandroOPC6-Apr-09 4:59
AlessandroOPC6-Apr-09 4:59 
QuestionShare DB on LAN Pin
mehrdadc4810-Jul-07 1:22
mehrdadc4810-Jul-07 1:22 
AnswerRe: Share DB on LAN Pin
Christian Graus10-Jul-07 1:24
protectorChristian Graus10-Jul-07 1:24 
GeneralRe: Share DB on LAN Pin
mehrdadc4810-Jul-07 1:33
mehrdadc4810-Jul-07 1:33 
GeneralRe: Share DB on LAN Pin
Colin Angus Mackay10-Jul-07 1:44
Colin Angus Mackay10-Jul-07 1:44 
GeneralRe: Share DB on LAN Pin
originSH10-Jul-07 1:48
originSH10-Jul-07 1:48 
QuestionWIN APP ON LAN Pin
Banjo Ayorinde10-Jul-07 0:54
Banjo Ayorinde10-Jul-07 0:54 
AnswerRe: WIN APP ON LAN Pin
Christian Graus10-Jul-07 1:19
protectorChristian Graus10-Jul-07 1:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.