简体   繁体   English

.NET C# 在 WinForms 中使用 TreeView,如选项卡控件

[英].NET C# Using TreeView in WinForms like tab control

I want to use TreeView in my software so when user clicks a node, the content of the Form will be changed.我想在我的软件中使用 TreeView,所以当用户单击一个节点时,表单的内容将会改变。 Example in this picture, but also I want to grab user input from all forms.这张图片中的示例,但我也想从所有表单中获取用户输入。

WinForms TreeView example WinForms 树视图示例

https://youtu.be/9BdYzMDxl9M?t=46 https://youtu.be/9BdYzMDxl9M?t=46

TreeView -> Node -> User Click -> Display Form_X in the GroupBox TreeView -> 节点 -> 用户单击 -> 在 GroupBox 中显示 Form_X

I tried but I could not find related resources on this topic so I posted this question.我试过了,但找不到关于这个主题的相关资源,所以我发布了这个问题。 Thank you for your help谢谢您的帮助

Don't use forms as child controls.不要将表单用作子控件。 User controls exist specifically for that so use them.用户控件专门为此而存在,因此请使用它们。 You add a user control to a project in the same way as you do a form, simply selecting a different menu option.将用户控件添加到项目中的方式与添加表单的方式相同,只需选择不同的菜单选项即可。 You then design it and add code to it just as you would a form.然后,您可以像设计表单一样设计它并向其添加代码。 Once you build your project, the user control will appear in the Toolbox and you can use it just like you would any other control.构建项目后,用户控件将出现在工具箱中,您可以像使用任何其他控件一样使用它。 You then have a couple of options for how to handle switching controls via the TreeView .然后,您有几个选项可用于如何通过TreeView处理切换控件。

If the number of controls is fairly small, you can add one of each to the GroupBox in the designer, so they all exist all the time.如果控件的数量相当少,您可以在设计器中将每个控件添加到GroupBox中,这样它们就一直存在。 You would probably want to set the Dock property of each to Fill , so they all fill the parent GroupBox .您可能希望将每个的Dock属性设置为Fill ,以便它们都填充父GroupBox In the Load event handler of the form, you can assign each user control instance to the Tag property of the corresponding TreeNode .在表单的Load事件处理程序中,您可以将每个用户控件实例分配给相应TreeNodeTag属性。 When the user selects a node, you get the user control from its Tag property and call BringToFront on it, so the user will see it and it will hide all the other user controls, eg当用户选择一个节点时,您从其Tag属性中获取用户控件并在其上调用BringToFront ,因此用户将看到它并且它将隐藏所有其他用户控件,例如

private void Form1_Load(object sender, EventArgs e)
{
    var nodes = treeView1.Nodes;

    nodes[0].Tag = userControl1;
    nodes[1].Tag = userControl2;
    nodes[2].Tag = userControl3;
}

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
    var userControl = treeView1.SelectedNode.Tag as Control;

    userControl?.BringToFront();
}

Because all the controls exist all the time, you can load all the data at the start and save all the changes at the end and not have to transfer any data when the selection changes.由于所有控件始终存在,因此您可以在开始时加载所有数据并在结束时保存所有更改,而不必在选择更改时传输任何数据。

If the number of controls is larger, you might want to show just one at a time.如果控件的数量较大,您可能希望一次只显示一个。 That becomes more complex because, when the selection changes, you have to make sure that any changes in the current user control are remembered, remove the existing control and then create and load the new control. That becomes more complex because, when the selection changes, you have to make sure that any changes in the current user control are remembered, remove the existing control and then create and load the new control. Because the process is more complex, there are more options about exactly how to implement it.因为这个过程更复杂,所以关于如何实现它有更多的选择。 For that reason, I won't go into specifics here.出于这个原因,我不会在这里详细说明。 If you want to go that way, you need to consider those options and how they relate to the specifics of your application and then decide how you want to implement the process.如果您想这样做,您需要考虑这些选项以及它们与您的应用程序细节的关系,然后决定您希望如何实现该过程。 If you do that and encounter an issue along the way, that would be another question.如果您这样做并在此过程中遇到问题,那将是另一个问题。

What have you tried?你试过什么? I am confident there are many ways to achieve what you describe.我相信有很多方法可以实现您所描述的。 Since you mentioned a TabControl have you considered using a TabControl without the Tabs and manually switch to the proper tab page whenever the tree view node is clicked.由于您提到了TabControl ,您是否考虑过使用不带选项卡的TabControl ,并在单击树视图节点时手动切换到正确的选项卡页。

Something like…就像是…

在此处输入图像描述

private void Form1_Load(object sender, EventArgs e) {
  tabControl1.Appearance = TabAppearance.FlatButtons;
  tabControl1.ItemSize = new Size(0, 1);
  tabControl1.SizeMode = TabSizeMode.Fixed;
}

private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) {
  if (e.Node.Name == "TabPage1") {
    tabControl1.SelectTab("TabPage1");
  }
  else {
    tabControl1.SelectTab("TabPage2");
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM