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

📄 formj.cs

📁 这是用C#编的一个简单的电子日记本软件
💻 CS
📖 第 1 页 / 共 5 页
字号:
			}
		}

		private void DocTree_AfterCollapse(object sender, System.Windows.Forms.TreeViewEventArgs e)
		{
			e.Node.ImageIndex = (int)TreeImgIndex.ClosedFolder ;
		}

		private void DocTree_AfterExpand(object sender, System.Windows.Forms.TreeViewEventArgs e)
		{
			e.Node.ImageIndex = (int)TreeImgIndex.OpendFolder ;
		}

		private void DocTree_AfterLabelEdit(object sender, System.Windows.Forms.NodeLabelEditEventArgs e)
		{
			e.CancelEdit = true ;
			// 原结点
			FileTreeNode fnode = (FileTreeNode)e.Node ;
			// 新名字
			if (e.Label == null) return ;
			string sNewName = e.Label.Trim () ;
			// 新扩展名
			string sExtName = "" ;

			// 如果新名是空串
			if (Path.GetFileNameWithoutExtension(sNewName).Length == 0)
				return ;
			if (e.Node == DocTree.Nodes[0] || e.Node == DocTree.Nodes[1])
			{
				MessageBox.Show ("对不起 您不能从命名根结点!其实这样的设"
					+"计的确不太好,但目前的版本就是这样。", "Rename Error", 
					MessageBoxButtons.OK, MessageBoxIcon.Warning) ;
				return ;
			}
			
			try 
			{
				if (fnode.NoteStyle == TreeNodeStyle.File)
				{
					sExtName = Path.GetExtension (sNewName) ;
					// 如果没写扩展名 则添加
					if (sExtName.Length == 0)
					{
						sNewName += Path.GetExtension (fnode.Text) ;
					}
						// 如果用户更改了扩展名 这不允许
					else if (sExtName != Path.GetExtension (fnode.Text))
					{
						MessageBox.Show ("对不起 您不能更改扩展名!", "Rename Error", 
							MessageBoxButtons.OK, MessageBoxIcon.Warning) ;
						return ;
					}
				}
			}
				// 如果新名中有非法的 字符
			catch (ArgumentException  ae)
			{
				MessageBox.Show (ae.Message, "Rename Error", MessageBoxButtons.OK, MessageBoxIcon.Warning) ;
				return ;
			}
			try 
			{
				if (fnode.NoteStyle == TreeNodeStyle.File)
					this.ReNameFile (this.AppPath + "\\" + fnode.FullPath, 
						this.AppPath + "\\" + fnode.Parent.FullPath + "\\" + sNewName) ;
				else
					this.ReNameFolder (this.AppPath + "\\" + fnode.FullPath, 
						this.AppPath + "\\" + fnode.Parent.FullPath + "\\" + sNewName) ;

			}
			catch (IOException ioe)
			{
				MessageBox.Show (ioe.Message ,"Rename Error", MessageBoxButtons.OK, MessageBoxIcon.Warning) ;
				return ;
			}
			fnode.Text = sNewName ;
			this.MdiTabs.Invalidate () ;
				
		}

		private void DocTree_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Delete)
				DTMenuDel_Click(sender, e) ;
		}


		private void DTMenuNewF_Click(object sender, System.EventArgs e)
		{
			if (this.DocTree.SelectedNode == null) 
				return ;
			FileTreeNode parentNode = null ;
			this.NewTextDI ++ ;	
			if (((FileTreeNode)this.DocTree.SelectedNode).NoteStyle == TreeNodeStyle.File)
			{
				parentNode = (FileTreeNode)this.DocTree.SelectedNode.Parent ;
			}
			else 
				parentNode = (FileTreeNode)this.DocTree.SelectedNode ;
			// 这里应该没有问题
			// 因为树结点是排序的并且没有重名的
			// 应该可以得到一个新名字
			foreach (TreeNode tn in parentNode.Nodes)
			{
				if (tn.Text == "New" + NewTextDI.ToString())
					NewTextDI ++ ;
			}
			FileTreeNode newftn = new FileTreeNode (TreeNodeStyle.Folder, "New" + this.NewTextDI.ToString()) ; 				
			parentNode.Nodes.Add (newftn) ;
			this.DocTree.SelectedNode = newftn ;
			Directory.CreateDirectory (this.AppPath + "\\" + newftn.FullPath) ;
			newftn.BeginEdit () ;
		}
		private void DTMenuOpen_Click(object sender, System.EventArgs e)
		{
			if (this.DocTree.SelectedNode == null)  return ;
			// Open file
			if (((FileTreeNode)this.DocTree.SelectedNode).NoteStyle == TreeNodeStyle.File)
			{
				this.OpenFileFromDocTree () ;
			}
			// Open 文件夹
			else
			{
				if (!this.DocTree.SelectedNode.IsEditing)
				{
					this.DocTree.SelectedNode.Expand() ;
				}
			}
		}

		private void DTMenuDaoFile_Click(object sender, System.EventArgs e)
		{
			OpenFileDialog openfileDlg = new OpenFileDialog () ;
			try 
			{
				if (openfileDlg.ShowDialog() == DialogResult.OK)
				{			
					
					string strFileName = Path.GetFileName (openfileDlg.FileName) ;
					FileTreeNode nodeNew = (FileTreeNode)this.IsBeNode ((FileTreeNode)(this.DocTree.SelectedNode),
																		strFileName, TreeNodeStyle.File) ;
					if (nodeNew != null)
					{
						switch (MessageBox.Show("文件已存在,要覆盖吗?", "J",
							MessageBoxButtons.YesNo, MessageBoxIcon.Question))
						{
							case DialogResult.Yes :
								break ;
							case DialogResult.No :
								return ;
						}
					}
					else 
					{ 
						// Tree add Note.
						nodeNew = new FileTreeNode (TreeNodeStyle.File, strFileName) ;							
						this.DocTree.SelectedNode.Nodes.Add (nodeNew) ;
					}
					// Copy 
					File.Copy (openfileDlg.FileName, 
						       this.AppPath + @"\" +this.DocTree.SelectedNode.FullPath +  @"\" +  strFileName,							  
							   true) ;
					this.DocTree.SelectedNode = nodeNew ;
				}
			}
			catch (Exception exc)
			{
				MessageBox.Show (exc.ToString ()) ;
			}
		}

		private void DTMenuDaoFolder_Click(object sender, System.EventArgs e)
		{
			FolderBrowserDialog folderDlg = new FolderBrowserDialog () ;
			folderDlg.ShowNewFolderButton = false ;
			folderDlg.Description = "Please Selete!" ;
			if (folderDlg.ShowDialog() == DialogResult.OK)
			{			
				string strFolder = Path.GetFileName (folderDlg.SelectedPath) ;
				TreeNode oldNode = this.IsBeNode ((FileTreeNode)(this.DocTree.SelectedNode), strFolder, TreeNodeStyle.Folder) ;
				
				if (oldNode != null)
				{
					if ((MessageBox.Show("文件夹已存在,要覆盖吗?", "J",
						MessageBoxButtons.YesNo, MessageBoxIcon.Question)) == DialogResult.No )
						return ;
					oldNode.Remove () ;
				}
				FileTreeNode newNode = null ;
				try 
				{
					newNode = new FileTreeNode (TreeNodeStyle.Folder, strFolder) ;							
					this.DocTree.SelectedNode.Nodes.Add (newNode) ;
					// Copy  
					Directory.CreateDirectory (newNode.FullPath) ;
					this.CopyDirectory (folderDlg.SelectedPath, this.AppPath + "\\" + newNode.FullPath) ;
					// Add new node
					this.AddSubNodeFromIO (newNode) ;
					this.DocTree.SelectedNode = newNode ;
				}
				catch (Exception exc)
				{
					if (newNode != null)    newNode.Remove () ;
					if (oldNode != null)    this.DocTree.SelectedNode.Nodes.Add (oldNode) ;
					MessageBox.Show (exc.ToString ()) ;
				}
			}
		}

		private void DTMenuDel_Click(object sender, System.EventArgs e)
		{
			// 我的 Note 与我的 Text 不允许用户删除
			if (this.DocTree.SelectedNode == null) 
				return ;
			if (this.DocTree.SelectedNode.Equals(DocTree.Nodes[0]) || this.DocTree.SelectedNode.Equals(DocTree.Nodes[1]))
			{
				MessageBox.Show ("请不要试图删除跟结点!谢谢合作!","J-Delete error!",
					              MessageBoxButtons.OK, MessageBoxIcon.Error) ;
				return ;
			}
			
			if (MessageBox.Show(@"真的要从磁盘删除 ..\" + this.DocTree.SelectedNode.FullPath + @" 吗?", "J",
								MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
			{
				try 
				{
					// 删除文件
					if (((FileTreeNode)this.DocTree.SelectedNode).NoteStyle == TreeNodeStyle.Folder)
					{
						this.DeleteFolderForMdiDoc (this.AppPath + @"\" + this.DocTree.SelectedNode.FullPath) ;
						Directory.Delete (this.AppPath + @"\" + this.DocTree.SelectedNode.FullPath, true) ;
					}
					else if (((FileTreeNode)this.DocTree.SelectedNode).NoteStyle == TreeNodeStyle.File)
					{
						this.MdiDocManager.DeleteFile (this.AppPath + @"\" + this.DocTree.SelectedNode.FullPath) ;
						File.Delete (this.AppPath + @"\" + this.DocTree.SelectedNode.FullPath) ;
					}
					// 从DocTree 中移除结点
					this.DocTree.SelectedNode.Remove() ;
				}
				catch (Exception exc)
				{
					MessageBox.Show (exc.Message) ;
				}	
			}	
			this.MdiTabs.Invalidate () ;
			this.TextPaneShowNewText () ;
		}
		
		private void DTMenuReName_Click(object sender, System.EventArgs e)
		{
			this.DocTree.SelectedNode.BeginEdit () ;
		}

		private void DocTreeMenu_Popup(object sender, System.EventArgs e)
		{
			if (this.DocTree.SelectedNode == null) 
			{
				this.DocTree.SelectedNode = DocTree.SelectedNode ;
			}
			// 如果依然没有将操作的结点(就是 DocTree.SelectedNode 也为 null) 则返回。
			if (this.DocTree.SelectedNode == null)  return ;

			// 修改弹出式菜单项 Enabled 属性
			if (((FileTreeNode)this.DocTree.SelectedNode).NoteStyle == TreeNodeStyle.File)
			{
				DTMenuOpenInCurWnd.Enabled = true ;
				DTMenuDao.Enabled = false ;
			}
			else
			{
				DTMenuOpenInCurWnd.Enabled = false ;
				DTMenuDao.Enabled = true ;
			}
		}


		private void TimeTab_Tick_L(object sender, System.EventArgs e)
		{
			if (this.MdiTabs.Location.X < 0 && 
				this.MdiTabs.Size.Width + this.MdiTabs.Location.X -20
				< this.MdiTabOwner.Size.Width)
			{
				this.MdiTabs.Location = new Point (this.MdiTabOwner.Size.Width - this.MdiTabs.Size.Width, 0) ;
				this.TimeTab.Enabled = false ;
				this.TimeTab.Tick -= ehTimeTab_Tick_L ;
				return ;
			}
			this.MdiTabs.Location = new Point (this.MdiTabs.Location.X - 20, 0) ;
		}

		private void TimeTab_Tick_R(object sender, System.EventArgs e)
		{
			if (this.MdiTabs.Location.X + 20 > 0 )
			{
				this.MdiTabs.Location = new Point (0, 0) ;
				this.TimeTab.Enabled = false ;
				this.TimeTab.Tick -= ehTimeTab_Tick_R ;
				return ;
			}
			this.MdiTabs.Location = new Point (this.MdiTabs.Location.X + 20, 0) ;
		}

		private void TrundleTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
		{
			// 将滚动出 窗口
			if (this.GenericPaneState == WndState.Outing)
			{	
				// 如果已经差 15 象素即滚动完毕 则 
				// 设置好窗口位置 
				// 计时器停止 ;
				if (this.GenericPane.Location.X + 15 >= 15)
				{
					this.GenericPane.Location = new Point (20, 0) ;				
					this.DocTree.Focus () ;
					this.TrundleTimer.Enabled = false ;
					this.GenericPane.Invalidate () ;
					return ;
				}
				else this.GenericPane.Location = new Point (GenericPane.Location.X + 15, 0) ;
			}
		
			// 缩回窗口
			else if (this.GenericPaneState == WndState.Ining)
			{
				// 如果已经全部隐藏了窗口
				// 则 计时器停止 停止滚动
				if (20 - this.GenericPane.Location.X  > this.GenericPane.Size.Width)
				{
					this.TrundleTimer.Enabled = false ;
					return ;
				}
				else this.GenericPane.Location = new Point (GenericPane.Location.X - 15, 0) ; 
			}
		}


		private void JMenuNew_Click(object sender, System.EventArgs e)
		{
			if (this.DocTree.SelectedNode == null) 
				return ;
			FileTreeNode parentNode = null ;
			this.NewTextDI ++ ;	
			if (((FileTreeNode)this.DocTree.SelectedNode).NoteStyle == TreeNodeStyle.File)
			{
				parentNode = (FileTreeNode)this.DocTree.SelectedNode.Parent ;
			}
			else 
				parentNode = (FileTreeNode)this.DocTree.SelectedNode ;
			// 这里应该没有问题
			// 因为树结点是排序的并且没有重名的
			// 应该可以得到一个新名字
			foreach (TreeNode tn in parentNode.Nodes)
			{
				if (tn.Text == "New" + NewTextDI.ToString() + ".txt")
					NewTextDI ++ ;
			}
			FileTreeNode newftn = new FileTreeNode (TreeNodeStyle.File, "New" + this.NewTextDI.ToString() 
													+ ".txt") ;
			parentNode.Nodes.Add (newftn) ;
			this.DocTree.SelectedNode

⌨️ 快捷键说明

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