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

📄 form1.cs

📁 用c#写的记事本程序,对了解 WINFORM编程有一定用处.
💻 CS
📖 第 1 页 / 共 2 页
字号:
		private void menuItem5_Click(object sender, System.EventArgs e)
		{
			if(needToSave==true)
			{
				DialogResult result=MessageBox.Show("文本内容已经改变,需要保存吗?","保存文件",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Question);
				if(result==DialogResult.Cancel)
				{
					return;
				}
				if(result==DialogResult.Yes)
				{
					menuItemSave_Click(sender,e);
					needToSave=false;
				}
			}
			string file=GetOpenFile();
			if(file==null)
			{
				return;
			}
			else
			{
				currentFileName=file;
				OpenFile();
			}
		}
		private void OpenFile()
		{
			try
			{
				FileInfo f=new FileInfo(currentFileName);
				StreamReader reader=f.OpenText();
				textBoxEdit.Text=reader.ReadToEnd();
				reader.Close();
				this.Text="文本编辑--"+f.Name;
			}
			catch(Exception e)
			{
				MessageBox.Show(e.Message);
			}
		}
		private string GetOpenFile()
		{
			OpenFileDialog openFile=new OpenFileDialog();
			openFile.Title="打开文本文件";
			openFile.CheckFileExists=true;
			openFile.CheckPathExists=true;
			openFile.AddExtension=true;
			openFile.Multiselect=false;
			openFile.Filter="文本文件 (*.txt)|*.txt|所有文件 (*.*)|*.*";
			if(openFile.ShowDialog()==DialogResult.OK)
			{
				return openFile.FileName;
			}
			else
			{
				return null;
			}
		}
		private void menuItemExit_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void menuItemUndo_Click(object sender, System.EventArgs e)
		{
			if(textBoxEdit.CanUndo==true)
			{
				textBoxEdit.Undo();
				textBoxEdit.ClearUndo();
			}
		}

		private void menuItemCut_Click(object sender, System.EventArgs e)
		{
			if(textBoxEdit.SelectedText!="")
			{
				textBoxEdit.Cut();
			}
		}

		private void menuItemCopy_Click(object sender, System.EventArgs e)
		{
			if(textBoxEdit.SelectionLength>0)
			{
				textBoxEdit.Copy();
			}
		}

		private void menuItemPaste_Click(object sender, System.EventArgs e)
		{
			if(Clipboard.GetDataObject().GetDataPresent(DataFormats.Text)==true)
			{
				if(textBoxEdit.SelectionLength>0)
				{
					DialogResult result;
					result=MessageBox.Show("你想覆盖掉选择的文本吗?","覆盖确认",MessageBoxButtons.YesNo);
					if(result==DialogResult.No)
					{
						textBoxEdit.SelectionStart=textBoxEdit.SelectionStart+textBoxEdit.SelectionLength;
					}	
				}
				textBoxEdit.Paste();   
			}
		}

		private void menuItemDel_Click(object sender, System.EventArgs e)
		{
			textBoxEdit.SelectedText.Remove(0,textBoxEdit.SelectionLength);
			textBoxEdit.SelectedText="";
		}

		private void menuItemSelAll_Click(object sender, System.EventArgs e)
		{
			textBoxEdit.SelectAll();
		}

		private void menuItemEdit_Popup(object sender, System.EventArgs e)
		{
			if(Clipboard.GetDataObject().GetDataPresent(DataFormats.Text))
			{
				menuItemPaste.Enabled=true;
			}
			else 
			{
				menuItemPaste.Enabled=false;
			}
			if(textBoxEdit.SelectionLength>0)
			{
				menuItemCopy.Enabled=true;
				menuItemCut.Enabled=true;
				menuItemDel.Enabled=true;
			}
			else
			{
				menuItemCopy.Enabled=false;
				menuItemCut.Enabled=false;
				menuItemDel.Enabled=false;
			}
			if(textBoxEdit.CanUndo==true)
			{
				menuItemUndo.Enabled=true;
			}
			else
			{
				menuItemUndo.Enabled=false;
			}
		}
		private void menuItemSave_Click(object sender, System.EventArgs e)
		{
			if(currentFileName==null)
			{
				menuItemSaveAs_Click(sender,e);
			}
			else
			{
				SaveFile(textBoxEdit.Text);
			}
			needToSave=false;
		}

		private void menuItemSaveAs_Click(object sender, System.EventArgs e)
		{
			string file=GetSaveFile();
			if(file==null)
			{
				return;
			}
			else
			{
				currentFileName=file;
				SaveFile(textBoxEdit.Text);
				FileInfo f=new FileInfo(currentFileName);
				this.Text="文本编辑--"+f.Name;
				needToSave=false;
			}
		} 
		private void SaveFile(string str)
		{
			try
			{
				StreamWriter writer=new StreamWriter(currentFileName);
				writer.Write(str);
				writer.Close();
			}
			catch(Exception e)
			{
				MessageBox.Show(e.Message);
			}
		}
		private string GetSaveFile()
		{
			SaveFileDialog saveFile=new SaveFileDialog();
			saveFile.Title="保存文本文件";
            saveFile.OverwritePrompt=true;
			saveFile.CreatePrompt=true;
			saveFile.AddExtension=true;
			saveFile.Filter="文本文件 (*.txt)|*.txt|所有文件 (*.*)|*.*";
			if(saveFile.ShowDialog()==DialogResult.OK)
			{
				return saveFile.FileName;
			}
			else
			{
				return null;
			}
		}

		private void menuItemFont_Click(object sender, System.EventArgs e)
		{
			FontDialog fontDialog=new FontDialog();
			fontDialog.ShowColor=true;
			fontDialog.AllowScriptChange=true;
			fontDialog.AllowVectorFonts=true;
			fontDialog.ShowEffects=true;
			if(fontDialog.ShowDialog()==DialogResult.OK)
			{
				textBoxEdit.Font=fontDialog.Font;
				textBoxEdit.ForeColor=fontDialog.Color;
			}
		}

		private void menuItemColor_Click(object sender, System.EventArgs e)
		{
			ColorDialog colorDialog=new ColorDialog();
			colorDialog.AllowFullOpen=true;
			colorDialog.AnyColor=true;
			colorDialog.FullOpen=true;
			if(colorDialog.ShowDialog()==DialogResult.OK)
			{
				textBoxEdit.BackColor=colorDialog.Color;
			}
		}

		private void menuItemPrint_Click(object sender, System.EventArgs e)
		{
			PrintDialog printDialog=new PrintDialog();
			printDialog.Document=printDocument;
			if(printDialog.ShowDialog()==DialogResult.OK)
			{
				try
				{
					printDocument.Print();
				}
				catch(Exception e2)
				{
					MessageBox.Show(e2.Message);
				}
			}
		}
		private void printDocument_PrintPage(object sender,PrintPageEventArgs e)
		{
			float linesPerPage=0;
			float yPos=0;
			int count=0;
			float leftMargin=e.MarginBounds.Left;
			float topMargin=e.MarginBounds.Top;
			string line=null;
            StreamReader streamToPrint=new StreamReader(currentFileName);
			SolidBrush brush=new SolidBrush(textBoxEdit.ForeColor);
			linesPerPage=e.MarginBounds.Height/textBoxEdit.Font.GetHeight(e.Graphics);
			while(count<linesPerPage&&((line=streamToPrint.ReadLine())!=null))
			{
				yPos=topMargin+(count*textBoxEdit.Font.GetHeight(e.Graphics));
				e.Graphics.DrawString(line,textBoxEdit.Font,brush,leftMargin,yPos,new StringFormat());
				count++;
			}
			if(line!=null)
			{
				e.HasMorePages=true;
			}
			else
			{
				e.HasMorePages=false;
			}
		}

		private void menuItemPageSet_Click(object sender, System.EventArgs e)
		{
			PageSetupDialog pageSet=new PageSetupDialog();
			pageSet.Document=printDocument;
			pageSet.ShowDialog();
		}

		private void FormMain_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			if(needToSave==true)
			{
				DialogResult result=MessageBox.Show("文本内容已经改变,需要保存吗?","保存文件",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Question);
				if(result==DialogResult.Cancel)
				{
					e.Cancel=true;
					return;
				}
				if(result==DialogResult.Yes)
				{
					menuItemSave_Click(sender,e);
					e.Cancel=false;
					return;
				}
			}
		}

		private void menuItemNew_Click(object sender, System.EventArgs e)
		{
			if(needToSave==true)
			{
				DialogResult result=MessageBox.Show("文本内容已经改变,需要保存吗?","保存文件",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Question);
				switch(result)
				{
					case DialogResult.Yes:
					{
						menuItemSave_Click(sender,e);
						textBoxEdit.Clear();
						this.Text="文本编辑--新建文本";
						needToSave=false;
						break;
					}
					case DialogResult.No:
					{
						textBoxEdit.Clear();
						this.Text="文本编辑--新建文本";
						needToSave=false;
						break;
					}
					case DialogResult.Cancel:
					{
						break;
					}
				}
			}
			else
			{
				textBoxEdit.Clear();
				this.Text="文本编辑新建文本";
			}
		}

	}
}

⌨️ 快捷键说明

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