添加项目文件。
This commit is contained in:
77
口罩泄露定制款/UserControl/TreeComboBox.cs
Normal file
77
口罩泄露定制款/UserControl/TreeComboBox.cs
Normal file
@@ -0,0 +1,77 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace 口罩泄露测试仪控制系统_定制款.窗体
|
||||
{
|
||||
public partial class TreeComboBox : UserControl
|
||||
{
|
||||
public TreeComboBox()
|
||||
{
|
||||
InitializeComponent();
|
||||
comboBox.DropDown += ComboBox_DropDown;
|
||||
treeView.NodeMouseClick += TreeView_NodeMouseClick;
|
||||
treeView.Visible = false; // 初始化时隐藏 treeView
|
||||
}
|
||||
|
||||
private void ComboBox_DropDown(object sender, EventArgs e)
|
||||
{
|
||||
// 设置 treeView 的位置和宽度
|
||||
treeView.Location = new Point(comboBox.Left, comboBox.Bottom);
|
||||
treeView.Width = comboBox.Width; // 与 comboBox 宽度一致
|
||||
|
||||
// 根据节点数量动态调整高度,最多显示 5 个节点
|
||||
treeView.Height = Math.Min(treeView.Nodes.Count * 20, 100); // 每个节点 20 像素,最大高度 100
|
||||
|
||||
treeView.Visible = true;
|
||||
treeView.BringToFront();
|
||||
}
|
||||
|
||||
private void TreeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
|
||||
{
|
||||
comboBox.Text = e.Node.Text;
|
||||
treeView.Visible = false; // 选择后隐藏 treeView
|
||||
}
|
||||
|
||||
// 公共方法,用于添加节点
|
||||
public void AddNode(string name)
|
||||
{
|
||||
treeView.Nodes.Add(name);
|
||||
}
|
||||
|
||||
// 公共方法,用于移除选中的节点
|
||||
public void RemoveSelectedNode()
|
||||
{
|
||||
if (treeView.SelectedNode != null)
|
||||
{
|
||||
treeView.Nodes.Remove(treeView.SelectedNode);
|
||||
}
|
||||
}
|
||||
|
||||
// 公共属性,提供对 treeView 的访问
|
||||
public TreeView TreeViewControl => treeView;
|
||||
|
||||
// 公共属性,提供对 comboBox 的访问
|
||||
public ComboBox ComboBoxControl => comboBox;
|
||||
|
||||
private void btnAdd_Click(object sender, EventArgs e)
|
||||
{
|
||||
using (var inputBox = new InputBoxForm("新增项", "请输入新项名称:"))
|
||||
{
|
||||
if (inputBox.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
string newItem = inputBox.InputText;
|
||||
if (!string.IsNullOrEmpty(newItem))
|
||||
{
|
||||
AddNode(newItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void btnRemove_Click(object sender, EventArgs e)
|
||||
{
|
||||
RemoveSelectedNode();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user