0

I am creating a dynamic form where if I click on "Add", a new panel appears with a set of buttons.

In this panel, I would like to add a list which will remember how many buttons were created. Thus I thought realize a list, but I want to increment again even though that we restart the console.

May be there is a tip to put it in a XMLfile. In this way my strategy can be renamed with the max of what is present in the CSV, but I do not know how to record and how to increment...

Any idea?

public class SerialStrategyFuture
{
    public string StrategyName { get; set; }
    public string NumStrategy { get; set; }
}

public void CreateStrategyFuture()
{
    ConsoleStrategyItem strategyItemFuture = new ConsoleStrategyItem();
    strategyItemFuture.Location = new Point(3, 3);
    futureContainer.Height += 85;
    futureContainer.Controls.Add(strategyItemFuture);

    SerialStrategyFuture strategyFuture = new SerialStrategyFuture();
    strategyFuture.StrategyName = "Strat Future ";
    strategyFuture.NumStrategy = "How to increment it ???";
    XmlSerializer serializer = new XmlSerializer(typeof(SerialStrategyFuture));
    TextWriter textWriter = new StreamWriter(@"C:\Users\...");
    serializer.Serialize(textWriter,strategyFuture);
    textWriter.Close();

    ConsoleStrategyItem.Instance.txtStrategyName.Text = "Strat Future 1 ";
}
4

1 回答 1

0

我不确定您是否可以序列化 utureContainer.Controls 列表。因此,我会使用这种方式:

  1. 为您的按钮声明一个列表:

    列表 controlList = new List();

  2. 每次您的用户创建一个新按钮时:controlList.add(strategyItemFuture);

  3. 当您的程序关闭或另一个合适的时刻序列化列表:
  4. 当你的程序启动时反序列化它。
  5. 构建一个从反序列化列表中提取按钮的 foreach 循环。

Here are the two methods I use to serialize / deserialize

/// <summary>
/// Serializes a file to a compressed XML file. If an error occurs, the exception is NOT caught.
/// </summary>
/// <typeparam name="T">The Type</typeparam>
/// <param name="obj">The object.</param>
/// <param name="fileName">Name of the file.</param>
public static void SerializeToXML<T>(T obj, string fileName)
{
    try
    {
        XmlSerializer serializer = new XmlSerializer(typeof(T));

        using (FileStream fs = new FileStream(fileName, FileMode.Create))
        {
            using (GZipStream compressor = new GZipStream(fs, CompressionMode.Compress))
            {
                serializer.Serialize(compressor, obj);
            }
        }
    }
    catch (Exception)
    {
        throw;
    }
}

/// <summary>
/// Deserializes an object from XML.
/// </summary>
/// <typeparam name="T">The object</typeparam>
/// <param name="file">The file.</param>
/// <returns>
/// The deserialized object
/// </returns>
public static T DeserializeFromXml<T>(string file)
{
    T result;
    XmlSerializer ser = new XmlSerializer(typeof(T));
    using (FileStream fs = new FileStream(file, FileMode.Open))
    {
        using (GZipStream decompressor = new GZipStream(fs, CompressionMode.Decompress))
        {
            result = (T)ser.Deserialize(decompressor);
            return result;
        }
    }
}

}

Usage:
SerializeToXML(controlList , yourPath);
  this.controlList = DeserializeFromXml<List<ConsoleStrategyItem>>(yourPath);

I am doing a little bit more than just storing the number of buttons, but this has the advantage that you can store more data when your buttons have more logic.

于 2013-05-06T10:28:05.220 回答