📄 textedit.cs
字号:
/// mitemToolsOptions -- Respond to menu selection
/// Tools->Options...
private void mitemToolsOptions_Click(
object sender,
System.EventArgs e)
{
DlgToolsOptions dlg = new DlgToolsOptions(this);
// Get flag for whether toolbar is being displayed
bool bHasTB = this.Controls.Contains(tbarCommands);
// Initialize input values to dialog.
dlg.sbScrollBars = textInput.ScrollBars;
dlg.bProgramMenu = (this.Menu != null);
dlg.bToolbar = bHasTB;
dlg.haTextAlign = textInput.TextAlign;
dlg.bWordWrap = textInput.WordWrap;
// Summon dialog box.
if (dlg.ShowDialog() != DialogResult.OK)
return;
// Hide textbox to minimize redrawing time.
textInput.Visible = false;
// Modify settings based on user input.
textInput.ScrollBars = dlg.sbScrollBars;
this.Menu = (dlg.bProgramMenu) ? menuMain : null;
// Do we need to add toolbar?
// (adding a toolbar twice causes an
// exception, so we have to be careful)
if(dlg.bToolbar && (!bHasTB))
this.Controls.Add(tbarCommands);
// Do we need to remove toolbar?
// (okay to remove a toolbar twice -- we
// do the following to parallel the add code)
if (bHasTB && (!dlg.bToolbar))
this.Controls.Remove(tbarCommands);
// Update text alignment.
textInput.TextAlign = dlg.haTextAlign;
// Update word-wrap setting.
textInput.WordWrap = dlg.bWordWrap;
// Make textbox visible again.
textInput.Visible = true;
} // mitemToolsOptions_Click
/// tbarCommands_ButtonClick - Respond to ButtonClick
/// event for toolbar tbarCommands
private void tbarCommands_ButtonClick(
object sender,
ToolBarButtonClickEventArgs e)
{
if (e.Button == tbbEditFormat)
{
mitemEditFont_Click(sender, e);
}
else if (e.Button == tbbViewOptions)
{
mitemToolsOptions_Click (sender, e);
}
}
/// cmenuMain_Popup -- Handle Popup event for
/// context menu. Set/clear check-mark on context
/// menu items.
private void cmenuMain_Popup(
object sender,
System.EventArgs e)
{
bool bMenu = (this.Menu != null);
mitemProgramMenu.Checked = bMenu;
bool bTB = this.Controls.Contains(tbarCommands);
mitemToolbar.Checked = bTB;
}
/// mitemProgramMenu_Click -- Handle Click on context
/// menu to toggle visibility of program menu.
private void mitemProgramMenu_Click(
object sender,
System.EventArgs e)
{
this.Menu = (this.Menu == null) ? menuMain : null;
}
/// mitemToolbar_Click -- Handle Click on context
/// menu to toggle visibility of toolbar.
private void mitemToolbar_Click(
object sender,
System.EventArgs e)
{
if (mitemToolbar.Checked)
this.Controls.Remove(tbarCommands);
else
this.Controls.Add(tbarCommands);
}
#endregion
#region Text FileIO routines
// Routines to read / write the Text property
// of the multiline Textbox to / from a file.
private OpenFileDialog fdlgOpen;
private SaveFileDialog fdlgSave;
private Encoding encodeFile = Encoding.Default;
private string strCurrentFile = string.Empty;
private void mitemFileOpen_Click(object sender,
EventArgs e)
{
// Create a OpenFile dialog if necessary.
if ( fdlgOpen == null )
{ fdlgOpen = new OpenFileDialog(); }
fdlgOpen.InitialDirectory = "NotepadCE";
fdlgOpen.Filter =
"dat files (*.dat)|*.dat|" +
"txt files (*.txt)|*.txt|" +
"All files (*.*)|*.*";
// Show it.
switch (fdlgOpen.ShowDialog())
{
// Check user//s response.
case DialogResult.OK:
// Save file name.
strCurrentFile = fdlgOpen.FileName;
// Open, read, close file.
StreamReader srdrFile =
new StreamReader(
new FileStream(strCurrentFile,
FileMode.Open),
this.encodeFile);
this.textInput.Text = srdrFile.ReadToEnd();
srdrFile.Close();
break;
default:
break;
}
}
private void mitemFileSave_Click(object sender,
EventArgs e)
{
// if the user has not yet specified
// a file name, do SaveAs.
// else,
// open, write, close file.
if ( strCurrentFile == string.Empty )
{
mitemFileSaveAs_Click(sender, e);
}
else
{
// If the file does not exist,
// create it.
if (! File.Exists(strCurrentFile) )
{
File.Create(strCurrentFile).Close();
}
// Create Writer
StreamWriter swrtFile =
new StreamWriter(
new FileStream(strCurrentFile,
FileMode.Truncate),
this.encodeFile);
// Write the file.
swrtFile.Write(this.textInput.Text);
// Close the file.
swrtFile.Close();
}
}
private void mitemFileSaveAs_Click(object sender,
EventArgs e)
{
// get { the file name from the user.
if ( fdlgSave == null )
{
fdlgSave = new SaveFileDialog();
}
switch (fdlgSave.ShowDialog())
{
case DialogResult.OK:
// Save file name.
strCurrentFile = fdlgSave.FileName;
// Save file.
mitemFileSave_Click(sender, e);
break;
default:
break;
}
}
private void mitemFFFormat_Click(object sender,
EventArgs e)
{
// Set this.encodeFile to the selected encoding.
if (sender.Equals(mitemFFAscii))
{
this.encodeFile = Encoding.ASCII;
}
else if (sender.Equals(mitemFFUnicode))
{
this.encodeFile = Encoding.Unicode;
}
else if (sender.Equals(mitemFFUtf8))
{
this.encodeFile = Encoding.UTF8;
}
else if (sender.Equals(mitemFFUtf7))
{
this.encodeFile = Encoding.UTF7;
}
else
{
this.encodeFile = Encoding.Default;
}
}
#endregion
#region Binary File IO routines
private string strDirName = @"My Documents\NotepadCE";
private string strFileName = "Settings.dat";
private void SaveSettingsToFile()
{
// Create the directory if it does not
// already exist, and make it the
// current directory.
if (! Directory.Exists(strDirName) )
{
Directory.CreateDirectory(strDirName);
}
Directory.SetCurrentDirectory(strDirName);
// Create the file if it does not
// already exist.
if (! File.Exists(strFileName) )
{
File.Create(strFileName).Close();
}
// Create a BinaryWriter (wrapped around a
// FileStream wrapped around the file)
// for output using the user specified
// encoding.
BinaryWriter bwrtFile =
new BinaryWriter(
new FileStream(strFileName,
FileMode.OpenOrCreate,
FileAccess.Write,
FileShare.None),
encodeFile);
// Write the three fields, each of a
// different data type.
// Close the file.
bwrtFile.Write(textInput.Font.Name);
bwrtFile.Write(textInput.Font.Size);
bwrtFile.Write(unchecked((int)textInput.Font.Style));
bwrtFile.Close();
// Create an ourFontInfo structure, load it from
// TextInput's font, have the structure save
// its contents to the file.
//
// ourFontInfo fiFont;
// fiFont.strName = textInput.Font.Name;
// fiFont.sglSize = textInput.Font.Size;
// fiFont.intStyle = textInput.Font.Style;
// fiFont.WriteToFile(strDirName,
// strFileName,
// Encoding.Default);
}
private void ReadSettingsFromFile()
{
// Make the directory the current directory.
Directory.SetCurrentDirectory(strDirName);
// Create a BinaryReader (wrapped around a
// FileStream wrapped around the file)
// for input using the user specified
// encoding.
BinaryReader brdrFile =
new BinaryReader(
new FileStream(strFileName,
FileMode.OpenOrCreate,
FileAccess.Read,
FileShare.None),
encodeFile);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -