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);
        }

    }
  TextBox_Bind_ComboBox在TextBox中加一控件ComboBox,当TextBox绑定到表的外键字段(假如学生档案表的班级ID_Point字段),而ComboBox绑定到班级档案表的班级ID和班级名称字段,则当学生档案表的记录移动时,ComboBox将根据TextBox中的班级ID_Point字段,查找班级档案表中班级ID项,并显示班级名称。
  程序代码使用没有错误,只是显示时效果不太好,因为ComboBox挤在TextBox内部,感觉不好,所以作了修改,有了后面的TextBox_Bind_TextBoxComboBox类
posted on 2005-04-02 17:31  爱好  阅读(982)  评论(0)    收藏  举报