如何在C#Windows窗体中创建选项窗体? [英] How to create an options form in C# Windows Forms?

查看:122
本文介绍了如何在C#Windows窗体中创建选项窗体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

See the picture above. This is a screenshot from Visual Studio's options form.

左侧实际上是一个TreeView.右侧是用于更改程序选项的各种控件. 在TreeView中选择节点后,右侧会发生变化,显示不同的选项.

The left side is essentially a TreeView. The right side is various controls that change program options. When nodes in the TreeView are selected, the right side changes, showing different options.

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

How do you program something like this? Does the right side just have 50 overlapping panels, and the selecting of nodes just changes which panel is visible? If this is the case, how would you go about managing such? It would be a mess in the designer.

推荐答案

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

No you don't make 50 overlapping panels. Just create several usercontrols and, for example, link the types on the tag of a node. You can use the Activator to create the controls. Create 1 treeview and 1 panel: (PSEUDO CODE)

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


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


The next question would be, "I'd like to call a save method, or RequestClose method in the controls". For this, you should implement an Interface on the controls, and when you switch nodes, just try to cast the _currentusercontrol to IRequestClose interface and call, for example, bool RequestClose(); method.

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

但这将是下一步.

这篇关于如何在C#Windows窗体中创建选项窗体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆