组合框简约风格 [英] Combo Box Simple Style

查看:58
本文介绍了组合框简约风格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个Win表单。


我的组合框DropDownStyle设置为简单。当用户在组合框中键入3

字符串时,集合中最近的项目显示为

,就在用户输入3个字符串的位置下方。


当用户离开组合框时,我希望它自动选择

项目,该项目位于用户在3中输入的正下方的集合中

字符串。

This is for a Win form.

My combo box DropDownStyle is set to simple. When the user types in a 3
character string in the combo box, the closest item in the collection is
shown, right below where the user typed in the 3 character string.

When the user leaves the combo box I want it to automatically select the
item in the collection that is right below where the user typed in the 3
character string.

推荐答案

嗨Cadel,


您可以处理ComboBox.LostFocus事件。当事件触发时,只需设置

ComboBox.Text = ComboBox.SelectedItem.ToString()。


HTH。


Kevin Yu

=======

此帖子已提供按现状没有保证,并且不授予

权利。

Hi Cadel,

You can handle the ComboBox.LostFocus event. When the event fires, just set
the ComboBox.Text = ComboBox.SelectedItem.ToString().

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."


我收到错误消息,未设置对象引用到一个

对象的实例。关于你给我的代码,cboPrivilege.Text =

cboPrivilege.SelectedItem.ToString();"


这是我的组合代码框和我在组合后的按钮

框。


private void cboPrivilege_Leave(object sender,System.EventArgs e)

{

cboPrivilege.Text = cboPrivilege.SelectedItem.ToString();

}


private void cmdAddPrivileges_Click(对象发送者) ,System.EventArgs e)

{


DataRowView drv = cboPrivilege.SelectedItem as DataRowView;


int intSalesRevKey =(int)drv [0];

string sLicenseCode =(string)drv [1];

object flGrossFee = drv [2];


DataTable dt = dgPrivileges.DataSource as DataTable;


DataRow dr = dt.NewRow();


dr [ 0] = intSalesRevKey;

dr [1] = sLicenseCode;

dr [2] = flGrossFee;


dt.Rows .Add(dr);


cboPrivile ge.Focus();

decimal calTotal = Convert.ToDecimal(txtTotal.Text)+

Convert.ToDecimal(flGrossFee);

txtTotal .Text = calTotal.ToString();


}


我试过了


" Kevin Yu [MSFT]"写道:
I get an error message, "Object reference not set to an instance of an
object." on the code you gave me, "cboPrivilege.Text =
cboPrivilege.SelectedItem.ToString();"

Here is my code for the combo box and for the button I have after the combo
box.

private void cboPrivilege_Leave(object sender, System.EventArgs e)
{
cboPrivilege.Text = cboPrivilege.SelectedItem.ToString();
}

private void cmdAddPrivileges_Click(object sender, System.EventArgs e)
{

DataRowView drv = cboPrivilege.SelectedItem as DataRowView;

int intSalesRevKey = (int)drv[0];
string sLicenseCode = (string)drv[1];
object flGrossFee = drv[2];

DataTable dt = dgPrivileges.DataSource as DataTable;

DataRow dr = dt.NewRow();

dr[0] = intSalesRevKey;
dr[1] = sLicenseCode;
dr[2] = flGrossFee;

dt.Rows.Add(dr);

cboPrivilege.Focus();
decimal calTotal = Convert.ToDecimal(txtTotal.Text) +
Convert.ToDecimal(flGrossFee);
txtTotal.Text = calTotal.ToString();

}

I tried

"Kevin Yu [MSFT]" wrote:
嗨Cadel,

您可以处理ComboBox.LostFocus事件。当事件触发时,只需设置
ComboBox.Text = ComboBox.SelectedItem.ToString()。

HTH。

Kevin Yu
== =====
此帖子已提供按原样没有保证,也没有授予
权利。
Hi Cadel,

You can handle the ComboBox.LostFocus event. When the event fires, just set
the ComboBox.Text = ComboBox.SelectedItem.ToString().

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."



嗨Cadel,


如果此时抛出NullReferenceException,请检查集合中是否设置了

项。如果没有,请尝试在尝试搜索输入的代码中选择

所需项目。您可以使用

ComboBox.SelectedItem或使用ComboBox.SelectedIndex选择

与输入字符串匹配的项目。


Kevin Yu

=======

此帖子已提供按现状没有保证,也没有赋予

权利。

Hi Cadel,

If a NullReferenceException is thrown at this point, please check if an
item in the collection has been set. If not, please try to select the
desired item in the code that try to search for the input. You can use
ComboBox.SelectedItem or use ComboBox.SelectedIndex to select the item that
matches the input strings.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."


这篇关于组合框简约风格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆