动态Windows表单 - 包含XML的列表

时间:2013-05-06 06:42:02

标签: c# .net winforms button csv

我正在创建一个动态表单,如果单击“添加”,则会出现一个带有一组按钮的新面板。

在此面板中,我想添加一个列表,该列表将记住创建了多少个按钮。因此我想要实现一个列表,但我想再次增加,即使我们重新启动控制台。

可能有提示将其放入XML文件中。这样我的策略可以用CSV中的最大值重命名,但我不知道如何记录以及如何增加...

有什么想法吗?

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 ";
}

1 个答案:

答案 0 :(得分:0)

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

  1. 声明按钮列表:

    列表controlList = new List();

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

  3. 在您的计划关闭或其他适当时机时序列化列表:
  4. 程序启动时反序列化。
  5. 构建一个foreach循环,从反序列化的List中拉出按钮。
  6. 以下是我用于序列化/反序列化的两种方法

    /// <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);
    

    我所做的不仅仅是存储按钮的数量,但这样做的好处是,当按钮具有更多逻辑时,您可以存储更多数据。