您的位置:首页 > 其它

WPF学习-文本编辑器(部分功能的设计)

2012-04-24 09:57 369 查看
1.文件-新建:检查文本中的内容是否为null,不为null,提示是否将当前内容保存,如果需要保存(MessageBoxResult.Yes),则保存。

表用系统自带的类SaveFileDialog,实现文本流的读入读出。

private void NewCmdExecuted(object sender, ExecutedRoutedEventArgs e)
{
if (txtData.Text != null)
{
MessageBoxResult result= MessageBox.Show("create new document, save yes or not", "create new", MessageBoxButton.YesNo);
if (result == MessageBoxResult.Yes)
{
SaveFileDialog saveDlg = new SaveFileDialog();
saveDlg.Filter = "Text Files | *.txt";
if (true == saveDlg.ShowDialog())
{
File.WriteAllText(saveDlg.FileName, txtData.Text);
}
}
else
{
txtData.Text = null;
}
}
}

2.文件-退出:和File-New功能类似,在退出应用程序时,先询问是否保存当前文本。这里的提示框MessageBox使用的是MessageBoxButton.YesNoCancel,可以取消退出。

private void FileExit_Click(object sender, RoutedEventArgs e)
{
MessageBoxResult result = MessageBox.Show("save before exit, yes or not", "exit", MessageBoxButton.YesNoCancel);
if (result == MessageBoxResult.Yes)
{
SaveFileDialog saveDlg = new SaveFileDialog();
saveDlg.Filter = "Text Files | *.txt";
if (true == saveDlg.ShowDialog())
{
File.WriteAllText(saveDlg.FileName, txtData.Text);
}
}
else if (result == MessageBoxResult.No)
{
this.Close();
}
}

3.文件-打开:使用系统自带的OpenFileDialog类,与上面的SaveFileDialog类似,不多说了。

private void OpenCmdExecuted(object sender, ExecutedRoutedEventArgs e)
{
OpenFileDialog openDlg = new OpenFileDialog();
openDlg.Filter = "Text Files | *.txt";
if (true == openDlg.ShowDialog())
{
string dataFromFile = File.ReadAllText(openDlg.FileName);
txtData.Text = dataFromFile;
}
}

4.工具-查找和替换,InputGestureText="Ctrl+F",设置快捷键,这里调用了另一个findAndReplace窗口,frw.Owner = this,拥有这窗口的使用权限。

private void findAndReplace(object sender, RoutedEventArgs e)
{
findAndReplace frw = new findAndReplace();
frw.ShowInTaskbar = false;
frw.Title = "查找替换对话框";
frw.Owner = this;
frw.Show();
}

下面详细讲解findAndReplace窗口。



(1)查找

查找MainWindow窗口中的txtData.Text,需要在findAndReplace窗口中获得MainWindow窗口的权限,MainWindow win = (MainWindow)this.Owner;

这里需要注意的是区分大小写,bool bCase = chkCase.IsChecked == true ? true : false; 通过StringComparison设置大小写,CurrentCulture为区分大小写,CurrentCultureIgnoreCase不区分。

查找的基本思想:设置pos开始的位置,通过txtData.Text.IndexOf(txtFind.Text, start, com) 找到查找的内容,根据StringComparison设置CurrentCulture为需要大小写,CurrentCultureIgnoreCase忽略大小写,最后将start前移到下一次查找的位置,start = pos + txtFind.Text.Length,方便下一次查找。

private void btnFindNext_Click(object sender, RoutedEventArgs e)
{
MainWindow win = (MainWindow)this.Owner;
string strFind = win.txtData.Text;
bool bCase = chkCase.IsChecked == true ? true : false;
StringComparison com=bCase==true?StringComparison.CurrentCulture:StringComparison.CurrentCultureIgnoreCase;
if ((pos = strFind.IndexOf(txtFind.Text, start, com)) != -1)
{
win.txtData.Select(pos, txtFind.Text.Length);
win.txtData.Focus();
start = pos + txtFind.Text.Length;
}
else
{
pos = 0;
start = 0;
}
}


(2)替换

查找过后进行替换,替换前先将start后移,返回到查找内容的开始位置start = pos- txtFind.Text.Length,替换结束后将start前移到下一次查找的位置,start = pos + txtFind.Text.Length。

private void btnReplace_Click(object sender, RoutedEventArgs e)
{
start = pos- txtFind.Text.Length;
MainWindow win = (MainWindow)this.Owner;    //获取MainWindow的访问权限
string strFind = win.txtData.Text;
bool bCase = chkCase.IsChecked == true ? true : false;
StringComparison com = bCase == true ? StringComparison.CurrentCulture : StringComparison.CurrentCultureIgnoreCase;
if ((pos = strFind.IndexOf(txtFind.Text, start, com)) != -1)
{
win.txtData.Select(pos, txtFind.Text.Length);
win.txtData.Focus();
win.txtData.SelectedText = txtReplace.Text;
start = pos + txtFind.Text.Length;
}
else
{
pos=0;
start=0;
}
}

(3)全部替换

private void btnReplaceAll_Click(object sender, RoutedEventArgs e)

{

pos = 0;

start = 0;

MainWindow win = (MainWindow)this.Owner;

string strFind = win.txtData.Text;

bool bCase = chkCase.IsChecked == true ? true : false;

StringComparison com = bCase == true ? StringComparison.CurrentCulture : StringComparison.CurrentCultureIgnoreCase;

while ((pos = strFind.IndexOf(txtFind.Text, start, com)) != -1)

{

win.txtData.Select(pos, txtFind.Text.Length);

win.txtData.Focus();

win.txtData.SelectedText = txtReplace.Text;

start = pos + txtFind.Text.Length;

}

}

5.工具-自动换行:使用TextWrapping。

private void menuAutoWrap_Click(object sender, RoutedEventArgs e)
{
if (menuAutoWrap.IsChecked)
{
menuAutoWrap.IsChecked = false;
txtData.TextWrapping = TextWrapping.WrapWithOverflow;
}
}

6.工具-拼写错误

private void ToolsSpellingHins_Click(object sender, RoutedEventArgs e)
{
string spellingHints = string.Empty;
SpellingError error = txtData.GetSpellingError(txtData.CaretIndex);
if (error != null)
{
foreach (string s in error.Suggestions)
{
spellingHints += string.Format("{0}\n", s);
}
lblSpellingHints.Content = spellingHints;
expanderSpelling.IsExpanded = true;
}
}

结束语:主要功能介绍完成,不足就是文字功能部分没有完成。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐