如何使用c#创建记事本 [英] how to create a notepad using c#
本文介绍了如何使用c#创建记事本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在c#中学习,我想用c#
创建我的记事本
我写这段代码
I'm learnin c# and I want to create my notepad using c#
I writing this code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace NotePad
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{ // This is where the code for the button click of "New" is going to go.
SaveFileDialog sfd = new SaveFileDialog();
DialogResult dr = MessageBox.Show("Do you want to save the file", "save", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr.Equals(DialogResult.Yes)) //Statement that execute when user click on yes button
{
string filename = sfd.FileName;
String filter = "Text Files|*.txt|All Files|*.*";
sfd.Filter = filter;
sfd.Title = "Save";
if (sfd.ShowDialog(this) == DialogResult.OK)
{
//Write all of the text in txtBox to the specified file
System.IO.File.WriteAllText(filename, richTextBox1.Text);
}
else
{
//Return
return;
}
}
else
{
richTextBox1.Clear();
}
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
DialogResult dr = MessageBox.Show("Do you want to save the file", "save", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
//Declare open as a new OpenFileDailog
OpenFileDialog open = new OpenFileDialog();
//Set the Filename of the OpenFileDailog to nothing
open.FileName = "";
//Declare filename as a String equal to the OpenFileDialog's FileName
String filename = open.FileName;
//Declare filter as a String equal to our wanted OpenFileDialog Filter
String filter = "Text Files|*.txt|All Files|*.*";
//Set the OpenFileDialog's Filter to filter
open.Filter = filter;
//Set the title of the OpenFileDialog to Open
open.Title = "Open";
//Show the OpenFileDialog
if (open.ShowDialog(this) == DialogResult.OK)
{
sfd.Filter = filter;
sfd.Title = "Save";
if (sfd.ShowDialog(this) == DialogResult.OK)
{
//Write all of the text in txtBox to the specified file
System.IO.File.WriteAllText(filename, richTextBox1.Text);
}
else
{
//Return
return;
}
}
else
{
//Return
return;
}
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{ //Declare save as a new SaveFileDailog
SaveFileDialog sw = new SaveFileDialog();
//Declare filename as a String equal to the SaveFileDialog's FileName
string filename = sw.FileName;
//Declare filter as a String equal to our wanted SaveFileDialog Filter
String filter = "Text Files|*.txt|All Files|*.*";
//Set the SaveFileDialog's Filter to filter
sw.Filter = filter;
//Set the title of the SaveFileDialog to Save
sw.Title = "Save";
//Show the SaveFileDialog
if (sw.ShowDialog(this) == DialogResult.OK)
{
//Write all of the text in txtBox to the specified file
System.IO.File.WriteAllText(filename, richTextBox1.Text);
}
else
{
//Return
return;
}
}
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
//Declare save as a new SaveFileDailog
SaveFileDialog sw = new SaveFileDialog();
//Declare filename as a String equal to the SaveFileDialog's FileName
string filename = sw.FileName;
//Declare filter as a String equal to our wanted SaveFileDialog Filter
String filter = "Text Files|*.txt|All Files|*.*";
//Set the SaveFileDialog's Filter to filter
sw.Filter = filter;
//Set the title of the SaveFileDialog to Save
sw.Title = "Save";
//Show the SaveFileDialog
if (sw.ShowDialog(this) == DialogResult.OK)
{
//Write all of the text in txtBox to the specified file
System.IO.File.WriteAllText(filename, richTextBox1.Text);
}
else
{
//Return
return;
}
}
private void exitToolStripMenuItem_Click_1(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
//statement that execute on click of exit button
//and chekcing whether textbox modified or not if modified
//then prompt user to save or not
if (richTextBox1.Modified == true)
{
DialogResult dr = MessageBox.Show("Do you want to save the file before exiting", "unsaved file", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr == DialogResult.Yes)
{
string filename = sfd.FileName;
String filter = "Text Files|*.txt|All Files|*.*";
sfd.Filter = filter;
sfd.Title = "Save";
if (sfd.ShowDialog(this) == DialogResult.OK)
{
//Write all of the text in txtBox to the specified file
System.IO.File.WriteAllText(filename, richTextBox1.Text);
}
else
{
//Return
return;
}
}
else
{
richTextBox1.Modified = false;
Application.Exit();
}
}
}
private void cutToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Cut();
}
private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Copy();
}
private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Paste();
}
private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.SelectAll();
}
//Declare prntDoc as a new PrintDocument
System.Drawing.Printing.PrintDocument prntDoc = new System.Drawing.Printing.PrintDocument();
private void printToolStripMenuItem_Click(object sender, EventArgs e)
{
//Declare print as a new PrintDialog
PrintDialog print = new PrintDialog();
//Declare prntDoc_PrintPage as a new EventHandler for prntDoc's Print Page
prntDoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(prntDoc_PrintPage);
//Set prntDoc to the PrintDialog's Document
print.Document = prntDoc;
//Show the PrintDialog
if (print.ShowDialog(this) == DialogResult.OK)
{
//Print the Page
prntDoc.Print();
}
}
private void prntDoc_PrintPage(Object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//Declare g as Graphics equal to the PrintPageEventArgs Graphics
Graphics g = e.Graphics;
//Draw the Text in txtBox to the Document
g.DrawString(richTextBox1.Text, richTextBox1.Font, Brushes.Black, 0, 0);
}
private void prientPriewevToolStripMenuItem_Click(object sender, EventArgs e)
{
//Declare preview as a new PrintPreviewDialog
PrintPreviewDialog preview = new PrintPreviewDialog();
//Declare prntDoc_PrintPage as a new EventHandler for prntDoc's Print Page
prntDoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(prntDoc_PrintPage);
//Set the PrintPreview's Document equal to prntDoc
preview.Document = prntDoc;
//Show the PrintPreview Dialog
if (preview.ShowDialog(this) == DialogResult.OK)
{
//Generate the PrintPreview
prntDoc.Print();
}
}
private void undoToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Undo();
}
private void wordWrapToolStripMenuItem_Click(object sender, EventArgs e)
{
//performing wordwrap operation
if (wordWrapToolStripMenuItem.Checked == false)
{
wordWrapToolStripMenuItem.Checked = true;
richTextBox1.WordWrap = true;
}
else
{
wordWrapToolStripMenuItem.Checked = false;
richTextBox1.WordWrap = false;
}
}
private void redoToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Redo();
}
private void fontToolStripMenuItem_Click(object sender, EventArgs e)
{
FontDialog fontDialog = new FontDialog();
fontDialog.ShowColor = true;
fontDialog.ShowEffects = true;
if (fontDialog.ShowDialog(this) == DialogResult.OK)
{
richTextBox1.ForeColor = fontDialog.Color;
richTextBox1.Font = fontDialog.Font;
}
}
private void findToolStripMenuItem_Click(object sender, EventArgs e)
{
findform f = new findform();
f.ShowDialog();
if (Text != "")
{
richTextBox1.Find(Text);
}
}
private void statusbarToolStripMenuItem_Click(object sender, EventArgs e)
{
//statuslabel1.Text = "Cols " + richTextBox1.Text.Length;
if (statusbarToolStripMenuItem.Checked == false)
{
statusbarToolStripMenuItem.Checked = true;
}
else
{
statusbarToolStripMenuItem.Checked = false;
}
}
private void dateTimeToolStripMenuItem_Click_1(object sender, EventArgs e)
{
richTextBox1.Text = System.DateTime.Now.ToString();
}
private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Clear();
}
private void goToToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void pageSetupToolStripMenuItem_Click(object sender, EventArgs e)
{
}
}
But I did not write find,find next and pagesetup
please help me
推荐答案
这很好我见过的例子
http://www.sourcecodester.com/tutorials/c/6548/simple-notepad-application-using-c-part-1.html [<一个HREF =http://www.sourcecodester.com/tutorials/c /6548/simple-notepad-application-using-c-part-1.html\"target =_ blanktitle =新窗口> ^ ]
http://www.sourcecodester.com /tutorials/c/6562/simple-notepad-application-using-c-part-2.html [ ^ ]
This is good Example I Have seen
http://www.sourcecodester.com/tutorials/c/6548/simple-notepad-application-using-c-part-1.html[^]
http://www.sourcecodester.com/tutorials/c/6562/simple-notepad-application-using-c-part-2.html[^]
引用这个 [ ^ ]
或在此搜索 [ ^ ]
这篇关于如何使用c#创建记事本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文