On the basis of notepad V1.0, added the cut, assignment, paste, and delete functions in editing, and made some changes to the processing before the program is closed.
Newly Added code:
// Operation of the system clipboard
Private void copy toolstripmenuitem_click (Object sender, eventargs E)
{
If (this. textbox1.selectedtext! = "")
{
Clipboard. setdataobject (this. textbox1.selectedtext );
}
Else
MessageBox. Show ("No selected text! ");
}
Private void pasting toolstripmenuitem_click (Object sender, eventargs E)
{
Idataobject idata = clipboard. getdataobject ();
// Check whether the data is in the available format, that is, the text format.
If (idata. getdatapresent (dataformats. Text ))
{
If (this. textbox1.selectedtext! = "")
{
Textbox1.selectedtext = (string) idata. getdata (dataformats. Text );
}
Else
{
Textbox1.text + = (string) idata. getdata (dataformats. Text );
}
}
Else
{
MessageBox. Show ("No data is received from the clipboard! ");
}
}
Private void Delete toolstripmenuitem_click (Object sender, eventargs E)
{
This. textbox1.selectedtext = "";
}
Private void cut xtoolstripmenuitem_click (Object sender, eventargs E)
{
If (this. textbox1.selectedtext! = "")
{
Clipboard. setdataobject (this. textbox1.selectedtext );
This. textbox1.selectedtext = "";
}
Else
MessageBox. Show ("No selected text! ");
}
Modified code:
// Check whether the file is saved before closing
Private void mynotepad_formclosing (Object sender, formclosingeventargs E)
{
If (this. textbox1.text! = "")
{
If (! File. exists (this. Text ))
{
Dialogresult d = MessageBox. Show (the text of "file" + this. Text + @ "has changed.
Do you want to save the file? "," Mynotepad ", messageboxbuttons. yesnocancel, messageboxicon. Information, messageboxdefaultbutton. button1, messageboxoptions. defaulttoptoponly );
Switch (d)
{
Case dialogresult. Yes:
Using (savefiledialog savedig = new savefiledialog ())
{
Savedig. Filter = @ "Text Document (*. txt) | *. txt ";
Savedig. filename = "*. txt ";
If (savedig. showdialog () = dialogresult. OK)
{
Streamwriter Sw = new streamwriter (savedig. filename, false, system. Text. encoding. Default );
Sw. Write (this. textbox1.text );
Sw. Close ();
This. Text = savedig. filename;
}
}
Break;
Case dialogresult. No:
Break;
Case dialogresult. Cancel:
Break;
Default: system. Diagnostics. Debug. Assert (false );
Break;
}
}
}
}