实例介绍
【实例简介】
【实例截图】
【核心代码】
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using PanGu; using PanGu.Dict; namespace DictManage { public partial class FormMain : Form { WordDictionary _WordDict = null; String m_DictFileName; string _Version = ""; private int Count { get { if (_WordDict != null) { return _WordDict.Count; } else { return 0; } } } public FormMain() { InitializeComponent(); } private void ShowCount() { labelCount.Text = Count.ToString(); } private void openBinDictFile13ToolStripMenuItem_Click(object sender, EventArgs e) { openFileDialogDict.RestoreDirectory = true; openFileDialogDict.FileName = "Dict.dct"; openFileDialogDict.Filter = "Dictionay file|*.dct"; if (openFileDialogDict.ShowDialog() == DialogResult.OK) { try { DateTime old = DateTime.Now; _WordDict = new WordDictionary(); _WordDict.Load(openFileDialogDict.FileName, out _Version); TimeSpan s = DateTime.Now - old; statusStrip.Items[0].Text = s.TotalMilliseconds.ToString() "ms"; } catch (Exception e1) { MessageBox.Show(String.Format("Can not open dictionary, errmsg:{0}", e1.Message)); return; } panelMain.Enabled = true; m_DictFileName = openFileDialogDict.FileName; this.Text = "V" _Version " " openFileDialogDict.FileName; ShowCount(); } } private void saveBinDictFile13ToolStripMenuItem_Click(object sender, EventArgs e) { if (_WordDict == null) { return; } saveFileDialogDict.RestoreDirectory = true; saveFileDialogDict.FileName = "Dict.dct"; saveFileDialogDict.Filter = "Dictionay file|*.dct"; if (saveFileDialogDict.ShowDialog() == DialogResult.OK) { FormInputDictVersion frmInputDictVersion = new FormInputDictVersion(); frmInputDictVersion.Version = _Version; if (frmInputDictVersion.ShowDialog() == DialogResult.OK) { _WordDict.Save(saveFileDialogDict.FileName, frmInputDictVersion.Version); } } } private void buttonSearch_Click(object sender, EventArgs e) { if (textBoxSearch.Text.Trim() == "") { return; } List<SearchWordResult> result = _WordDict.Search(textBoxSearch.Text.Trim()); result.Sort(); listBoxList.Items.Clear(); foreach (SearchWordResult word in result) { listBoxList.Items.Add(word); } label.Text = "符合條件的記錄數:" result.Count.ToString(); } private void listBoxList_SelectedIndexChanged(object sender, EventArgs e) { int index = listBoxList.SelectedIndex; if (index < 0) { return; } object obj = listBoxList.Items[index]; SearchWordResult word = (SearchWordResult)obj; textBoxWord.Text = word.Word.Word; numericUpDownFrequency.Value = (decimal)word.Word.Frequency; posCtrl.Pos = (int)word.Word.Pos; } private void textBoxWord_TextChanged(object sender, EventArgs e) { String word = textBoxWord.Text.Trim(); if (word == "") { buttonUpdate.Enabled = false; buttonInsert.Enabled = false; buttonDelete.Enabled = false; return; } WordAttribute selWord = _WordDict.GetWordAttr(word); if (selWord != null) { buttonUpdate.Enabled = true; buttonInsert.Enabled = false; buttonDelete.Enabled = true; numericUpDownFrequency.Value = (decimal)selWord.Frequency; posCtrl.Pos = (int)selWord.Pos; } else { buttonUpdate.Enabled = false; buttonInsert.Enabled = true; buttonDelete.Enabled = false; numericUpDownFrequency.Value = 0; posCtrl.Pos = 0; } } private void buttonInsert_Click(object sender, EventArgs e) { _WordDict.InsertWord(textBoxWord.Text.Trim(), (double)numericUpDownFrequency.Value, (POS)posCtrl.Pos); MessageBox.Show("增加成功", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); ShowCount(); textBoxWord_TextChanged(sender, e); } private void buttonUpdate_Click(object sender, EventArgs e) { _WordDict.UpdateWord(textBoxWord.Text.Trim(), (double)numericUpDownFrequency.Value, (POS)posCtrl.Pos); MessageBox.Show("修改成功", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); ShowCount(); textBoxWord_TextChanged(sender, e); } private void buttonDelete_Click(object sender, EventArgs e) { if (MessageBox.Show("確認刪除改單詞?", "刪除提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) { _WordDict.DeleteWord(textBoxWord.Text.Trim()); MessageBox.Show("刪除成功", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); } ShowCount(); textBoxWord_TextChanged(sender, e); } private void BatchInsert(String fileName, String encoder) { String content = PanGu.Framework.File.ReadFileToString(fileName, Encoding.GetEncoding(encoder)); String[] words = PanGu.Framework.Regex.Split(content, @"\r\n"); bool allUse = false; WordAttribute lstWord = null; foreach (String word in words) { if (word == null) { continue; } if (word.Trim() == "") { continue; } string[] strs = word.Split(new char[] { '|' }); if (strs.Length == 3) { try { POS pos = (POS)int.Parse(strs[1].Substring(2, strs[1].Length - 2), System.Globalization.NumberStyles.HexNumber); double frequency = double.Parse(strs[2]); string w = strs[0].Trim(); _WordDict.InsertWord(w, frequency, pos); continue; } catch { } } FormBatchInsert frmBatchInsert = new FormBatchInsert(); if (!allUse || lstWord == null) { frmBatchInsert.Word.Word = word.Trim(); if (frmBatchInsert.ShowDialog() == DialogResult.OK) { lstWord = frmBatchInsert.Word; allUse = frmBatchInsert.AllUse; _WordDict.InsertWord(lstWord.Word, lstWord.Frequency, lstWord.Pos); } } else { lstWord.Word = word.Trim(); _WordDict.InsertWord(lstWord.Word, lstWord.Frequency, lstWord.Pos); } } } private void buttonBatchInsert_Click(object sender, EventArgs e) { openFileDialogDict.RestoreDirectory = true; openFileDialogDict.FileName = "*.txt"; openFileDialogDict.Filter = "Text files|*.txt"; if (openFileDialogDict.ShowDialog() == DialogResult.OK) { try { FormEncoder frmEncoder = new FormEncoder(); if (frmEncoder.ShowDialog() != DialogResult.OK) { return; } BatchInsert(openFileDialogDict.FileName, frmEncoder.Encoding); MessageBox.Show("批量增加成功,注意只有保存字典後,導入的單詞才會生效!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); ShowCount(); } catch(Exception e1) { MessageBox.Show(e1.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } private void ListWordsByPos(int pos) { List<SearchWordResult> result = _WordDict.SearchByPos((POS)pos); result.Sort(); listBoxList.Items.Clear(); foreach (SearchWordResult word in result) { listBoxList.Items.Add(word); } label.Text = "符合條件的記錄數:" result.Count.ToString(); } private void ListWordsByLength(int length) { List<SearchWordResult> result = _WordDict.SearchByLength(length); result.Sort(); listBoxList.Items.Clear(); foreach (SearchWordResult word in result) { listBoxList.Items.Add(word); } label.Text = "符合條件的記錄數:" result.Count.ToString(); } private void findToolStripMenuItem_Click(object sender, EventArgs e) { FormFind frmFind = new FormFind(); frmFind.ShowDialog(); switch (frmFind.Mode) { case FormFind.SearchMode.None: listBoxList.Items.Clear(); break; case FormFind.SearchMode.ByPos: ListWordsByPos(frmFind.POS); break; case FormFind.SearchMode.ByLength: ListWordsByLength(frmFind.Length); break; } } private void exportToolStripMenuItem_Click(object sender, EventArgs e) { if (saveFileDialogText.ShowDialog() == DialogResult.OK) { StringBuilder str = new StringBuilder(); foreach (object text in listBoxList.Items) { str.AppendLine(text.ToString()); } PanGu.Framework.File.WriteString(saveFileDialogText.FileName, str.ToString(), Encoding.UTF8); MessageBox.Show("Save OK!"); } } private void textBoxSearch_TextChanged(object sender, EventArgs e) { buttonSearch_Click(sender, e); } private void OpenAsTextToolStripMenuItem_Click(object sender, EventArgs e) { openFileDialogDict.RestoreDirectory = true; openFileDialogDict.FileName = "Dict.txt"; openFileDialogDict.Filter = "Dictionay file|*.txt"; if (openFileDialogDict.ShowDialog() == DialogResult.OK) { _Version = ""; try { DateTime old = DateTime.Now; _WordDict = new WordDictionary(); _WordDict.Load(openFileDialogDict.FileName, true, out _Version); TimeSpan s = DateTime.Now - old; statusStrip.Items[0].Text = s.TotalMilliseconds.ToString() "ms"; } catch (Exception e1) { MessageBox.Show(String.Format("Can not open dictionary, errmsg:{0}", e1.Message)); return; } panelMain.Enabled = true; m_DictFileName = openFileDialogDict.FileName; this.Text = "V" _Version " " openFileDialogDict.FileName; ShowCount(); } } private void SaveAsTextToolStripMenuItem_Click(object sender, EventArgs e) { if (_WordDict == null) { return; } saveFileDialogDict.RestoreDirectory = true; saveFileDialogDict.FileName = "Dict.txt"; saveFileDialogDict.Filter = "Dictionay file|*.txt"; if (saveFileDialogDict.ShowDialog() == DialogResult.OK) { _WordDict.SaveToText(saveFileDialogDict.FileName); } } } }
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论