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

2024-04-24 11:52

本文主要是介绍C#设计树形程序界面的方法:创建特殊窗体,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

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,请网友看到给一个修复的建议吧。

这篇关于C#设计树形程序界面的方法:创建特殊窗体的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/931688

相关文章

python获取指定名字的程序的文件路径的两种方法

《python获取指定名字的程序的文件路径的两种方法》本文主要介绍了python获取指定名字的程序的文件路径的两种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 最近在做项目,需要用到给定一个程序名字就可以自动获取到这个程序在Windows系统下的绝对路径,以下

JavaScript中的高级调试方法全攻略指南

《JavaScript中的高级调试方法全攻略指南》什么是高级JavaScript调试技巧,它比console.log有何优势,如何使用断点调试定位问题,通过本文,我们将深入解答这些问题,带您从理论到实... 目录观点与案例结合观点1观点2观点3观点4观点5高级调试技巧详解实战案例断点调试:定位变量错误性能分

Python中 try / except / else / finally 异常处理方法详解

《Python中try/except/else/finally异常处理方法详解》:本文主要介绍Python中try/except/else/finally异常处理方法的相关资料,涵... 目录1. 基本结构2. 各部分的作用tryexceptelsefinally3. 执行流程总结4. 常见用法(1)多个e

C#使用Spire.Doc for .NET实现HTML转Word的高效方案

《C#使用Spire.Docfor.NET实现HTML转Word的高效方案》在Web开发中,HTML内容的生成与处理是高频需求,然而,当用户需要将HTML页面或动态生成的HTML字符串转换为Wor... 目录引言一、html转Word的典型场景与挑战二、用 Spire.Doc 实现 HTML 转 Word1

C#实现一键批量合并PDF文档

《C#实现一键批量合并PDF文档》这篇文章主要为大家详细介绍了如何使用C#实现一键批量合并PDF文档功能,文中的示例代码简洁易懂,感兴趣的小伙伴可以跟随小编一起学习一下... 目录前言效果展示功能实现1、添加文件2、文件分组(书签)3、定义页码范围4、自定义显示5、定义页面尺寸6、PDF批量合并7、其他方法

JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法

《JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法》:本文主要介绍JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法,每种方法结合实例代码给大家介绍的非常... 目录引言:为什么"相等"判断如此重要?方法1:使用some()+includes()(适合小数组)方法2

C#下Newtonsoft.Json的具体使用

《C#下Newtonsoft.Json的具体使用》Newtonsoft.Json是一个非常流行的C#JSON序列化和反序列化库,它可以方便地将C#对象转换为JSON格式,或者将JSON数据解析为C#对... 目录安装 Newtonsoft.json基本用法1. 序列化 C# 对象为 JSON2. 反序列化

C#文件复制异常:"未能找到文件"的解决方案与预防措施

《C#文件复制异常:未能找到文件的解决方案与预防措施》在C#开发中,文件操作是基础中的基础,但有时最基础的File.Copy()方法也会抛出令人困惑的异常,当targetFilePath设置为D:2... 目录一个看似简单的文件操作问题问题重现与错误分析错误代码示例错误信息根本原因分析全面解决方案1. 确保

504 Gateway Timeout网关超时的根源及完美解决方法

《504GatewayTimeout网关超时的根源及完美解决方法》在日常开发和运维过程中,504GatewayTimeout错误是常见的网络问题之一,尤其是在使用反向代理(如Nginx)或... 目录引言为什么会出现 504 错误?1. 探索 504 Gateway Timeout 错误的根源 1.1 后端

基于C#实现PDF转图片的详细教程

《基于C#实现PDF转图片的详细教程》在数字化办公场景中,PDF文件的可视化处理需求日益增长,本文将围绕Spire.PDFfor.NET这一工具,详解如何通过C#将PDF转换为JPG、PNG等主流图片... 目录引言一、组件部署二、快速入门:PDF 转图片的核心 C# 代码三、分辨率设置 - 清晰度的决定因