C#设计树形程序界面的方法:创建特殊窗体

news/2024/5/15 1:23:57

目录

1.TreeView控件

2.实例 

(1)Resources.Designer.cs

(2)Form1.Designer.cs

(3)Form1.cs

(4)生成效果


        以树形来显示程序的菜单,可以更直观、更快捷地对窗体进行操作,而且树型菜单比菜单栏更加美观实用。

1.TreeView控件

        TreeView控件又称为树控件,它可以为用户显示节点层次结构,而每个节点又可以包含子节点,包含子节点的节点叫父节点,其效果就像在Windows操作系统的Windows资源管理器功能的左窗口中显示文件和文件夹一样。TreeView控件的ExpandAll方法用来展开所有树节点,语法格式如下:

public void ExpandAll()

        TreeView控件经常用来设计Windows窗体的左侧导航菜单。

2.实例 

        本实例在制作以树形显示的程序界面时,主要用TreeView控件及其ExpandAll方法。 

(1)Resources.Designer.cs

//------------------------------------------------------------------------------
// <auto-generated>
//     此代码由工具生成。
//     运行时版本:4.0.30319.42000
//
//     对此文件的更改可能会导致不正确的行为,并且如果
//     重新生成代码,这些更改将会丢失。
// </auto-generated>
//------------------------------------------------------------------------------namespace _194.Properties {using System;/// <summary>///   一个强类型的资源类,用于查找本地化的字符串等。/// </summary>// 此类是由 StronglyTypedResourceBuilder// 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。// 若要添加或移除成员,请编辑 .ResX 文件,然后重新运行 ResGen// (以 /str 作为命令选项),或重新生成 VS 项目。[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")][global::System.Diagnostics.DebuggerNonUserCodeAttribute()][global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]internal class Resources {private static global::System.Resources.ResourceManager resourceMan;private static global::System.Globalization.CultureInfo resourceCulture;[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]internal Resources() {}/// <summary>///   返回此类使用的缓存的 ResourceManager 实例。/// </summary>[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]internal static global::System.Resources.ResourceManager ResourceManager {get {if (object.ReferenceEquals(resourceMan, null)) {global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("_194.Properties.Resources", typeof(Resources).Assembly);resourceMan = temp;}return resourceMan;}}/// <summary>///   重写当前线程的 CurrentUICulture 属性,对///   使用此强类型资源类的所有资源查找执行重写。/// </summary>[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]internal static global::System.Globalization.CultureInfo Culture {get {return resourceCulture;}set {resourceCulture = value;}}/// <summary>///   查找 System.Drawing.Bitmap 类型的本地化资源。/// </summary>internal static System.Drawing.Bitmap 上 {get {object obj = ResourceManager.GetObject("上", resourceCulture);return ((System.Drawing.Bitmap)(obj));}}/// <summary>///   查找 System.Drawing.Bitmap 类型的本地化资源。/// </summary>internal static System.Drawing.Bitmap 右 {get {object obj = ResourceManager.GetObject("右", resourceCulture);return ((System.Drawing.Bitmap)(obj));}}}
}

(2)Form1.Designer.cs

namespace _194
{partial class Form1{/// <summary>///  Required designer variable./// </summary>private System.ComponentModel.IContainer components = null;/// <summary>///  Clean up any resources being used./// </summary>/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>protected override void Dispose(bool disposing){if (disposing && (components != null)){components.Dispose();}base.Dispose(disposing);}#region Windows Form Designer generated code/// <summary>///  Required method for Designer support - do not modify///  the contents of this method with the code editor./// </summary>private void InitializeComponent(){TreeNode treeNode1 = new TreeNode("企业类型设置");TreeNode treeNode2 = new TreeNode("企业性质设置");TreeNode treeNode3 = new TreeNode("企业级别设置");TreeNode treeNode4 = new TreeNode("企业资信设置");TreeNode treeNode5 = new TreeNode("基础信息维护", new TreeNode[] { treeNode1, treeNode2, treeNode3, treeNode4 });TreeNode treeNode6 = new TreeNode("客户信息");TreeNode treeNode7 = new TreeNode("联系人信息");TreeNode treeNode8 = new TreeNode("业务往来");TreeNode treeNode9 = new TreeNode("客户信息维护", new TreeNode[] { treeNode6, treeNode7, treeNode8 });TreeNode treeNode10 = new TreeNode("客户投诉");TreeNode treeNode11 = new TreeNode("客户反馈");TreeNode treeNode12 = new TreeNode("客户服务", new TreeNode[] { treeNode10, treeNode11 });TreeNode treeNode13 = new TreeNode("联系人信息查询");TreeNode treeNode14 = new TreeNode("客户信息查询");TreeNode treeNode15 = new TreeNode("客户信息查询", new TreeNode[] { treeNode13, treeNode14 });TreeNode treeNode16 = new TreeNode("客户信息报表");TreeNode treeNode17 = new TreeNode("业务往来报表");TreeNode treeNode18 = new TreeNode("联系人信息表");TreeNode treeNode19 = new TreeNode("打印报表", new TreeNode[] { treeNode16, treeNode17, treeNode18 });TreeNode treeNode20 = new TreeNode("调用Word");TreeNode treeNode21 = new TreeNode("调用EXCEL");TreeNode treeNode22 = new TreeNode("辅助工具", new TreeNode[] { treeNode20, treeNode21 });menuStrip1 = new MenuStrip();toolStripMenuItem2 = new ToolStripMenuItem();toolStripMenuItem3 = new ToolStripMenuItem();toolStripMenuItem4 = new ToolStripMenuItem();toolStripMenuItem1 = new ToolStripMenuItem();panel1 = new Panel();splitContainer1 = new SplitContainer();treeView1 = new TreeView();pictureBox1 = new PictureBox();menuStrip1.SuspendLayout();((System.ComponentModel.ISupportInitialize)splitContainer1).BeginInit();splitContainer1.Panel1.SuspendLayout();splitContainer1.Panel2.SuspendLayout();splitContainer1.SuspendLayout();((System.ComponentModel.ISupportInitialize)pictureBox1).BeginInit();SuspendLayout();// // menuStrip1// menuStrip1.Dock = DockStyle.None;menuStrip1.Items.AddRange(new ToolStripItem[] { toolStripMenuItem2, toolStripMenuItem3, toolStripMenuItem4, toolStripMenuItem1 });menuStrip1.Location = new Point(0, 0);menuStrip1.Name = "menuStrip1";menuStrip1.Size = new Size(352, 25);menuStrip1.TabIndex = 0;menuStrip1.Text = "menuStrip1";// // toolStripMenuItem2// toolStripMenuItem2.Name = "toolStripMenuItem2";toolStripMenuItem2.Size = new Size(92, 21);toolStripMenuItem2.Text = "客户信息维护";// // toolStripMenuItem3// toolStripMenuItem3.Name = "toolStripMenuItem3";toolStripMenuItem3.Size = new Size(68, 21);toolStripMenuItem3.Text = "客户服务";// // toolStripMenuItem4// toolStripMenuItem4.Name = "toolStripMenuItem4";toolStripMenuItem4.Size = new Size(92, 21);toolStripMenuItem4.Text = "客户信息查询";// // toolStripMenuItem1// toolStripMenuItem1.Name = "toolStripMenuItem1";toolStripMenuItem1.Size = new Size(92, 21);toolStripMenuItem1.Text = "基础信息查询";// // panel1// panel1.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;panel1.BackgroundImage = Properties.Resources.上;panel1.BackgroundImageLayout = ImageLayout.Stretch;panel1.Location = new Point(0, 28);panel1.Name = "panel1";panel1.Size = new Size(496, 40);panel1.TabIndex = 1;// // splitContainer1// splitContainer1.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;splitContainer1.Location = new Point(0, 65);splitContainer1.Name = "splitContainer1";// // splitContainer1.Panel1// splitContainer1.Panel1.Controls.Add(treeView1);// // splitContainer1.Panel2// splitContainer1.Panel2.BackgroundImageLayout = ImageLayout.Stretch;splitContainer1.Panel2.Controls.Add(pictureBox1);splitContainer1.Size = new Size(496, 282);splitContainer1.SplitterDistance = 165;splitContainer1.TabIndex = 2;// // treeView1// treeView1.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left;treeView1.Location = new Point(0, 0);treeView1.Name = "treeView1";treeNode1.Name = "节点6";treeNode1.Text = "企业类型设置";treeNode2.Name = "节点7";treeNode2.Text = "企业性质设置";treeNode3.Name = "节点8";treeNode3.Text = "企业级别设置";treeNode4.Name = "节点9";treeNode4.Text = "企业资信设置";treeNode5.Name = "节点0";treeNode5.Text = "基础信息维护";treeNode6.Name = "节点10";treeNode6.Text = "客户信息";treeNode7.Name = "节点11";treeNode7.Text = "联系人信息";treeNode8.Name = "节点12";treeNode8.Text = "业务往来";treeNode9.Name = "节点1";treeNode9.Text = "客户信息维护";treeNode10.Name = "节点13";treeNode10.Text = "客户投诉";treeNode11.Name = "节点14";treeNode11.Text = "客户反馈";treeNode12.Name = "节点2";treeNode12.Text = "客户服务";treeNode13.Name = "节点15";treeNode13.Text = "联系人信息查询";treeNode14.Name = "节点16";treeNode14.Text = "客户信息查询";treeNode15.Name = "节点3";treeNode15.Text = "客户信息查询";treeNode16.Name = "节点17";treeNode16.Text = "客户信息报表";treeNode17.Name = "节点18";treeNode17.Text = "业务往来报表";treeNode18.Name = "节点19";treeNode18.Text = "联系人信息表";treeNode19.Name = "节点4";treeNode19.Text = "打印报表";treeNode20.Name = "节点20";treeNode20.Text = "调用Word";treeNode21.Name = "节点21";treeNode21.Text = "调用EXCEL";treeNode22.Name = "节点5";treeNode22.Text = "辅助工具";treeView1.Nodes.AddRange(new TreeNode[] { treeNode5, treeNode9, treeNode12, treeNode15, treeNode19, treeNode22 });treeView1.Size = new Size(165, 282);treeView1.TabIndex = 0;// // pictureBox1// pictureBox1.BackgroundImage = Properties.Resources.右;pictureBox1.BackgroundImageLayout = ImageLayout.Stretch;pictureBox1.Dock = DockStyle.Fill;pictureBox1.Location = new Point(0, 0);pictureBox1.Name = "pictureBox1";pictureBox1.Size = new Size(327, 282);pictureBox1.TabIndex = 0;pictureBox1.TabStop = false;// // Form1// AutoScaleDimensions = new SizeF(7F, 17F);AutoScaleMode = AutoScaleMode.Font;ClientSize = new Size(497, 347);Controls.Add(splitContainer1);Controls.Add(panel1);Controls.Add(menuStrip1);MainMenuStrip = menuStrip1;Name = "Form1";Text = "Form1";Load += Form1_Load;menuStrip1.ResumeLayout(false);menuStrip1.PerformLayout();splitContainer1.Panel1.ResumeLayout(false);splitContainer1.Panel2.ResumeLayout(false);((System.ComponentModel.ISupportInitialize)splitContainer1).EndInit();splitContainer1.ResumeLayout(false);((System.ComponentModel.ISupportInitialize)pictureBox1).EndInit();ResumeLayout(false);PerformLayout();}#endregionprivate MenuStrip menuStrip1;private ToolStripMenuItem toolStripMenuItem1;private ToolStripMenuItem toolStripMenuItem2;private ToolStripMenuItem toolStripMenuItem3;private ToolStripMenuItem toolStripMenuItem4;private Panel panel1;private SplitContainer splitContainer1;private TreeView treeView1;private PictureBox pictureBox1;}
}

(3)Form1.cs

namespace _194
{public partial class Form1 : Form{public Form1(){InitializeComponent();}/// <summary>/// 展开所有节点/// </summary>private void Form1_Load(object sender, EventArgs e){treeView1.ExpandAll();}}
}

(4)生成效果

 

 

        这是一个缺点,当拖拽窗体放大时,panel2里的图片不能stretch,请网友看到给一个修复的建议吧。


http://www.mrgr.cn/p/04467720

相关文章

Kafka 3.x.x 入门到精通(02)——对标尚硅谷Kafka教程

Kafka 3.x.x 入门到精通&#xff08;02&#xff09;——对标尚硅谷Kafka教程 2. Kafka基础2.1 集群部署2.1.1 解压文件2.1.2 安装ZooKeeper2.1.3 安装Kafka2.1.4 封装启动脚本 2.2 集群启动2.2.1 相关概念2.2.1.1 代理&#xff1a;Broker2.2.1.2 控制器&#xff1a;Controller …

【论文阅读】互连网络的负载平衡路由算法 (RLB RLBth)

前言Oblivious Load Balancing 不经意路由负载平衡 1. oblivious routing 不经意/无关路由的背景知识 1. oblivious routing, adaptive routing & minimal/non-minimal routing algorithms 2. RLB and RLBth 1. Motivation of Balancing load 平衡负载的动机2. 一维 ring 的…

【论文阅读】ViTAE:Vision transformer advanced by exploring intrinsic inductive bias

ViTAE:Vision transformer advanced by exploring intrinsic inductive bias 论文地址摘要&#xff1a;简介&#xff1a;3 方法论3.1 重温视觉变压器3.2 ViTAE3.3 缩减单元3.4 Normal cell3.5 模型细节 4 训练4.1 Implementation details4.2 Comparison with the state-of-the-…

Python高阶--闭包

闭包作用:可以用来在一个函数与一组私有变量之间创建关联关系,在给定函数被多次调用的过程中,这些私有变量能够保持其持久性(保存运行环境与变量的状态) 闭包的特征:1.必须要有函数的嵌套,而且外层函数必须返回内层函数。外层函数相当于给内层函数提供了一个包装起来的运…

实验五 Spark SQL编程初级实践

Spark SQL编程初级实践 Spark SQL基本操作 将下列JSON格式数据复制到Linux系统中&#xff0c;并保存命名为employee.json。 { "id":1 , "name":" Ella" , "age":36 } { "id":2, "name":"Bob","a…

数据结构和算法:贪心

贪心算法 贪心算法是一种常见的解决优化问题的算法&#xff0c;其基本思想是在问题的每个决策阶段&#xff0c;都选择当前看起来最优的选择&#xff0c;即贪心地做出局部最优的决策&#xff0c;以期获得全局最优解。 贪心算法和动态规划都常用于解决优化问题。它们之间存在一…

【数据结构】最小生成树(Prim算法、Kruskal算法)解析+完整代码

5.1 最小生成树 定义 对一个带权连通无向图 G ( V , E ) G(V,E) G(V,E)&#xff0c;生成树不同&#xff0c;每棵树的权&#xff08;即树中所有边上的权值之和&#xff09;也可能不同。 设R为G的所有生成树的集合&#xff0c;若T为R中边的权值之和最小的生成树&#xff0c;则T称…

使用大卫的k8s监控面板(k8s+prometheus+grafana)

问题 书接上回&#xff0c;对EKS&#xff08;AWS云k8s&#xff09;启用AMP&#xff08;AWS云Prometheus&#xff09;监控AMG(AWS云 grafana)&#xff0c;上次我们只是配通了EKSAMPAMG的监控路径。这次使用一位大卫老师的grafana的面板&#xff0c;具体地址如下&#xff1a; ht…

【idea】idea 中 git 分支多个提交合并一个提交到新的分支

一、方法原理讲解 我们在 dev 分支对不同的代码文件做了多次提交。现在我们想要把这些提交都合并到 test 分支。首先我们要明白四个 git 操作&#xff0c; commit&#xff1a;命令用于将你的代码变更保存到本地代码仓库中&#xff0c;它创建了一个新的提交&#xff08;commit…

干货整理:好用的文件加密软件有哪些

说到文件加密&#xff0c;想必大家都很熟悉&#xff0c;文件加密已经普遍应用&#xff0c;文件加密是一种重要的安全措施&#xff0c;可以确保数据的机密性、完整性和可用性&#xff0c;降低因数据泄露或丢失带来的风险。 下面小编给大家分享几款常用的加密软件&#xff0c;大…

Spark和Hadoop的安装

实验内容和要求 1&#xff0e;安装Hadoop和Spark 进入Linux系统&#xff0c;完成Hadoop伪分布式模式的安装。完成Hadoop的安装以后&#xff0c;再安装Spark&#xff08;Local模式&#xff09;。 2&#xff0e;HDFS常用操作 使用hadoop用户名登录进入Linux系统&#xff0c;启动…

【Flink入门修炼】2-3 Flink Checkpoint 原理机制

如果让你来做一个有状态流式应用的故障恢复&#xff0c;你会如何来做呢&#xff1f; 单机和多机会遇到什么不同的问题&#xff1f; Flink Checkpoint 是做什么用的&#xff1f;原理是什么&#xff1f; 一、什么是 Checkpoint&#xff1f; Checkpoint 是对当前运行状态的完整记…

IDM下载器_Internet Download Manager 6.42.7

网盘下载 IDM下载器是一款针对互联网所打造的下载管理器。IDM下载器能将下载速度提高5倍&#xff0c;恢复因丢失的连接&#xff0c;网络问题&#xff0c;计算机关闭或意外断电而重新启动中断或中断的下载。IDM下载器还可支持所有流行的浏览器&#xff0c;以使用独特的“高级浏…

论文解读:(VPT)Visual Prompt Tuning

文章汇总 要解决的问题 大型模型应用于下游任务本身就存在挑战。最明显的(通常也是最有效的)适应策略是对预先训练好的模型进行全面的端到端微调。 动机 只微调参数的一个子集 解决的办法 只在输入空间中引入少量特定于任务的可学习参数&#xff0c;而在下游训练期间冻结…

03-JAVA设计模式-策略模式

策略模式 什么是策略模式 策略模式&#xff08;Strategy Pattern&#xff09;是行为设计模式之一&#xff0c;它使你能在运行时改变对象的行为。在策略模式中&#xff0c;一个类的行为或其算法可以在运行时更改。这种类型的设计模式属于行为模式。 在策略模式中&#xff0c;…

Matlab 使用subplot绘制多个子图,一元拟合

实现效果&#xff1a; clc; clear;filename sri.xlsx; % 确认文件路径data readtable(filename); datavalue data{:,2:end}; datavalue datavalue;fig figure(Position, [0, 0, 1500, 900]); indexString ["(a)","(b)","(c)","(d)&qu…

go语言并发实战——日志收集系统(八) go语言操作etcd以及利用watch实现对键值的监控

有关包的安装 我们要实现go语言对第三方包的操作需要我们下载第三方包go.etcd.io&#xff0c;下载命令&#xff1a; go get go.etcd.io/etcd/client/v3 ectd的put与get操作 相关函数说明与示例 我们想实现对etcd进行简单的操作的步骤还是比较简单的&#xff0c;在我上一篇文…

Spring Boot 如何实现缓存预热

Spring Boot 实现缓存预热 1、使用启动监听事件实现缓存预热。2、使用 PostConstruct 注解实现缓存预热。3、使用 CommandLineRunner 或 ApplicationRunner 实现缓存预热。4、通过实现 InitializingBean 接口&#xff0c;并重写 afterPropertiesSet 方法实现缓存预热。 1、使用…

Linux学习之路 -- 进程篇 -- 自定义shell的编写

前面介绍了进程程序替换的相关知识&#xff0c;接下来&#xff0c;我将介绍如何基于前面的知识&#xff0c;编写一个简单的shell&#xff0c;另外本文的所展示的shell可能仅供参考。 目录 <1>获取用户的输入和打印命令行提示符 <2>切割字符串 <3>执行这个…

分析和比较深度学习框架 PyTorch 和 Tensorflow

&#x1f349; CSDN 叶庭云&#xff1a;https://yetingyun.blog.csdn.net/ 深度学习作为人工智能的一个重要分支&#xff0c;在过去十年中取得了显著的进展。PyTorch 和 TensorFlow 是目前最受欢迎、最强大的两个深度学习框架&#xff0c;它们各自拥有独特的特点和优势。 1. Py…