功能:
用户在textbox中输入字符串;
软件自动判断输入的字符串与数据源中的字符串列表是否有相匹配的,如果有则将相匹配的字符串显示在提示界面中;
提示界面的位置永远在光标的后方;
工程为WPF工程,主界面中放了一个TextBox,提示界面是一个WPF界面,里面放了一个ListBox。
主界面的主要代码如下:
public partial class MainWindow : Window
{
private struct POINT
{
public int x;
public int y;
}
private PopupWnd popupWindow = new PopupWnd();
List<string> lstSourceStr;//所有备选字符串的队列
public MainWindow()
{
lstSourceStr = new List<string>();
lstSourceStr.Add("Apple");
lstSourceStr.Add("Almond");//杏树
lstSourceStr.Add("Banana");
lstSourceStr.Add("Berry");//果酱
lstSourceStr.Add("Cherry");//樱桃
InitializeComponent();
}
[DllImport("User32", EntryPoint = "GetCaretPos", SetLastError = true,
ExactSpelling = true, CharSet = CharSet.Auto)]
private static extern int GetCaretPos(out POINT lpPoint);//获取光标位置坐标
private string m_SelectionStr = "";
private int m_nSelectionStart = 0;
private int m_nSelectionLen = 0;
private bool m_bTextChanged = false;
private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
if (!m_bTextChanged)//判断m_bTextChanged是因为程序初始化的时候会触发该函数,但初始化的时候本窗口还未初始化完成,运行下面的代码会报异常
return;
POINT ptCursor;
GetCaretPos(out ptCursor);//获取光标坐标位置
Point ptIntelliSense = new Point(ptCursor.x, ptCursor.y);
popupWindow.Left = this.Left + ptIntelliSense.X + 5;
popupWindow.Top = this.Top + ptIntelliSense.Y + 2 + textBox1.FontSize;
m_nSelectionStart = 0;//光标所在点的字符串起始位置
m_nSelectionLen = 0;//光标所在点的字符串长度
FindSelection(textBox1.Text, textBox1.SelectionStart, ref m_nSelectionStart, ref m_nSelectionLen);//获取光标所在点的字符串起始位置m_nSelectionStart和长度m_nSelectionLen
m_SelectionStr = textBox1.Text.Substring(m_nSelectionStart, m_nSelectionLen);//获取光标所在点的字符串
popupWindow.SenseLst = GetRelateStings(m_SelectionStr, lstSourceStr);//获取与用户输入的字符串相关的备选字符串队列
if (popupWindow.SenseLst.Count > 0)
{
popupWindow.Owner = this;
popupWindow.Show();
}
else
{
popupWindow.Hide();
}
m_bTextChanged = false;
this.Activate();//激活本窗口
}
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
m_bTextChanged = true;
}
//获取与第一个string相关的string的队列
private List<string> GetRelateStings(string str1, List<string> lstStringSource)
{
List<string> lstStringOut = new List<string>();
foreach (string str in lstStringSource)
{
if ((str1.Length <= str.Length) && (str1.Length > 0))
{
string tempStr = str.Substring(0, str1.Length);
if (tempStr.ToLower() == str1.ToLower())
{
lstStringOut.Add(str);
}
}
}
return lstStringOut;
}
//获取光标所在点的字符串起始位置nStart和长度nLen
private void FindSelection(string strText, int nFindFrom, ref int nStart, ref int nLen)
{
char[] cFind = new char[] { '.', ' ', '\t', '\n', '\r', '(', ')', '+', '-', '*', '/', ',' };
int nIndex = -1;
if (nFindFrom <= strText.Length)
{
string strTemp = new string(strText.Reverse().ToArray());
nIndex = strTemp.IndexOfAny(cFind, strText.Length - nFindFrom);
}
if (nIndex == -1)
nStart = 0;
else
nStart = strText.Length - nIndex;
int nEnd = -1;
if (nFindFrom < strText.Length)
nEnd = strText.IndexOfAny(cFind, nFindFrom);
if (nEnd == -1)
nLen = strText.Length - nStart;
else
nLen = nEnd - nStart;
}
}
提示界面主要代码如下:
public partial class PopupWnd : Window
{
private List<string> lstContent;//存放字符串数据源
public List<string> SenseLst//存放字符串数据源
{
get
{
return lstContent;
}
set
{
lstContent = value;
listContent.ItemsSource = lstContent;
}
}
public PopupWnd()
{
InitializeComponent();
}
private void listContent_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("you have choosen one related string");
this.Hide();
}
}
源码地址:
https://download.csdn.net/download/weixin_43935474/12099465