📄 mainform.cs
字号:
this.mnuFormatSetFont.Text = "Set Font";
this.mnuFormatSetFont.Click += new System.EventHandler(this.mnuFormatSetFont_Click);
//
// menuItem9
//
this.menuItem9.Index = 4;
this.menuItem9.Text = "help";
this.menuItem9.Click += new System.EventHandler(this.menuItem9_Click);
//
// colorDialog1
//
this.colorDialog1.FullOpen = true;
//
// saveFileDialog1
//
this.saveFileDialog1.FileName = "doc1";
//
// openFileDialog1
//
this.openFileDialog1.CheckFileExists = false;
this.openFileDialog1.CheckPathExists = false;
this.openFileDialog1.DefaultExt = "cs";
//
// printPreviewDialog1
//
this.printPreviewDialog1.AutoScrollMargin = new System.Drawing.Size(0, 0);
this.printPreviewDialog1.AutoScrollMinSize = new System.Drawing.Size(0, 0);
this.printPreviewDialog1.ClientSize = new System.Drawing.Size(400, 300);
this.printPreviewDialog1.Enabled = true;
this.printPreviewDialog1.Icon = ((System.Drawing.Icon)(resources.GetObject("printPreviewDialog1.Icon")));
this.printPreviewDialog1.Location = new System.Drawing.Point(353, 33);
this.printPreviewDialog1.MinimumSize = new System.Drawing.Size(375, 250);
this.printPreviewDialog1.Name = "printPreviewDialog1";
this.printPreviewDialog1.TransparencyKey = System.Drawing.Color.Empty;
this.printPreviewDialog1.Visible = false;
//
// tbDir
//
this.tbDir.Text = "OpenDirectory";
this.tbDir.ToolTipText = "OpenDirectory";
//
// menuItem10
//
this.menuItem10.Index = 9;
this.menuItem10.Text = "OpenDir";
this.menuItem10.Click += new System.EventHandler(this.menuItem10_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(776, 433);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.toolBar1);
this.Controls.Add(this.statusBar1);
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Text = "Editor";
this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_OnClosing);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
Form1.ActiveForm.ShowDialog();
}
string m_strFileName = "";
bool m_bReadOnly = false;
private void mnuFileNew_Click(object sender, System.EventArgs e)
{
//
// If the text has changed since the last save, ask first
// before clearing the text
if (textBox1.Modified == true)
{
string str;
if (m_strFileName.Length > 0)
{
str = m_strFileName;
}
else
{
str = "The text in the edit control ";
}
str += " has changed. Do you want to save it?";
if (MessageBox.Show(str, "TextChanged",
MessageBoxButtons.YesNo) == DialogResult.Yes)
{
mnuFileSave_Click (sender, e);
}
}
//
// Erase any text in the text box.
textBox1.Clear ();
// and set the modified flag to false
textBox1.Modified = false;
// Empty the file name
m_strFileName = "";
statusBar1.Text = "";
}
private void mnuFileOpen_Click(object sender, System.EventArgs e)
{
// Intialize with the last file name used.
openFileDialog1.FileName = m_strFileName;
// Set the filter for text files
openFileDialog1.Filter = "Text files (*.txt)|*.txt|C# files (*.cs)|*.cs";
// Show the Read Only check box on the dialog box
openFileDialog1.ShowReadOnly = true;
// The default extension is for text files
// openFileDialog1.DefaultExt = "txt";
if (openFileDialog1.ShowDialog () ==
DialogResult.Cancel)
{
return;
}
this.m_strFileName = openFileDialog1.FileName;
FileStream strm;
try
{
strm = new FileStream (m_strFileName,
FileMode.Open,
FileAccess.Read);
StreamReader reader = new StreamReader(strm);
textBox1.Text = reader.ReadToEnd ();
// Save the state of the read only box
m_bReadOnly = openFileDialog1.ReadOnlyChecked;
strm.Close ();
// Set the selection so the caret is at the top of the file
textBox1.SelectionStart = 0;
textBox1.SelectionLength = 0;
// Adding text to the text box sets its Modified property to
// true. The file has not actually been modified, so reset
// the Modified property.
textBox1.Modified = false;
statusBar1.Text = m_strFileName;
}
// Catch the exception when the file cannot be found.
catch (FileNotFoundException)
{
MessageBox.Show ("Cannot open file", "Warning");
}
}
private void mnuFileSave_Click(object sender, System.EventArgs e)
{
if (m_bReadOnly)
{
MessageBox.Show ("File is open as Read-Only",
"Warning");
return;
}
if (m_strFileName.Length == 0)
{
mnuFileSaveAs_Click (sender, e);
return;
}
if (textBox1.Modified == false)
{
return;
}
if (m_strFileName.Length == 0)
{
return;
}
FileStream strm;
try
{
strm = new FileStream (m_strFileName,
FileMode.Open,
FileAccess.Write);
StreamWriter writer = new StreamWriter(strm);
writer.Write (textBox1.Text);
writer.Flush ();
// Chop off any straggler text in the file.
strm.SetLength (textBox1.Text.Length);
strm.Close ();
textBox1.Modified = false;
}
catch (FileNotFoundException)
{
MessageBox.Show ("Cannot open file", "Warning");
}
catch (NotSupportedException)
{
MessageBox.Show ("Cannot write to file", "Warning");
}
catch (UnauthorizedAccessException)
{
MessageBox.Show ("Not authorized to write to file",
"Warning");
}
}
private void mnuFileSaveAs_Click(object sender, System.EventArgs e)
{
if (m_strFileName.Length > 0)
{
saveFileDialog1.FileName = m_strFileName;
}
if (saveFileDialog1.ShowDialog () ==
DialogResult.Cancel)
{
return;
}
if (saveFileDialog1.FileName.Length == 0)
{
return;
}
string fn = saveFileDialog1.FileName;
FileStream strm;
try
{
strm = new FileStream (fn, FileMode.OpenOrCreate,
FileAccess.Write);
StreamWriter writer = new StreamWriter(strm);
writer.Write (textBox1.Text);
writer.Flush ();
// Chop off any straggler text in the file.
strm.SetLength (textBox1.Text.Length);
strm.Close ();
textBox1.Modified = false;
m_strFileName = fn;
statusBar1.Text = m_strFileName;
}
catch (FileNotFoundException)
{
MessageBox.Show ("Cannot open file", "Warning");
}
catch (NotSupportedException)
{
MessageBox.Show ("Cannot write to file", "Warning");
}
catch (UnauthorizedAccessException)
{
MessageBox.Show ("Not authorized to write to file",
"Warning");
}
}
private void mnuFileExit_Click(object sender, System.EventArgs e)
{
Close ();
}
private void mnuEditCut_Click(object sender, System.EventArgs e)
{
textBox1.Cut();
}
private void mnuEditCopy_Click(object sender, System.EventArgs e)
{
textBox1.Copy();
}
private void mnuEditPaste_Click(object sender, System.EventArgs e)
{
textBox1.Paste();
}
private void mnuViewStatusBar_Click(object sender, System.EventArgs e)
{
statusBar1.Visible ^= true;
mnuViewStatusBar.Checked = statusBar1.Visible;
}
private void Form1_OnClosing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (textBox1.Modified == true)
{
DialogResult result;
result = MessageBox.Show ("The file contents have changed. Do you want to save it first?",
"File Modified", MessageBoxButtons.YesNoCancel);
switch (result)
{
case DialogResult.Yes:
mnuFileSave_Click (sender, e);
break;
case DialogResult.No:
break;
case DialogResult.Cancel:
e.Cancel = true;
return;
}
}
Application.Exit ();
}
private void mnuViewToolbar_Click(object sender, System.EventArgs e)
{
toolBar1.Visible ^= true;
mnuViewToolbar.Checked = toolBar1.Visible;
}
private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
switch (e.Button.ImageIndex)
{
case 0:
mnuFileNew_Click(sender, (System.EventArgs) e);
break;
case 1:
mnuFileOpen_Click(sender, (System.EventArgs) e);
break;
case 2:
mnuFileSave_Click(sender, (System.EventArgs) e);
break;
}
}
// Set up some permanent storage for the custom colors
int [] clrCustom = new int [16]
{
0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff,
0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff,
0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff,
0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff
};
private void mnuFormatSetTextColor_Click(object sender, System.EventArgs e)
{
ColorDialog cd = new ColorDialog ();
// Setting FullOpen to true will display the custom color portion
// when the dialog box first opens.
cd.FullOpen = false;
// Setting the Color property will select the color in the dialog
cd.Color = textBox1.ForeColor;
// Setting AnyColor to true will cause the dialog box to display
// all available colors
cd.AnyColor = false;
// Setting AllowFullOpen to false will prevent the user from
// displaying the custom color section. This override FullOpen.
cd.AllowFullOpen = true;
// Setting CustomColors to an array of int will initialize the
// 16 custom color boxes.
cd.CustomColors = clrCustom;
// Setting ShowHelp to true will display a help button. You are
// responsible for writing the help file, however.
cd.ShowHelp = false;
// Setting SolidColorOnly to false will restrict the selection to
// a solidcolor. This has meaning only when the palette contains less than
// a full set.
cd.SolidColorOnly = false;
if (cd.ShowDialog () == DialogResult.Cancel)
return;
textBox1.BackColor = cd.Color;
clrCustom = cd.CustomColors;
}
private void mnuFormatSetBackgroundColor_Click(object sender, System.EventArgs e)
{
// Intialize the color for the dialog box.
colorDialog1.Color = textBox1.BackColor;
// Set the custom colors to the member array.
colorDialog1.CustomColors = clrCustom;
// Show the colors.
if (colorDialog1.ShowDialog () == DialogResult.Cancel)
return;
// Save any custom colors the user selected.
clrCustom = colorDialog1.CustomColors;
// Set the text box background color.
textBox1.ForeColor = colorDialog1.Color;
}
private void mnuFormatSetFont_Click(object sender, System.EventArgs e)
{
// Initialize the font display to the text box font.
fontDialog1.Font = textBox1.Font;
// Initialize the font display color to the text box text color. This
// will be an approximate value because the font dialog has a limited
// color display
fontDialog1.Color = textBox1.ForeColor;
// Show the font dialog. Do nothing if the user presses Cancel.
if (fontDialog1.ShowDialog () == DialogResult.Cancel)
return;
// Call the OnApply method to set the text box values. Calling the
// same method as the Apply event means you need maintain code in
// only one location.
fontDialog1_OnApply (sender, e);
}
private void fontDialog1_OnApply(object sender, System.EventArgs e)
{
// Set the text box font.
textBox1.Font = fontDialog1.Font;
// Set the text box text color.
textBox1.ForeColor = fontDialog1.Color;
}
private void menuItem3_Click(object sender, System.EventArgs e)
{
textBox1.SelectAll();
}
private void menuItem4_Click(object sender, System.EventArgs e)
{
}
private void menuItem6_Click(object sender, System.EventArgs e)
{
printDocument1.CreateObjRef(textBox1.GetType());
printPreviewDialog1.Document=printDocument1;
printPreviewDialog1.ShowDialog();
}
private void menuItem8_Click(object sender, System.EventArgs e)
{
Process myProcess = new Process();
myProcess.StartInfo.FileName = @"C:\Powerword 2003\Xdict.exe";
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
myProcess.Start();
}
private void menuItem9_Click(object sender, System.EventArgs e)
{
Process myProcess = new Process();
myProcess.StartInfo.FileName = @"F:\SQL Server\1.chm";
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
myProcess.Start();
}
private void textBox1_TextChanged(object sender, System.EventArgs e)
{
}
private void menuItem10_Click(object sender, System.EventArgs e)
{
folderBrowserDialog1.ShowDialog();
if(folderBrowserDialog1.ShowDialog()==DialogResult.Cancel)
return;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -