Winform下Textbox的样式(2)—TextBox_Bind_ComboBox
public class TextBox_Bind_ComboBox:TextBox_Bind
{//不推荐使用
System.Windows .Forms .ComboBox myComboBox=new ComboBox ();
public DataView ChoiceDataViewSource=null;
public string ChoiceDisplayField=null;
public string ChoiceValueField=null;
public TextBox_Bind_ComboBox( ):base()
{
//throw new System .Exception ("不推荐使用,可使用TextBox_Bind_TextBoxComboBox");
}
public new void Bind()
{
myComboBox.DataSource =ChoiceDataViewSource;
//base.BorderStyle =System.Windows .Forms .BorderStyle .None ;
myComboBox.DisplayMember =ChoiceDisplayField;
myComboBox.ValueMember =ChoiceValueField;
base.Controls .Add (myComboBox);
//myComboBox.SelectedIndexChanged +=new System .EventHandler (SetTextBoxValue);
base.TextChanged +=new System .EventHandler (ContentChanged);
myComboBox.SelectionChangeCommitted +=new System.EventHandler (SetTextBoxValue);
myComboBox.Dock =DockStyle.Fill ;
//.SelectedIndex =-1;
base.Bind ();
myComboBox.Text =null;
//myComboBox.SelectedIndexChanged +=new EventHandler(myComboBox_SelectedIndexChanged);
}
protected int Find(System.Object value)
{
int int1;
int int2;
int2=ChoiceDataViewSource.Count ;
if (int2<=0)
{
return -1;
}
for (int1=0;int1<int2;int1++)
{
if (ChoiceDataViewSource[int1][this.myComboBox .ValueMember ].ToString ().Trim ()==value.ToString ().Trim ())
{
return int1;
}
}
return -1;
}
new public void Show()
{
myComboBox.SelectedIndex =Find(base.Text);
base.Show ();
myComboBox.Height =base.Height ;
myComboBox.Top =-2;
}
private void ContentChanged(object sender,System.EventArgs e)
{
int Temp=Find(base.Text);
if (Temp>=0)
{
myComboBox.SelectedIndex =Temp;
}
else
{
myComboBox.SelectedIndex =Temp;
myComboBox.Text ="";
}
}
private void SetTextBoxValue(object sender, System.EventArgs e)
{
base.TextChanged -=new System .EventHandler (ContentChanged);
base.Text =myComboBox.SelectedValue.ToString ();
int Temp=base.BindingContext[BindDataViewSource].Position;
base.BindDataViewSource [Temp][base.BindField]=myComboBox.SelectedValue;
base.TextChanged +=new System .EventHandler (ContentChanged);
}
}程序代码使用没有错误,只是显示时效果不太好,因为ComboBox挤在TextBox内部,感觉不好,所以作了修改,有了后面的TextBox_Bind_TextBoxComboBox类

浙公网安备 33010602011771号