*/ 出自: 编程中国 http://www.bc-cn.net
*/ 作者: C_B_Lu QQ:184118549
*/ 时间: 2007-9-4 编程论坛首发
*/ 声明: 尊重作者劳动,转载请保留本段文字
*/ --------------------------------------------------------------------------------------
常有这种情况,在一个ComboBox控件的左侧,放一个TextBox控件,用于存放ComboBox所选项的值(即SelectedValue属性).
同时ComboBox控件还允许直接在其中输入内容来搜索.
private void cbUnitName_SelectedIndexChanged(object sender, EventArgs e)
{
if ((cbUnitName.Text != "") && (cbClientName.SelectedIndex > -1))
{
tbUnitID.Text = cbUnitName.SelectedValue.ToString().Trim();
}
}
private void cbUnitName_Validating(object sender, CancelEventArgs e)
{
if (currentState == state.Browse)
{
return;
}
errorProvider1.SetError(cbUnitName, "");
string strUnitName = cbUnitName.Text.Trim();
if (strUnitName == "")
{
tbUnitID.Text = cbUnitName.Text = "";
return;
}
cbUnitName.SelectedIndex = cbUnitName.FindString(strUnitName);
if (cbUnitName.SelectedIndex < 0)
{
errorProvider1.SetError(cbUnitName, "單位有誤,請重新輸入!");
cbUnitName.SelectAll();
cbUnitName.Focus();
e.Cancel = true;
}
}