首先先报道哈,我是初学C#,因为很喜欢编程,又觉得C#相对来说很无敌的,就选择学它了...好了废话不多说主要的~~
最近用C#学编写记事本,就是想跟window自带的记事本功能和外表基本相同,前面在网上找了个代码
但是里面的那个我不知道咱弄啊?我已运行就是错误了·~~~~希望高手指点!
就是前面这行的 using System.Linq;
我不知道怎样给定义个什么,下面的inputInfo好像都实在这有关~~
实在菜鸟,希望大家帮忙了·还有一个问题就是怎样在状态栏中显示光标所在行数和列数,就如同自带的记事本那样?希望大家有知晓的可以告知一二,感激不已啊...
一下是主要代码:(希望有什么不好的地方可以提出来)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 Mickey记事本
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} // 用于存储当前操作的文件的名称
private string textFileName = "";
private string filePath = ""; private void 新建_Click(object sender, EventArgs e)
{ textFileName = "";
// 新建一个文本时,若输入框中的内容不为空,应先提示“是否保存”
if (inputInfo.Text != string.Empty)
{ // 若用户选择“是”,应弹出保存文件的对话框
if (MessageBox.Show("是否保存当前文件?", "Mickey温馨提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information)
== DialogResult.Yes)
{
// 保存文件
Save();
Text = "新建-Mickey记事本";
inputInfo.Text = "";
}
else if (MessageBox.Show("是否保存当前文件?", "Mickey温馨提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information)
== DialogResult.No)
{
// 用户选择不保存时将输入框中的内容清除
inputInfo.Text = "";
}
}
}
private void 打开_Click(object sender, EventArgs e)
{
OpenFileDialog openFile = new OpenFileDialog();
openFile.Filter = "文本文件(*.txt)|*.txt";
if (openFile.ShowDialog() == DialogResult.OK)
{
StreamReader sr = new StreamReader(openFile.FileName);
inputInfo.Text = sr.ReadToEnd();
sr.Close(); FileInfo fileInfo = new FileInfo(openFile.FileName);
// 把标题改为打开的文件的名称
Text = "*" + fileInfo.Name + "-Mickey记事本"; textFileName = fileInfo.Name;
}
}
private void 保存_Click(object sender, EventArgs e)
{
Save();
}
// “保存”
private void Save()
{
if (!textFileName.Equals(""))
{
SaveFileDialog saveFile = new SaveFileDialog();
// 默认保存格式
saveFile.Filter = "文本文件(*.txt)|*.txt"; StreamWriter sw = new StreamWriter(filePath, false);
sw.Write(inputInfo.Text);
sw.Close();
MessageBox.Show("文件保存成功!", "Mickey温馨提示");
filePath = saveFile.FileName; // 把标题改为打开的文件的名称
Text = textFileName + "-Mickey记事本"; }
else
{
// 成员变量为“”,说明文件第一次保存,执行“另存为”操作
HoldFile();
}
}
private void HoldFile()
{ // 若用户选择另保存文件
SaveFileDialog saveFile = new SaveFileDialog();
// 默认保存格式
saveFile.Filter = "文本文件(*.txt)|*.txt";
if (saveFile.ShowDialog() == DialogResult.OK)
{
StreamWriter sw = new StreamWriter(saveFile.FileName, false);
sw.WriteLine(inputInfo.Text);
sw.Close();
MessageBox.Show("文件保存成功!", "Mickey温馨提示");
filePath = saveFile.FileName;
}
// 判断是第一次保存还是第二次
if (textFileName.Equals(""))
{
FileInfo fileInfo = new FileInfo(saveFile.FileName);
Text = fileInfo.Name + "-Mickey记事本";
textFileName = fileInfo.Name;
}
else
{
// 把标题改为打开的文件的名称
Text = textFileName + "-Mickey记事本";
}
}
private void 另存为_Click(object sender, EventArgs e)
{
HoldFile();
}
private void 页面设置_Click(object sender, EventArgs e)
{
this.pageSetupDialog1.Document = this.printDocument1;
pageSetupDialog1.ShowDialog();
}
private void 打印_Click(object sender, EventArgs e)
{
if (inputInfo.Text.Length < 1)
{
MessageBox.Show("请确保要打印的文件的内容不为空!", "Mickey温馨提示");
return;
}
else
{
// 设置Document的属性
this.printDialog1.Document = this.printDocument1; this.printDialog1.PrinterSettings = this.pageSetupDialog1.PrinterSettings; if (this.printDialog1.ShowDialog() == DialogResult.OK)
{
try
{
this.printDocument1.Print();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
private void 退出_Click(object sender, EventArgs e)
{
// 退出时应提示用户是否保存当前文本文件
DialogResult result = MessageBox.Show("是否将更改保存?", "Mickey温馨提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
if (result == DialogResult.Yes)
{
Save();
Application.Exit();
}
else if (result == DialogResult.No)
{
Application.Exit();
}
} // 这个成员变量用来存储用户选择进行操作的字符串
private string selectedInfo = "";
private void 编辑_Click(object sender, EventArgs e)
{
if ((inputInfo.SelectedText.Equals("")) && (selectedInfo.Equals("")))
{
剪切.Enabled = false;
复制.Enabled = false;
粘贴.Enabled = false;
删除.Enabled = false;
}
else
{
剪切.Enabled = true;
复制.Enabled = true;
粘贴.Enabled = true;
删除.Enabled = true;
}
}
private void 撤销_Click(object sender, EventArgs e)
{
this.inputInfo.Undo();
} private void 剪切_Click(object sender, EventArgs e)
{
selectedInfo = inputInfo.SelectedText;
this.inputInfo.Cut();
} private void 复制_Click(object sender, EventArgs e)
{
this.inputInfo.Copy();
} private void 粘贴_Click(object sender, EventArgs e)
{
this.inputInfo.Paste();
} private void 删除_Click(object sender, EventArgs e)
{
this.inputInfo.SelectedText = "";
}
private void 查找_Click(object sender, EventArgs e)
{
if (inputInfo.Text == string.Empty)
{
MessageBox.Show("请确保要查找的文件的内容不为空!", "Mickey温馨提示");
}
else
{
//Form2 fr2 = new Form2();
//fr2.sender(this);
//fr2.Show();
}
} private void 查找下一个_Click(object sender, EventArgs e)
{ }
private void 全选_Click(object sender, EventArgs e)
{
this.inputInfo.SelectAll();
//全选_Click(sender,e);
} private void 时间日期_Click(object sender, EventArgs e)
{
inputInfo.Text += "现在时间是:" + DateTime.Now.ToString(); }
private void 自动换行_Click(object sender, EventArgs e)
{
if (自动换行.Checked == true)
{
inputInfo.WordWrap = true;
}
else
{
inputInfo.WordWrap = false;
}
}
private void 字体_Click(object sender, EventArgs e)
{
FontDialog fontDialog = new FontDialog();
if (fontDialog.ShowDialog() == DialogResult.OK)
{
inputInfo.Font = fontDialog.Font;
}
}
private void 查看_Click(object sender, EventArgs e)
{
if (inputInfo.Text.Length > 0)
{
状态栏.Enabled = true;
}
else
{
状态栏.Enabled = false;
}
}
private void 状态栏_Click(object sender, EventArgs e)
{
if (状态栏.Checked == true)
{
状态栏.Checked = false;
statusStrip1.Visible = false;
}
else
{
状态栏.Checked = true;
statusStrip1.Visible = true;
}
}
private void 查看帮助_Click(object sender, EventArgs e)
{
string help = @"C:\Users\狗狗Mickey\Desktop\help.txt";
Help.ShowHelp(this, help);
}
private void 关于记事本_Click(object sender, EventArgs e)
{
AboutBox1 about = new AboutBox1();
about.Show();
}
}
}
最近用C#学编写记事本,就是想跟window自带的记事本功能和外表基本相同,前面在网上找了个代码
但是里面的那个我不知道咱弄啊?我已运行就是错误了·~~~~希望高手指点!
就是前面这行的 using System.Linq;
我不知道怎样给定义个什么,下面的inputInfo好像都实在这有关~~
实在菜鸟,希望大家帮忙了·还有一个问题就是怎样在状态栏中显示光标所在行数和列数,就如同自带的记事本那样?希望大家有知晓的可以告知一二,感激不已啊...
一下是主要代码:(希望有什么不好的地方可以提出来)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 Mickey记事本
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} // 用于存储当前操作的文件的名称
private string textFileName = "";
private string filePath = ""; private void 新建_Click(object sender, EventArgs e)
{ textFileName = "";
// 新建一个文本时,若输入框中的内容不为空,应先提示“是否保存”
if (inputInfo.Text != string.Empty)
{ // 若用户选择“是”,应弹出保存文件的对话框
if (MessageBox.Show("是否保存当前文件?", "Mickey温馨提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information)
== DialogResult.Yes)
{
// 保存文件
Save();
Text = "新建-Mickey记事本";
inputInfo.Text = "";
}
else if (MessageBox.Show("是否保存当前文件?", "Mickey温馨提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information)
== DialogResult.No)
{
// 用户选择不保存时将输入框中的内容清除
inputInfo.Text = "";
}
}
}
private void 打开_Click(object sender, EventArgs e)
{
OpenFileDialog openFile = new OpenFileDialog();
openFile.Filter = "文本文件(*.txt)|*.txt";
if (openFile.ShowDialog() == DialogResult.OK)
{
StreamReader sr = new StreamReader(openFile.FileName);
inputInfo.Text = sr.ReadToEnd();
sr.Close(); FileInfo fileInfo = new FileInfo(openFile.FileName);
// 把标题改为打开的文件的名称
Text = "*" + fileInfo.Name + "-Mickey记事本"; textFileName = fileInfo.Name;
}
}
private void 保存_Click(object sender, EventArgs e)
{
Save();
}
// “保存”
private void Save()
{
if (!textFileName.Equals(""))
{
SaveFileDialog saveFile = new SaveFileDialog();
// 默认保存格式
saveFile.Filter = "文本文件(*.txt)|*.txt"; StreamWriter sw = new StreamWriter(filePath, false);
sw.Write(inputInfo.Text);
sw.Close();
MessageBox.Show("文件保存成功!", "Mickey温馨提示");
filePath = saveFile.FileName; // 把标题改为打开的文件的名称
Text = textFileName + "-Mickey记事本"; }
else
{
// 成员变量为“”,说明文件第一次保存,执行“另存为”操作
HoldFile();
}
}
private void HoldFile()
{ // 若用户选择另保存文件
SaveFileDialog saveFile = new SaveFileDialog();
// 默认保存格式
saveFile.Filter = "文本文件(*.txt)|*.txt";
if (saveFile.ShowDialog() == DialogResult.OK)
{
StreamWriter sw = new StreamWriter(saveFile.FileName, false);
sw.WriteLine(inputInfo.Text);
sw.Close();
MessageBox.Show("文件保存成功!", "Mickey温馨提示");
filePath = saveFile.FileName;
}
// 判断是第一次保存还是第二次
if (textFileName.Equals(""))
{
FileInfo fileInfo = new FileInfo(saveFile.FileName);
Text = fileInfo.Name + "-Mickey记事本";
textFileName = fileInfo.Name;
}
else
{
// 把标题改为打开的文件的名称
Text = textFileName + "-Mickey记事本";
}
}
private void 另存为_Click(object sender, EventArgs e)
{
HoldFile();
}
private void 页面设置_Click(object sender, EventArgs e)
{
this.pageSetupDialog1.Document = this.printDocument1;
pageSetupDialog1.ShowDialog();
}
private void 打印_Click(object sender, EventArgs e)
{
if (inputInfo.Text.Length < 1)
{
MessageBox.Show("请确保要打印的文件的内容不为空!", "Mickey温馨提示");
return;
}
else
{
// 设置Document的属性
this.printDialog1.Document = this.printDocument1; this.printDialog1.PrinterSettings = this.pageSetupDialog1.PrinterSettings; if (this.printDialog1.ShowDialog() == DialogResult.OK)
{
try
{
this.printDocument1.Print();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
private void 退出_Click(object sender, EventArgs e)
{
// 退出时应提示用户是否保存当前文本文件
DialogResult result = MessageBox.Show("是否将更改保存?", "Mickey温馨提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
if (result == DialogResult.Yes)
{
Save();
Application.Exit();
}
else if (result == DialogResult.No)
{
Application.Exit();
}
} // 这个成员变量用来存储用户选择进行操作的字符串
private string selectedInfo = "";
private void 编辑_Click(object sender, EventArgs e)
{
if ((inputInfo.SelectedText.Equals("")) && (selectedInfo.Equals("")))
{
剪切.Enabled = false;
复制.Enabled = false;
粘贴.Enabled = false;
删除.Enabled = false;
}
else
{
剪切.Enabled = true;
复制.Enabled = true;
粘贴.Enabled = true;
删除.Enabled = true;
}
}
private void 撤销_Click(object sender, EventArgs e)
{
this.inputInfo.Undo();
} private void 剪切_Click(object sender, EventArgs e)
{
selectedInfo = inputInfo.SelectedText;
this.inputInfo.Cut();
} private void 复制_Click(object sender, EventArgs e)
{
this.inputInfo.Copy();
} private void 粘贴_Click(object sender, EventArgs e)
{
this.inputInfo.Paste();
} private void 删除_Click(object sender, EventArgs e)
{
this.inputInfo.SelectedText = "";
}
private void 查找_Click(object sender, EventArgs e)
{
if (inputInfo.Text == string.Empty)
{
MessageBox.Show("请确保要查找的文件的内容不为空!", "Mickey温馨提示");
}
else
{
//Form2 fr2 = new Form2();
//fr2.sender(this);
//fr2.Show();
}
} private void 查找下一个_Click(object sender, EventArgs e)
{ }
private void 全选_Click(object sender, EventArgs e)
{
this.inputInfo.SelectAll();
//全选_Click(sender,e);
} private void 时间日期_Click(object sender, EventArgs e)
{
inputInfo.Text += "现在时间是:" + DateTime.Now.ToString(); }
private void 自动换行_Click(object sender, EventArgs e)
{
if (自动换行.Checked == true)
{
inputInfo.WordWrap = true;
}
else
{
inputInfo.WordWrap = false;
}
}
private void 字体_Click(object sender, EventArgs e)
{
FontDialog fontDialog = new FontDialog();
if (fontDialog.ShowDialog() == DialogResult.OK)
{
inputInfo.Font = fontDialog.Font;
}
}
private void 查看_Click(object sender, EventArgs e)
{
if (inputInfo.Text.Length > 0)
{
状态栏.Enabled = true;
}
else
{
状态栏.Enabled = false;
}
}
private void 状态栏_Click(object sender, EventArgs e)
{
if (状态栏.Checked == true)
{
状态栏.Checked = false;
statusStrip1.Visible = false;
}
else
{
状态栏.Checked = true;
statusStrip1.Visible = true;
}
}
private void 查看帮助_Click(object sender, EventArgs e)
{
string help = @"C:\Users\狗狗Mickey\Desktop\help.txt";
Help.ShowHelp(this, help);
}
private void 关于记事本_Click(object sender, EventArgs e)
{
AboutBox1 about = new AboutBox1();
about.Show();
}
}
}
解决方案 »
- WinForm下Splash的问题
- .NET FRAMEWORK 3.0、3.5、4.0的WPF的区别
- C# datatable 数据验证
- 面向对象与数据库操作如何结合?
- 怎么把GRIDVIEW单元格的长度设置为四个字符啊????
- CookieContainer的问题,运行一会cookie失效。。。。在线给分
- 购买C#多线程邮件群发源码
- 只有10分了,可是这个问题很棘手,没有办法,只好出来请大家帮忙
- 如何将string型转换成System.Drawing.FontStyle型,谢谢!
- 相同数据类型的值如何比较大小?
- SqlServer中高级查询,group by,打印信息 ??????
- C#监控windows窗口的打开
如果你是使用.NET Framework 2.0又使用上面的就会提示错误。
如果没有使用到的那么你可以把上面那两句删掉。
如果你是使用Visual Studio 2008,你安装了.NET Framework 3.5,你可以右键项目添加引用,在“添加引用”的对话框中选择浏览,查找你安装.NET Framework 3.5的路径【默认是“C:\WINDOWS\Microsoft.NET\Framework\v3.5”】,选择:Microsoft.Build.Tasks.v3.5.dll再按确定就可以了。
昂~~说的有些专业了·~~呵呵~~~不是很懂~~他这个代码前面那个我怎样做才能正确啊?
using System.Linq;
要是删除这行了
就会提示inputInfo的错误~~代码里面一大堆这个啊~~
哦~~明白了~~我使用的是Visual stdio 2005
.NET Framework 2.0那我现在该怎么弄?谢谢指点~~麻烦了··
private void richTextBox1_MouseClick(object sender, MouseEventArgs e)
{
int x, y;
y = richTextBox1.SelectionStart;
x = richTextBox1.GetLineFromCharIndex(y) + 1;
y = y - richTextBox1.GetFirstCharIndexOfCurrentLine()+1;
toolStripStatusLabel1.Text = "第" + x + "行," + "第" + y + "列";
} private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
int x, y;
y = richTextBox1.SelectionStart;
x = richTextBox1.GetLineFromCharIndex(y) + 1;
y = y - richTextBox1.GetFirstCharIndexOfCurrentLine() + 2;
toolStripStatusLabel1.Text = "第" + x + "行," + "第" + y + "列";
}