如何在C#Windows窗体中创建选项窗体?

GrassProgrammer

http://i.stack.imgur.com/v58ov.png

参见上图。这是Visual Studio选项表单的屏幕截图。

左侧本质上是TreeView。右侧是用于更改程序选项的各种控件。在TreeView中选择节点时,右侧会发生变化,显示不同的选项。

你如何编程这样的东西?右侧是否只有50个重叠的面板,并且节点的选择只是更改了哪个面板可见?如果是这样,您将如何进行管理?在设计器中这将是一团糟。

耶隆·范·朗根(Jeroen van Langen)

不,您不会制作50个重叠的面板。只需创建几个用户控件,例如,在节点的标签上链接类型。您可以使用激活器来创建控件。创建1个树状视图和1个面板:(伪代码)

// create nodes:
TreeNode item = new TreeNode();

item.Tag = typeof(UserControl1);

TreeView.Nodes.Add( item );


// field currentControl
UserControl _currentControl;


// on selection:
TreeViewItem item = (TreeViewItem)sender;

if(_currentControl != null)
{
   _currentControl.Controls.Remove(_currentControl);
   _currentControl.Dispose();
}

// if no type is bound to the node, just leave the panel empty
if (item.Tag == null)
  return;

_currentControl = (UserControl)Activator.Create((Type)item.Tag);
Panel1.Controls.Add(_currentControl);

下一个问题是“我想在控件中调用保存方法或RequestClose方法”。为此,您应该在控件上实现一个接口,并且在切换节点时,只需尝试将_currentusercontrol强制转换为IRequestClose接口并调用,例如,bool RequestClose();。方法。

 // on selection:
 TreeViewItem item = (TreeViewItem)sender;

 if(_currentControl != null)
 {
    // if the _currentControl supports the IRequestClose interface:
    if(_currentControl is IRequestClose)
        // cast the _currentControl to IRequestCode and call the RequestClose method.
        if(!((IRequestClose)_currentControl).RequestClose())
             // now the usercontrol decides whether the control is closed/disposed or not.
             return;

    _currentControl.Controls.Remove(_currentControl);
    _currentControl.Dispose();
 }

 if (item.Tag == null)
   return;

_currentControl = (UserControl)Activator.Create(item.Tag);
Panel1.Controls.Add(_currentControl);

但这将是下一步。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在C#Windows窗体应用程序中动态创建网格

来自分类Dev

如何在C#Windows窗体应用程序中创建平滑的形状动画运动?

来自分类Dev

如何在C#Windows窗体中创建一个重置按钮?

来自分类Dev

如何在C#Windows窗体中的特定位置放置打开窗体?

来自分类Dev

如何清除C#Windows窗体中的DataGridView?

来自分类Dev

C#Windows窗体中的圆角

来自分类Dev

C#Windows窗体中的标签对齐

来自分类Dev

如何在C#Windows窗体应用程序中激活spellCheck?

来自分类Dev

如何在C#Windows窗体中的可缩放图像上绘制

来自分类Dev

如何在C#Windows窗体中获取所有动态numericupdown值

来自分类Dev

C#Windows窗体-如何在组合框中获取先前的SelectedItem

来自分类Dev

如何在C#Windows窗体中获取所有动态numericupdown值

来自分类Dev

如何在C#Windows窗体中使用具有空格的字体来创建Excel Workseet?

来自分类Dev

如何在Windows窗体中动态创建选项卡页内容?

来自分类Dev

创建控件时C#Windows窗体故障

来自分类Dev

在C#Windows窗体中为动态创建的组合框调用SelectedIndexChanged事件

来自分类Dev

如何在C#Windows窗体的TreeView中选择子节点

来自分类Dev

如何在C#Windows窗体的TreeView中选择子节点

来自分类Dev

如何为C#Windows窗体定义窗口类名称?

来自分类Dev

如何使C#Windows窗体应用程序适合屏幕

来自分类Dev

在C#Windows窗体中绑定组合框

来自分类Dev

C#Windows窗体中的计时器控件

来自分类Dev

清除c#windows窗体中画在图片框上的线条

来自分类Dev

如何在Windows窗体中创建树视图菜单?

来自分类Dev

如何在Windows窗体中自动创建按钮?

来自分类Dev

如何使用C#Windows窗体从Excel中的多个单元格读取

来自分类Dev

如何使用C#Windows窗体获取标签中的当前循环值

来自分类Dev

如何将类的对象放入C#Windows窗体的列表中

来自分类Dev

如何检索在C#Windows窗体中单击按钮后分配的当前值

Related 相关文章

  1. 1

    如何在C#Windows窗体应用程序中动态创建网格

  2. 2

    如何在C#Windows窗体应用程序中创建平滑的形状动画运动?

  3. 3

    如何在C#Windows窗体中创建一个重置按钮?

  4. 4

    如何在C#Windows窗体中的特定位置放置打开窗体?

  5. 5

    如何清除C#Windows窗体中的DataGridView?

  6. 6

    C#Windows窗体中的圆角

  7. 7

    C#Windows窗体中的标签对齐

  8. 8

    如何在C#Windows窗体应用程序中激活spellCheck?

  9. 9

    如何在C#Windows窗体中的可缩放图像上绘制

  10. 10

    如何在C#Windows窗体中获取所有动态numericupdown值

  11. 11

    C#Windows窗体-如何在组合框中获取先前的SelectedItem

  12. 12

    如何在C#Windows窗体中获取所有动态numericupdown值

  13. 13

    如何在C#Windows窗体中使用具有空格的字体来创建Excel Workseet?

  14. 14

    如何在Windows窗体中动态创建选项卡页内容?

  15. 15

    创建控件时C#Windows窗体故障

  16. 16

    在C#Windows窗体中为动态创建的组合框调用SelectedIndexChanged事件

  17. 17

    如何在C#Windows窗体的TreeView中选择子节点

  18. 18

    如何在C#Windows窗体的TreeView中选择子节点

  19. 19

    如何为C#Windows窗体定义窗口类名称?

  20. 20

    如何使C#Windows窗体应用程序适合屏幕

  21. 21

    在C#Windows窗体中绑定组合框

  22. 22

    C#Windows窗体中的计时器控件

  23. 23

    清除c#windows窗体中画在图片框上的线条

  24. 24

    如何在Windows窗体中创建树视图菜单?

  25. 25

    如何在Windows窗体中自动创建按钮?

  26. 26

    如何使用C#Windows窗体从Excel中的多个单元格读取

  27. 27

    如何使用C#Windows窗体获取标签中的当前循环值

  28. 28

    如何将类的对象放入C#Windows窗体的列表中

  29. 29

    如何检索在C#Windows窗体中单击按钮后分配的当前值

热门标签

归档