⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frmtreelist.cs

📁 Winform_OnlineTraning 我们培训机构的在线培训部分
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace TreeList
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class frmTreeList : System.Windows.Forms.Form
	{
		private System.Windows.Forms.ListView lvwList;
		private System.Windows.Forms.TreeView tvwTree;
		private System.ComponentModel.IContainer components;

		private string[] fonts = {"Arial","Comic Sans ms","Garamond","Impact","Century Gothic","Tahoma"};
		private string[] colors = {"Red","Green","Blue","Pink","Violet","Magenta"};
		private System.Windows.Forms.PictureBox picPicture; 
		private string[] styles = {"Regular","Bold","Italic","Underline","BoldItalic","Strikeout"};
		private DirectoryInfo path;
		private System.Windows.Forms.TextBox txtText;
		private System.Windows.Forms.Label lblText;
		private FileInfo[] files;
		

		public frmTreeList()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
			
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (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()
		{
			this.lvwList = new System.Windows.Forms.ListView();
			this.tvwTree = new System.Windows.Forms.TreeView();
			this.picPicture = new System.Windows.Forms.PictureBox();
			this.txtText = new System.Windows.Forms.TextBox();
			this.lblText = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// lvwList
			// 
			this.lvwList.Location = new System.Drawing.Point(144, 0);
			this.lvwList.Name = "lvwList";
			this.lvwList.Size = new System.Drawing.Size(288, 176);
			this.lvwList.TabIndex = 1;
			this.lvwList.Click += new System.EventHandler(this.lvwList_Click);
			// 
			// tvwTree
			// 
			this.tvwTree.Dock = System.Windows.Forms.DockStyle.Left;
			this.tvwTree.ImageIndex = -1;
			this.tvwTree.Location = new System.Drawing.Point(0, 0);
			this.tvwTree.Name = "tvwTree";
			this.tvwTree.SelectedImageIndex = -1;
			this.tvwTree.Size = new System.Drawing.Size(136, 389);
			this.tvwTree.TabIndex = 2;
			this.tvwTree.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.tvwTree_AfterSelect);
			// 
			// picPicture
			// 
			this.picPicture.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.picPicture.Location = new System.Drawing.Point(144, 264);
			this.picPicture.Name = "picPicture";
			this.picPicture.Size = new System.Drawing.Size(280, 120);
			this.picPicture.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
			this.picPicture.TabIndex = 7;
			this.picPicture.TabStop = false;
			// 
			// txtText
			// 
			this.txtText.Location = new System.Drawing.Point(144, 232);
			this.txtText.Name = "txtText";
			this.txtText.Size = new System.Drawing.Size(264, 21);
			this.txtText.TabIndex = 8;
			this.txtText.Text = "";
			// 
			// lblText
			// 
			this.lblText.Location = new System.Drawing.Point(144, 200);
			this.lblText.Name = "lblText";
			this.lblText.Size = new System.Drawing.Size(200, 23);
			this.lblText.TabIndex = 9;
			this.lblText.Text = "键入要应用格式的文本";
			// 
			// frmTreeList
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
			this.BackColor = System.Drawing.Color.BurlyWood;
			this.ClientSize = new System.Drawing.Size(440, 389);
			this.Controls.Add(this.lblText);
			this.Controls.Add(this.txtText);
			this.Controls.Add(this.picPicture);
			this.Controls.Add(this.tvwTree);
			this.Controls.Add(this.lvwList);
			this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.Name = "frmTreeList";
			this.Text = "TreeView 示例";
			this.Load += new System.EventHandler(this.frmTreeList_Load);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new frmTreeList());
		}

		private void frmTreeList_Load(object sender, System.EventArgs e)
		{
			this.tvwTree.ShowRootLines = true;
			this.tvwTree.ShowPlusMinus = true;
			this.tvwTree.Nodes.Add("格式");
			
			this.tvwTree.Nodes[0].Nodes.Add("字体");
			this.tvwTree.Nodes[0].Nodes.Add("颜色");
			this.tvwTree.Nodes[0].Nodes.Add("样式");
			this.tvwTree.Nodes[0].Nodes.Add("图片");
		
		}

	private void FontAdd(bool stat)
		{
			if(stat)
			{
				for(int i = 0; i<fonts.Length;i++)
					this.lvwList.Items.Add(fonts[i]);
			}
		}

		private void ColorAdd(bool stat)
		{
			if(stat)
			{
				for(int i = 0; i<colors.Length;i++)
					this.lvwList.Items.Add(colors[i]);
			}
		}

		private void StyleAdd(bool stat)
		{
			if(stat)
			{
				for(int i = 0; i < styles.Length;i++)
					this.lvwList.Items.Add(styles[i]);
			}
		}

		private void tvwTree_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
		{
			this.lvwList.Clear();
			this.lvwList.View = View.List;
			
			if(e.Node.Text == "字体")
				this.FontAdd(e.Node.IsSelected); 
			if(e.Node.Text == "颜色")
				this.ColorAdd(e.Node.IsSelected);
			if(e.Node.Text == "样式")
				this.StyleAdd(e.Node.IsSelected);
			if(e.Node.Text == "图片")
				this.PictureAdd(e.Node.IsSelected);
		}

		private void PictureAdd(bool stat)
		{
			string curdir = Environment.CurrentDirectory;
			path = new DirectoryInfo(curdir+@"\..\..\Images");
			files = path.GetFiles(); 

			for(int i=0;i<files.Length;i++)
				lvwList.Items.Add(files[i].Name) ;

		}

		private void lvwList_Click(object sender, System.EventArgs e)
		{
			string curdir = Environment.CurrentDirectory;
			path = new DirectoryInfo(curdir+@"\..\..\Images");
			files = path.GetFiles(); 
			switch(this.tvwTree.SelectedNode.Index)
			{
				case 0:
				{
					this.txtText.Font = new Font(this.lvwList.SelectedItems[0].Text,12);
					break;
				}
				case 1:
				{
					this.txtText.ForeColor = Color.FromName(this.lvwList.SelectedItems[0].Text);
					break;
				}
				case 2:
				{
				if(this.lvwList.SelectedIndices[0] == 0)
					this.txtText.Font = new Font(this.txtText.Font.Name,11,FontStyle.Regular);
				else if(this.lvwList.SelectedIndices[0] == 1)
					this.txtText.Font = new Font(this.txtText.Font.Name,11,FontStyle.Bold);
				else if(this.lvwList.SelectedIndices[0] == 2)
						this.txtText.Font = new Font(this.txtText.Font.Name,11,FontStyle.Italic);
				else if(this.lvwList.SelectedIndices[0] == 3)
						this.txtText.Font = new Font(this.txtText.Font.Name,11,FontStyle.Underline);
				else if(this.lvwList.SelectedIndices[0] == 4)
						this.txtText.Font = new Font(this.txtText.Font.Name,11,FontStyle.Bold|FontStyle.Italic);
				else if(this.lvwList.SelectedIndices[0] == 5)
					this.txtText.Font = new Font(this.txtText.Font.Name,11,FontStyle.Strikeout);
				break;		
				}
				case 3:
				{
					this.txtText.Text ="";
					this.picPicture.Image = System.Drawing.Image.FromFile(@files[this.lvwList.SelectedIndices[0]].Directory+"\\"+files[this.lvwList.SelectedIndices[0]].Name);
					break;
				}
			}
		}

		
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -