如何在combobox text 输入时,自动弹出下拉框并匹配文字?
4个回答
展开全部
Dim strItem(1 To 13) As String
Private Sub Combo1_Change()
Dim i As Integer '定义循环变量
For i = 1 To Me.Combo1.ListCount '清除列表框(不能用clear方法)
Me.Combo1.RemoveItem 0
Next
For i = LBound(strItem) To UBound(strItem) '遍历查找是否有符合条件的项
If (Len(strItem(i)) >= Len(Me.Combo1.Text)) _
And (Me.Combo1.Text = Mid(strItem(i), 1, Len(Me.Combo1.Text))) _
Then Me.Combo1.AddItem strItem(i)
Next
End Sub
Private Sub Form_Load()
MsgBox "请确保已经将Combol1的Style属性设置为1"
Me.Combo1.Height = 1800
Me.Combo1.Clear
strItem(1) = "abbreviation"
strItem(2) = "excessively"
strItem(3) = "competitor"
strItem(4) = "gigantic"
strItem(5) = "unanimous"
strItem(6) = "complement"
strItem(7) = "shameful"
strItem(8) = "platform"
strItem(9) = "concentrate"
strItem(10) = "shipment"
strItem(11) = "shoulder"
strItem(12) = "uranium"
strItem(13) = "alignment"
End Sub
推荐于2016-01-12 · 知道合伙人数码行家
可以叫我表哥
知道合伙人数码行家
向TA提问 私信TA
知道合伙人数码行家
采纳数:25897
获赞数:1464978
2010年毕业于北京化工大学北方学院计算机科学与技术专业毕业,学士学位,工程电子技术行业4年从业经验。
向TA提问 私信TA
关注
展开全部
可以使用这个方法来实现:
//在KeyPress事件处理里面写
private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
{
AutoComplete((ComboBox)sender, e);
}
//然后添加如下方法
// AutoComplete
public void AutoComplete(ComboBox cb, System.Windows.Forms.KeyPressEventArgs e)
{
this.AutoComplete(cb, e, false);
}
public void AutoComplete(ComboBox cb, System.Windows.Forms.KeyPressEventArgs e, bool blnLimitToList)
{
string strFindStr = "";
if (e.KeyChar == (char)8)
{
if (cb.SelectionStart <= 1)
{
cb.Text = "";
return;
}
if (cb.SelectionLength == 0)
strFindStr = cb.Text.Substring(0, cb.Text.Length - 1);
else
strFindStr = cb.Text.Substring(0, cb.SelectionStart - 1);
}
else
{
if (cb.SelectionLength == 0)
strFindStr = cb.Text + e.KeyChar;
else
strFindStr = cb.Text.Substring(0, cb.SelectionStart) + e.KeyChar;
}
int intIdx = -1;
// Search the string in the ComboBox list.
intIdx = cb.FindString(strFindStr);
if (intIdx != -1)
{
cb.SelectedText = "";
cb.SelectedIndex = intIdx;
cb.SelectionStart = strFindStr.Length;
cb.SelectionLength = cb.Text.Length;
e.Handled = true;
}
else
{
e.Handled = blnLimitToList;
}
}
//在KeyPress事件处理里面写
private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
{
AutoComplete((ComboBox)sender, e);
}
//然后添加如下方法
// AutoComplete
public void AutoComplete(ComboBox cb, System.Windows.Forms.KeyPressEventArgs e)
{
this.AutoComplete(cb, e, false);
}
public void AutoComplete(ComboBox cb, System.Windows.Forms.KeyPressEventArgs e, bool blnLimitToList)
{
string strFindStr = "";
if (e.KeyChar == (char)8)
{
if (cb.SelectionStart <= 1)
{
cb.Text = "";
return;
}
if (cb.SelectionLength == 0)
strFindStr = cb.Text.Substring(0, cb.Text.Length - 1);
else
strFindStr = cb.Text.Substring(0, cb.SelectionStart - 1);
}
else
{
if (cb.SelectionLength == 0)
strFindStr = cb.Text + e.KeyChar;
else
strFindStr = cb.Text.Substring(0, cb.SelectionStart) + e.KeyChar;
}
int intIdx = -1;
// Search the string in the ComboBox list.
intIdx = cb.FindString(strFindStr);
if (intIdx != -1)
{
cb.SelectedText = "";
cb.SelectedIndex = intIdx;
cb.SelectionStart = strFindStr.Length;
cb.SelectionLength = cb.Text.Length;
e.Handled = true;
}
else
{
e.Handled = blnLimitToList;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Dim strItem(1 To 13) As String
Private Sub Combo1_Change()
SendMessage Combo1.hwnd, &H14F, 1, 0 '没有这句,combo会自动对text进行先择,让第二次无法连接输入字符。
Dim i As Integer '定义循环变量
For i = 1 To Me.Combo1.ListCount '清除列表框(不能用clear方法)
Me.Combo1.RemoveItem 0
Next
For i = LBound(strItem) To UBound(strItem) '遍历查找是否有符合条件的项
If InStr(strItem(i), Combo1.Text) <> 0 Then
Me.Combo1.AddItem strItem(i)
End If
Next
Combo1.Refresh
Form1.Refresh
SendMessage Combo1.hwnd, &H14F, 1, 0 '关键代码,调出下拉列表框。
End Sub
Private Sub Form_Load()
Me.Combo1.Clear
strItem(1) = "abbreviation"
strItem(2) = "excessively"
strItem(3) = "competitor"
strItem(4) = "gigantic"
strItem(5) = "unanimous"
strItem(6) = "complement"
strItem(7) = "shameful"
strItem(8) = "platform"
strItem(9) = "concentrate"
strItem(10) = "shipment"
strItem(11) = "shoulder"
strItem(12) = "uranium"
strItem(13) = "alignment"
End Sub
用SendMessage Combo1.hwnd, &H14F, 1, 0 可以直接呼出下拉列表框。
Dim strItem(1 To 13) As String
Private Sub Combo1_Change()
SendMessage Combo1.hwnd, &H14F, 1, 0 '没有这句,combo会自动对text进行先择,让第二次无法连接输入字符。
Dim i As Integer '定义循环变量
For i = 1 To Me.Combo1.ListCount '清除列表框(不能用clear方法)
Me.Combo1.RemoveItem 0
Next
For i = LBound(strItem) To UBound(strItem) '遍历查找是否有符合条件的项
If InStr(strItem(i), Combo1.Text) <> 0 Then
Me.Combo1.AddItem strItem(i)
End If
Next
Combo1.Refresh
Form1.Refresh
SendMessage Combo1.hwnd, &H14F, 1, 0 '关键代码,调出下拉列表框。
End Sub
Private Sub Form_Load()
Me.Combo1.Clear
strItem(1) = "abbreviation"
strItem(2) = "excessively"
strItem(3) = "competitor"
strItem(4) = "gigantic"
strItem(5) = "unanimous"
strItem(6) = "complement"
strItem(7) = "shameful"
strItem(8) = "platform"
strItem(9) = "concentrate"
strItem(10) = "shipment"
strItem(11) = "shoulder"
strItem(12) = "uranium"
strItem(13) = "alignment"
End Sub
用SendMessage Combo1.hwnd, &H14F, 1, 0 可以直接呼出下拉列表框。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
AutoCompleteMode属性设置为SuggestAppendAutoCompleteSoure设置为ListItems就可以了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询