真的没有,有的话,把DropDownStyle改为DropDownList也不应该好用

解决方案 »

  1.   

    我刚才试了一下,我在的窗体中加的ComboBox没这个毛病。
    而我用的UserControl,在这里面加的ComboBox有这个毛病,不知道为什么
      

  2.   

    我这里没有问题呀,试试打个Pack2?
      

  3.   

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Windows.Forms;
    using System.Xml;
    using System.Data.OleDb;
    namespace TravelLibrary
    {
    /// <summary>
    /// UserControl1 的摘要说明。
    /// </summary>
    public class UstSerch : System.Windows.Forms.UserControl
    {
    private string[,] Area;
    /// <summary> 
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null;
            private DataSet ds=new DataSet();
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.Label label3;
    private System.Windows.Forms.TextBox TxtName;
    private System.Windows.Forms.ComboBox CmbProvice;
    private System.Windows.Forms.ComboBox CmbCity;
    private ArrayList alist=new ArrayList();
    public UstSerch()
    {
    // 该调用是 Windows.Forms 窗体设计器所必需的。
    InitializeComponent(); // TODO: 在 InitializeComponent 调用后添加任何初始化 } /// <summary> 
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if(components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Component Designer generated code
    /// <summary> 
    /// 设计器支持所需的方法 - 不要使用代码编辑器 
    /// 修改此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.label1 = new System.Windows.Forms.Label();
    this.label2 = new System.Windows.Forms.Label();
    this.label3 = new System.Windows.Forms.Label();
    this.TxtName = new System.Windows.Forms.TextBox();
    this.CmbProvice = new System.Windows.Forms.ComboBox();
    this.CmbCity = new System.Windows.Forms.ComboBox();
    this.SuspendLayout();
    // 
    // label1
    // 
    this.label1.AutoSize = true;
    this.label1.Location = new System.Drawing.Point(208, 8);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(42, 14);
    this.label1.TabIndex = 3;
    this.label1.Text = "省份:";
    // 
    // label2
    // 
    this.label2.AutoSize = true;
    this.label2.Location = new System.Drawing.Point(352, 8);
    this.label2.Name = "label2";
    this.label2.Size = new System.Drawing.Size(42, 14);
    this.label2.TabIndex = 4;
    this.label2.Text = "城市:";
    // 
    // label3
    // 
    this.label3.AutoSize = true;
    this.label3.Location = new System.Drawing.Point(8, 8);
    this.label3.Name = "label3";
    this.label3.Size = new System.Drawing.Size(42, 14);
    this.label3.TabIndex = 3;
    this.label3.Text = "名称:";
    // 
    // TxtName
    // 
    this.TxtName.Location = new System.Drawing.Point(56, 5);
    this.TxtName.Name = "TxtName";
    this.TxtName.Size = new System.Drawing.Size(136, 21);
    this.TxtName.TabIndex = 5;
    this.TxtName.Text = "";
    // 
    // CmbProvice
    // 
    this.CmbProvice.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
    this.CmbProvice.Location = new System.Drawing.Point(256, 5);
    this.CmbProvice.Name = "CmbProvice";
    this.CmbProvice.Size = new System.Drawing.Size(80, 20);
    this.CmbProvice.TabIndex = 6;
    this.CmbProvice.SelectedIndexChanged += new System.EventHandler(this.CmbProvice_SelectedIndexChanged);
    // 
    // CmbCity
    // 
    this.CmbCity.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
    this.CmbCity.Location = new System.Drawing.Point(400, 5);
    this.CmbCity.Name = "CmbCity";
    this.CmbCity.Size = new System.Drawing.Size(88, 20);
    this.CmbCity.TabIndex = 7;
    this.CmbCity.SelectedIndexChanged += new System.EventHandler(this.CmbCity_SelectedIndexChanged);
    // 
    // UstSerch
    // 
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
      this.CmbCity,
      this.CmbProvice,
      this.TxtName,
      this.label2,
      this.label1,
      this.label3});
    this.Name = "UstSerch";
    this.Size = new System.Drawing.Size(504, 40);
    this.Load += new System.EventHandler(this.UstSerch_Load);
    this.ResumeLayout(false); }
    #endregion private void UstSerch_Load(object sender, System.EventArgs e)
    {
    // string conn="Provider=Microsoft.jet.oledb.4.0;data source=traveldb.mdb";
    //            System.Data.OleDb.OleDbConnection myConn=new OleDbConnection(conn);
    //            myConn.Open();
    // string strsql="select T_B_Privence_Infor.strName as province,T_B_City_Infor.strName as city from T_B_Privence_Infor inner join T_B_City_Infor on T_B_City_Infor.lngPrivenceID=T_B_Privence_Infor.lngid order by T_B_Privence_Infor.strName";
    // OleDbDataAdapter myAdapter=new OleDbDataAdapter(strsql,myConn);
    // myAdapter.Fill(ds);
    // ds.WriteXml("area.xml");
    // XmlDocument xmldoc = new XmlDocument();
    // xmldoc.Load("area.xml");
    // XmlNodeList nodelist = xmldoc.SelectNodes("//Table[province='安徽']");
    // MessageBox.Show(nodelist.Count.ToString());
    // foreach(XmlNode node in nodelist)
    // {
    // MessageBox.Show(node.ChildNodes.Item(2).InnerText);
    // }
    //this.CmbCity.DataSource=nodelist;
    ds.ReadXml(Application.StartupPath+"\\xml\\area.xml");
    Area=new string[ds.Tables[0].Rows.Count,2];
    //this.dataGrid1.SetDataBinding(ds,ds.Tables[0].TableName);
    for(int i=0;i<ds.Tables[0].Rows.Count;i++)
    {
                   Area[i,0]=ds.Tables[0].Rows[i][0].ToString();
                   Area[i,1]=ds.Tables[0].Rows[i][1].ToString();
    }
    ProvinceLoad(); }
    private void ProvinceLoad()
    {
    this.CmbProvice.Items.Clear();
    for (int i=0;i<Area.Length;i++)
    {
    try
    {
    if (Area[i-1,0]!=Area[i,0])
    {
    this.CmbProvice.Items.Add(Area[i,0]);
    }
    }
    catch
    {
    try
    {
    this.CmbProvice.Items.Add(Area[i,0]);
    }
    catch
    {
    }
    }
    this.CmbProvice.SelectedIndex=0; }
    }
    private void CityLoad(string Province)
    {
    this.CmbCity.Items.Clear();
    // this.CmbCity.DataSource=null;
    // alist.Clear();
    for (int i=0;i<Area.Length;i++)
    {
    try
    {
    if(Area[i,0].Trim()==Province.Trim())
    {

    this.CmbCity.Items.Add(Area[i,1]);
    //     alist.Add(Area[i,1].Trim());
    }
    }
    catch
    {
    }
    }
    //this.CmbCity.DataSource=alist;
               this.CmbCity.SelectedIndex=0;
    }
    private void CmbProvice_SelectedIndexChanged(object sender, System.EventArgs e)
    {
                 CityLoad(this.CmbProvice.Text);
    } private void CmbCity_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    // MessageBox.Show(this.CmbCity.SelectedIndex.ToString());

        //this.CmbCity.Text=this.CmbCity.SelectedItem.ToString();
    }
    }
    }以上是我的代码,其中读了个xml,方法不见得好,还请大家指教。
    当我把这个UserControl加到我窗体中的时候,点击其中的项目,文字上不去
      

  4.   

    7710703(易水寒) 你是这样做的吗:
    将ComboBox加入UserControl
    然后在需要Usercontrol的窗体中添加panel,然后生成UserControl的新实例,最后用Panel.Controls.add(UserControl的实例)