2011-10-05 32 views
3

在Compact Framework中,我想在下拉列表打開時更改ComboBox的ItemIndex。我試圖從LostFocus或KeyPress事件中改變它,它似乎工作,但是當下拉列表關閉時,該值返回到原始值。當下拉列表打開時,在Comobox中更改ItemIndex

例如:

private void comboBox1_KeyPress(object sender, KeyPressEventArgs e) 
    { 
     if (e.KeyChar == (char)Keys.Tab) 
      return; 
     if (e.KeyChar == 'A') 
     { 
      e.Handled = true; 
      comboBox1.SelectedIndex = 2; 
     } 
    } 

當我按下A,有效的項目#2被選擇和文本,但是當我移動到其中的一個控件或乾脆關閉下拉列表,組合框的變化前一個值。

謝謝

+1

嘗試訂閱SelectedIndexChanged事件並在那裏記錄堆棧跟蹤以查看關閉組合時誰更改值。你可以使用try {throw new Exception(); } catch(Exception e){Debug.WriteLine(e.StackTrace); }來打印堆棧跟蹤。 – Damon8or

+0

追蹤並沒有太多幫助。在我看來,SelectedIndex無法在KeyPress事件中更改:SmartDeviceProject1.Form1.comboBox1_SelectedIndexChanged(Object sender,EventArgs e) System.Windows.Forms.ComboBox.OnSelectedIndexChanged(EventArgs e) System.Windows.Forms.ComboBox.WnProc( WM wm,Int32 wParam,Int32 lParam) System.Windows.Forms.Control._InternalWnProc(WM wm,Int32 wParam,Int32 lParam) Microsoft.AGL.Forms.EVL.EnterMainLoop(IntPtr hwnMain) System.Windows.Forms。 Application.Run(Form fm) –

+0

您是否嘗試過使用SelectedValue屬性而不是(或與)SelectedIndex? – Arie

回答

0

我只是想用下面的代碼複製您的問題(但在FF運行),它是工作的罰款:

using System; 
using System.Windows.Forms; 

namespace combotest 
{ 
    class MainClass 
    { 
     public static void Main (string[] args) 
     { 
      WinForm form = new WinForm(); 
      Application.Run (form); 
      //Console.WriteLine("Hello World!"); 
     } 
    } 
    public class WinForm : Form 
    { 
     public WinForm() 
     { 
      InitializeComponent(); 
     } 
     ComboBox comboBox1; 
     TextBox textBox1; 
     private void InitializeComponent() 
     { 
      this.Width = 400; 
      this.Height = 300; 
      this.Text = "My Dialog"; 
      Button btnOK = new Button(); 
      btnOK.Text = "OK"; 
      btnOK.Location = new System.Drawing.Point (10, 10); 
      btnOK.Size = new System.Drawing.Size (80, 24); 
      this.Controls.Add (btnOK); 
      btnOK.Click += new EventHandler (btnOK_Click); 
      comboBox1=new ComboBox(); 
      comboBox1.Location = new System.Drawing.Point (10, 50); 
      comboBox1.Size = new System.Drawing.Size (80, 24); 
      comboBox1.DropDownStyle=ComboBoxStyle.DropDownList; 
      this.Controls.Add (comboBox1); 

      textBox1=new TextBox(); 
      textBox1.Location = new System.Drawing.Point (100, 50); 
      textBox1.Size = new System.Drawing.Size (80, 24); 
      this.Controls.Add (textBox1); 

      this.SuspendLayout(); 
      String[] iList=new String[]{"text0","text1","text2","text3","text4"}; 
      comboBox1.Items.AddRange(iList); 
      comboBox1.SelectedIndex=0; 
      this.ResumeLayout();    
      comboBox1.KeyPress+=new KeyPressEventHandler(comboBox1_KeyPress); 
     } 

     private void comboBox1_KeyPress(object sender, KeyPressEventArgs e) 
     { 
      if (e.KeyChar == (char)Keys.Tab) 
       return; 
      if (e.KeyChar.ToString().ToUpper() == "A") 
      { 
       e.Handled = true; 
       comboBox1.SelectedIndex = 2; 
       textBox1.Text=comboBox1.SelectedItem.ToString(); 
      } 
     } 
     private void btnOK_Click (object sender, System.EventArgs e) 
     { 
      this.DialogResult = DialogResult.OK; 
      this.Close(); 
     } 

    } 
} 

所以我假定你有一些額外的代碼或附加到組合框的事件,或者它在FF中的行爲有所不同。

您可以在您的PC的文件資源管理器中進入bin \ Debug目錄,然後在PC上雙擊exe文件啓動您的SmartDevice應用程序,以測試您的應用程序是否也在FF內運行。通常情況下(沒有引用特殊的DLL),它也應該在PC上運行,因爲CF與FF的下行兼容。

如果您仍有問題,請發佈最小化的代碼示例來說明您的問題。