在C#中保存并重新加载动态生成的Windows窗体

时间:2014-12-13 08:06:37

标签: c# winforms

我尝试创建一个Windows窗体,用户可以在运行时动态添加用户控件并将其填入。

有什么办法可以保存Windows窗体的状态并在以后重新加载吗?

用于生成添加用户控件的代码示例:

private void assignmentToolStripMenuItem_Click(object sender, EventArgs e)
{
    counter++;
    tableLayoutPanel1.RowCount += 1;
    int index = tableLayoutPanel1.RowCount;
    tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize));    

    userControl21 = new Test2.UserControl3(assignmentDisplayTemplate, assignmentCommandTemplate,variables);
    userControl21.DataAvailable += new EventHandler(userControl21_DataAvailable);      

    tableLayoutPanel1.Controls.Add(userControl21,1,index);

    userControl21.AutoSize = true;
    userControl21.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
    userControl21.Location = new System.Drawing.Point(28, 28);
    userControl21.Size = new System.Drawing.Size(335, 35);
    userControl21.TabIndex = 0;

    userControl21.Name = "Assignment" + counter;
}

2 个答案:

答案 0 :(得分:0)

您应该查看FluentSharp(https://github.com/o2platform/fluentsharp),您或您的用户可以以编程方式执行此操作,例如

var targetFolder = @"C:\O2"; var sourceCodeViewer ="View C# and H2 files in folder".popupWindow() .add_SourceCodeViewer(); sourceCodeViewer.insert_Left(200) .add_TreeView() .add_Nodes(targetFolder.files(true,"*.cs","*.h2"), (file)=>file.fileName()) .after_Selected<string>((file)=>sourceCodeViewer.open(file));

请参阅https://github.com/o2platform/O2.Platform.Scripts了解大量示例

例如

以下是Web自动化API(https://github.com/o2platform/Book_WebAutomation/blob/master/O2Documentation.md)的文档,但WinForms API非常相似(功能更强大:https://github.com/o2platform/FluentSharp/tree/master/FluentSharp.WinForms/ExtensionMethods

答案 1 :(得分:0)

在某种程度上,您可以使用序列化来存储窗口的大小,然后在初始化窗口时可以将其更改回序列化值。但总的来说,你要做的是相当复杂和具有挑战性的任务,没有快速回答。下面我添加了两个链接。一个描述了如何创建自定义布局引擎,另一个是保存整个窗口状态的序列化器的实现。这应该为您提供搜索解决方案的方向: