2013-05-09 2 views
2

항목 목록이있는 콤보 상자가 있습니다. 사용자가 입력을 시작하면 드롭 다운이 열립니다. 키를 치면 콤보 상자의 텍스트를 확인해야합니다.Combobox에서 Enter 키를 누르면 텍스트가 지워집니다.

이 작업을 수행하려면 keydown 이벤트에서 DroppedDown = true를 설정하십시오. 그러나이 키를 치면 컴보 상자의 텍스트가 비어있게됩니다.

private void cmbItems_KeyDown(object sender, KeyEventArgs e) 
{ 
    if (!e.KeyCode.Equals(Keys.Enter)) { 
     if (!cmbItems.DroppedDown) { 
      cmbItems.DroppedDown = true; 
     } 
    } 
    else { 
     //Check the text 
    } 
} 

왜 콤보 상자 텍스트가 지워지고 그 주위에 방법이 있습니까?

내 사용자는 대신 AutoCompleteMode.SuggestAppend를 사용하지 않는 것이 좋지만 필요한 경우 알려 드리겠습니다.

전체 샘플 코드 :

public class Demo : Form { 
    private System.ComponentModel.IContainer components = null; 

    protected override void Dispose(bool disposing) 
    { 
     if (disposing && (components != null)) { 
      components.Dispose(); 
     } 
     base.Dispose(disposing); 
    } 

    private void InitializeComponent() 
    { 
     this.lblText = new System.Windows.Forms.Label(); 
     this.cmbItems = new System.Windows.Forms.ComboBox(); 
     this.SuspendLayout(); 
     // 
     // lblText 
     // 
     this.lblText.AutoSize = true; 
     this.lblText.Location = new System.Drawing.Point(152, 47); 
     this.lblText.Name = "lblText"; 
     this.lblText.Size = new System.Drawing.Size(155, 13); 
     this.lblText.TabIndex = 17; 
     this.lblText.Text = "Text"; 
     // 
     // cmbItems 
     // 
     this.cmbItems.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Append; 
     this.cmbItems.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems; 
     this.cmbItems.Location = new System.Drawing.Point(12, 44); 
     this.cmbItems.Name = "cmbItems"; 
     this.cmbItems.Size = new System.Drawing.Size(121, 21); 
     this.cmbItems.TabIndex = 0; 
     this.cmbItems.KeyDown += new System.Windows.Forms.KeyEventHandler(this.cmbItems_KeyDown); 
     // 
     // Demo 
     // 
     this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
     this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
     this.ClientSize = new System.Drawing.Size(338, 78); 
     this.Controls.Add(this.lblText); 
     this.Controls.Add(this.cmbItems); 
     this.Name = "Demo"; 
     this.Text = "Demo"; 
     this.ResumeLayout(false); 
     this.PerformLayout(); 

    } 

    #endregion 

    private System.Windows.Forms.Label lblText; 
    private ComboBox cmbItems; 

    DataTable _Items = new DataTable(); 

    public Demo() 
    { 
     InitializeComponent(); 

     BuildListOfItems(); 
    } 

    private void cmbItems_KeyDown(object sender, KeyEventArgs e) 
    { 
     if (!e.KeyCode.Equals(Keys.Enter)) { 
      if (!cmbItems.DroppedDown) { 
       cmbItems.DroppedDown = true; 
      } 
     } 
     else { 
      //Check the text 

     } 

     lblText.Text = cmbItems.Text; 
    } 

    //Create a demo list of items. 
    private void BuildListOfItems() 
    { 
     _Items.Columns.Add("Name").DataType = typeof(string); 
     _Items.Columns.Add("ID").DataType = typeof(int); 

     for (int i = (int)'a'; i < (int)'a' + 26; i++) { 
      CreateItem(i, "", 0); 
     } 
     cmbItems.DataSource = _Items; 
     cmbItems.ValueMember = "ID"; 
     cmbItems.DisplayMember = "Name"; 
    } 

    private void CreateItem(int symbol, string prev, int count) 
    { 
     string newPrev = string.Format("{0}{1}", prev, (char)symbol); 
     _Items.Rows.Add(newPrev, _Items.Rows.Count); 
     if (count < 4) 
      CreateItem(symbol + 1, newPrev, ++count); 

    } 
} 

답변

0

나는 대학에서 초기에이 중 일부를했고, 난 다시 가서 내 코드를 확인했습니다. 나는 100 % 확신하지는 못했지만 이것이 당신이 찾고 있던 것입니까?

private void comboCode_TextType(object sender, EventArgs e) 
    { 
     int itemsIndex = 0; 
     foreach (string item in cmbGageCode.Items) 
     { 
      if (item.IndexOf(cmbGageCode.Text) == 0) 
      { 
       cmbGageCode.SelectedIndex = itemsIndex; 
       cmbGageCode.Select(cmbGageCode.Text.Length - 1, 0); 
       break; 
      } 
      itemsIndex++; 
     } 
    } 
0

이것은 나를 위해 일했습니다.

string perfilText = ""; 
... 
private void cbPerfis_KeyUp(object sender, KeyEventArgs e) 
{ 
      if (e.KeyData != Keys.Enter) 
      {     
       perfilText = cbPerfis.Text; 
      } 
      else 
      { 
       cbPerfis.Text = perfilText; 
       cbOrdens.Focus(); 
      } 
} 
관련 문제