[C#]DictService
namespace DictServceDemo { public partial class Form1 : Form {
#region Var
private DictService _dictService;
#endregion
#region Private Property
public DictService m_DictService
{
get
{
if (_dictService == null)
_dictService = new DictService();
return _dictService;
}
}
#endregion
#region Constructor
public Form1()
{
InitializeComponent();
}
#endregion
#region Event Process
private void Form1_Load(object sender, EventArgs e)
{
cbxDict.DisplayMember = "Name";
cbxDict.DataSource = m_DictService.DictionaryList();
cbxStrategy.DisplayMember = "Id";
cbxStrategy.DataSource = m_DictService.StrategyList();
}
private void lbxResult_SelectedIndexChanged(object sender, EventArgs e)
{
if (cbxDict.SelectedItem == null)
return;
if (lbxResult.SelectedItem == null)
return;
var dict = cbxDict.SelectedItem as Dictionary;
var dictWord = lbxResult.SelectedItem as DictionaryWord;
lbxDefine.DisplayMember = "WordDefinition";
lbxDefine.DataSource = m_DictService.DefineInDict(dict.Id, dictWord.Word).Definitions;
}
private void tbxKeyword_KeyUp(object sender, KeyEventArgs e)
{
if (cbxDict.SelectedItem == null)
return;
if (cbxStrategy.SelectedItem == null)
return;
lbxDefine.DataSource = null;
if (tbxKeyword.Text.Length == 0)
{
lbxResult.DataSource = null;
return;
}
var dict = cbxDict.SelectedItem as Dictionary;
var strategy = cbxStrategy.SelectedItem as Strategy;
lbxResult.DisplayMember = "Word";
lbxResult.DataSource = m_DictService.MatchInDict(dict.Id, tbxKeyword.Text, strategy.Id);
}
#endregion
}
}
namespace WindowsFormsApplication6 { public partial class Form1 : Form {
#region Var
private AutoCompleteStringCollection _autoCompleteSource;
private DictService _dictService;
#endregion
#region Private Property
private AutoCompleteStringCollection m_AutoCompleteSource
{
get
{
if (_autoCompleteSource == null)
_autoCompleteSource = new AutoCompleteStringCollection();
return _autoCompleteSource;
}
set
{
_autoCompleteSource = value;
}
}
private DictService m_DictService
{
get
{
if (_dictService == null)
{
_dictService = new DictService();
_dictService.MatchInDictCompleted += new MatchInDictCompletedEventHandler(m_DictService_MatchInDictCompleted);
}
return _dictService;
}
}
#endregion
public Form1()
{
InitializeComponent();
textBox1.AutoCompleteMode = AutoCompleteMode.Append;
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
}
#region Event Process
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (textBox1.Text.Length == 0)
return;
textBox1.AutoCompleteCustomSource = m_AutoCompleteSource;
var userState = new object();
m_DictService.CancelAsync(userState);
m_DictService.MatchInDictAsync("wn", textBox1.Text, "prefix", userState);
}
void m_DictService_MatchInDictCompleted(object sender, MatchInDictCompletedEventArgs e)
{
if (e.Cancelled)
return;
m_AutoCompleteSource = new AutoCompleteStringCollection();
m_AutoCompleteSource.AddRange(e.Result.Select((item) => item.Word).ToArray());
}
#endregion
}
}