1. Development environment
Development language: C #
Development platform: Visual Studio2015
2. The function of the program
This program implements a simple text editor, which has the following functions:
1) has a concise and clear graphical interface;
2 text basic editing work such as inserting, delete, text content search, replace and other functions;
3 correctly save the text to the specified position;
4) The editor can display the total number of text rows;
5 Open the text file under the specified path;
6 text to set whether the line wraps automatically.
3. Code Listings
Interface design MainWindow.xaml//text editing window <window x:class= "Mytexteditor.mainwindow" xmlns= "Http://schemas.microsoft.com/wi" Nfx/2006/xaml/presentation "xmlns:x=" Http://schemas.microsoft.com/winfx/2006/xaml "xmlns:d=" Http://schemas
. microsoft.com/expression/blend/2008 "xmlns:mc=" http://schemas.openxmlformats.org/markup-compatibility/2006 "
Xmlns:local= "Clr-namespace:mytexteditor" mc:ignorable= "D" title= "text editor" height= "356" width= "531" > <Grid> <dockpanel margin= "0,0,0,27" > <toolbartray dockpanel.dock= "Top" background= "W Hite "> <toolbar margin=" 0,0,-517,0 "background=" white "> <menu dockpanel.do ck= ' top ' horizontalalignment= ' left ' background= ' white ' borderbrush= ' black ' > <menuitem Header = "File" > <menuitem command= "Open"/> <menuitem header= "Save
"command=" New "/> <Separator/> <menuitem header= "Exit" click= "Fileexit_click"/&G
T </MenuItem> <menuitem header= "Tools" > <menuitem header= "find Replace "inputgesturetext=" ctrl+f "click=" Findandreplace "/> <menuitem name=" Menuautowrap "Hea
Der= "ischecked=" "True" click= "Menuautowrap_click"/> </MenuItem> <menuitem header= "edit" > <menuitemcommand= "Cut"/> &l T MenuItem command= "Copy"/> <menuitem command= "Paste"/> ; MenuItem command= "Delete"/> <menuitem command= "SelectAll"/> & lt;/menuitem> </Menu> </ToolBar> </ToolBarTray>
<textbox x:name= "TextBox1" height= "265" scrollviewer.horizontalscrollbarvisibility= "Auto" acceptsreturn= "T Rue "scrollviewer.verticalscrollbarvisibility=" Visible "textwrapping=" Wrap "verticalalignment=" "Top" width= "515" textchanged= "Countlines"/> </DockPanel> <textbox x:name= "TextBox" horizontalalignment= "left" H eight= "margin=" "0,298,0,0" textwrapping= "Wrap" verticalalignment= "Top" width= "523" isenabled= "False"/> &L
t;/grid> <Window.CommandBindings> <commandbinding command= "New" executed= "newcmdexecuted"/> <commandbinding command= "Open" executed= "opencmdexecuted"/> <commandbinding command= "Cut" Exec uted= "cutcmdexecuted"/> <commandbinding command= "Copy" executed= "copycmdexecuted"/> <CommandBi nding command= "Paste" executed= "pastecmdexecuted"/> <commandbinding command= "Delete" executed= "DeleteCmdExec Uted "/> <commandbinding command= "selectall" executed= "selectallcmdexecuted"/> </Window.CommandBindings> </Window> Finda ndreplace.xaml//Find replacement window <window x:class= "Mytexteditor.findandreplace xmlns=" Http://schemas.microsoft.com/winfx /2006/xaml/presentation "xmlns:x=" Http://schemas.microsoft.com/winfx/2006/xaml "xmlns:d=" HTTP://SCHEMAS.MI
crosoft.com/expression/blend/2008 "xmlns:mc=" http://schemas.openxmlformats.org/markup-compatibility/2006 "
Xmlns:local= "Clr-namespace:mytexteditor" mc:ignorable= "D" title= "Find/Replace" height= "199.633" width= ">" <Grid> <Grid.ColumnDefinitions> <columndefinition width= "153*"/> ; ColumnDefinition width= "139*"/> </Grid.ColumnDefinitions> <label x:name= "Label" content= "Find:" Horizontalalignment= "left" margin= "10,33,0,0" verticalalignment= "Top"/> <textbox x:name= "TextBox" Horizontal Alignment= "left" height="Margin=" "71,35,0,0" textwrapping= "Wrap" verticalalignment= "Top" width= "a" grid.columnspan= "2"/> <button X:name= "button" content= "Find Next" horizontalalignment= "left" margin= "54.382,34,0,0" "verticalalignment=" Top "width=" 75 "Height=" "grid.column=" 1 "click=" Button_Click/> <label x:name= "Label1" replaced by: "Content= T= ' left ' verticalalignment= ' top ' margin= ' 10,73,0,0 '/> <textbox x:name= ' textbox_1 ' horizontalalignment= ' left ' height= "margin=" "71,73,0,0" textwrapping= "Wrap" verticalalignment= "Top" width= "2" grid.columnspan= "/>" ; Button X:name= "button1" content= "replace" horizontalalignment= "left" margin= "54.382,73,0,0" verticalalignment= "Top" Width = "height=" grid.column= "1" click= "Button1_Click"/> <button x:name= "button2" content= "Replace All" Horizontala Lignment= "left" margin= "54,101,0,0" verticalalignment= "the Top" width= "the Height=" "grid.column=" 1 "click=" button2_ Click "/> <checkbox x:name=" cheCkbox "content=" horizontalalignment= "left" margin= "71,101,0,0" verticalalignment= "Top" ischecked= "True"
Grid.columnspan= "2" width= "/> </Grid> </Window> program function to achieve MainWindow.cs using Microsoft.Win32;"
Using System;
Using System.Collections.Generic;
Using System.IO;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Using System.Windows;
Using System.Windows.Controls;
Using System.Windows.Data;
Using System.Windows.Documents;
Using System.Windows.Input;
Using System.Windows.Media;
Using System.Windows.Media.Imaging;
Using System.Windows.Navigation;
Using System.Windows.Shapes; namespace Mytexteditor {///<summary>///mainwindow.xaml interactive logic///</summary> public partial CL
Ass Mainwindow:window {Publicmainwindow () {InitializeComponent (); This.
Windowstartuplocation= System.Windows.WindowStartupLocation.CenterScreen; Private Voidnewcmdexecuted (object sender, Executedroutedeventargs e)//Save text to specified path {if (TextBox1.Text!= null) { Messageboxresultresult =messagebox.show ("Save text file.")
"," Save ", Messageboxbutton.yesno);
if (result = = Messageboxresult.yes) {Savefiledialogsavedlg =new savefiledialog (); Savedlg.filter = "Text Files | *.txt "; Can only be saved as txt text file if (True ==savedlg.showdialog ()) {File.wri
Tealltext (Savedlg.filename,textbox1.text);
} else {TextBox1.Text = null;
}} private Voidopencmdexecuted (object sender, Executedroutedeventargs e)//open Specified path text
{Openfiledialogopendlg =new OpenFileDialog (); Opendlg.filter = "Text Files |
*.txt ";//Only txt text file can be opened if (true ==opendlg.showdialog ()) {stringdatafromfile =file.readalltext (opendlg.filename);
TextBox1.Text = Datafromfile; } private voidselectallcmdexecuted (object sender, Executedroutedeventargs e) {} private Voi Ddeletecmdexecuted (object sender, Executedroutedeventargs e) {} private voidcutcmdexecuted (object sender,
Executedroutedeventargs e) {} private voidcopycmdexecuted (object sender, Executedroutedeventargs e) {} Private Voidpastecmdexecuted (object sender, Executedroutedeventargs e) {} private Voidfindandreplace (object
sender, RoutedEventArgs e)//Find replacement window {findandreplace fr=new findandreplace (); Fr.
Owner = this; Fr.
ShowInTaskbar = false; Fr.
Show (); Private Voidmenuautowrap_click (object sender, RoutedEventArgs e)//Auto Wrap {if (menuautowrap). IsChecked ==true) {TEXTBOx1.
textwrapping = Textwrapping.wrapwithoverflow;
menuautowrap.ischecked = false; } if (menuautowrap.ischecked ==false) {textbox1.textwrapping = Textwrapping.nowrap
;
Menuautowrap.ischecked = true; } private Voidfileexit_click (object sender, RoutedEventArgs e)//exit {this.
Newcmdexecuted (Null,null);
App.Current.Shutdown (); Private Voidcountlines (object sender, RoutedEventArgs e)//show number of lines of text {this
. textbox.text= "Current number of lines:" + this.textBox1.LineCount.ToString ();
findandreplace.cs//Find and Replace feature using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Using System.Windows;
Using System.Windows.Controls;
Using System.Windows.Data;
Using System.Windows.Documents;
Using System.Windows.Input;
Using System.Windows.Media; UsiNg System.Windows.Media.Imaging;
Using System.Windows.Shapes; namespace Mytexteditor {///<summary>///findandreplace.xaml interactive logic///</summary> public Partia
L class Findandreplace:window {public Intstart, POS;
Publicfindandreplace () {InitializeComponent (); This.
Windowstartuplocation= System.Windows.WindowStartupLocation.CenterOwner; Private Voidbutton_click (object sender, RoutedEventArgs e)//Find {MainWindow win= (mainwin Dow) this.
Owner;
Stringstrfind = Win.textBox1.Text; Boolbcase = checkbox.ischecked ==true?
True:false; StringComparison com= bcase ==true?
StringComparison.CurrentCulture:StringComparison.CurrentCultureIgnoreCase; if (pos = Strfind.indexof (textbox.text, start, com))!=-1) {Win.textBox1.Select (pos,textbox.
Text.length);
Win.textBox1.Focus (); start = pos +textbox.text.length;
else {pos = 0;
start = 0; } private Voidbutton1_click (object sender, RoutedEventArgs e)//replace {MainWindow Win= (MainWindow) this.
Owner;
Stringstrfind = Win.textBox1.Text; Boolbcase = checkbox.ischecked ==true?
True:false; StringComparison com= bcase ==true?
StringComparison.CurrentCulture:StringComparison.CurrentCultureIgnoreCase; if (pos = Strfind.indexof (textbox.text, start, com))!=-1) {Win.textBox1.Select (pos,textbox.
Text.length);
Win.textBox1.Focus ();
Win.textBox1.SelectedText =textbox_1.text;
start = pos +textbox.text.length;
else {pos = 0;
start = 0; }} private Voidbutton2_click (object sender, RoutedEventArgs e)//replace all {pos = 0;
start = 0; MainWindow win= (MainWindow) this.
Owner;
Stringstrfind = Win.textBox1.Text; Boolbcase = checkbox.ischecked ==true?
True:false; StringComparison com= bcase ==true?
StringComparison.CurrentCulture:StringComparison.CurrentCultureIgnoreCase; while (pos = Strfind.indexof (textbox.text, start, com))!=-1) {Win.textBox1.Select (POS,TEXTB Ox.
Text.length);
Win.textBox1.Focus ();
Win.textBox1.SelectedText =textbox_1.text;
Start = pos + textBox.Text.Length; }
}
}
}
-----------------------------
Implementation of the effect diagram
Other effect drawings and documents please go to the personal website www.ycxblog.com download view.
---------------------------------------------------------------------------------------------------
End
2016/06/07