当下拉列表打开时,在Comobox中更改ItemIndex

本文关键字:Comobox ItemIndex 下拉列表 | 更新日期: 2023-09-27 18:10:39

在Compact Framework中,我想在打开下拉列表时更改ComboBox的ItemIndex。我试图从LostFocus或KeyPress事件改变它,它似乎起作用,但当下拉列表关闭时,值返回到原始值。

例如:

    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和文本,但是当我移动到下一个控件或简单地关闭下拉列表时,组合框改变了前一个的值。

谢谢

当下拉列表打开时,在Comobox中更改ItemIndex

我只是试图用以下代码复制您的问题(但在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 ();
        }
    }
}

所以我假设你有一些额外的代码或事件附加到comboBox,或者它在FF中的行为真的不同。

您可以测试您的应用程序也在FF内运行,只需进入PC的文件资源管理器中的bin'Debug目录,然后双击exe文件启动PC上的SmartDevice应用程序。通常情况下(没有特殊的dll引用)它应该运行在PC上,因为CF是下行兼容FF。

如果你仍然有问题,请张贴一个最小化的代码示例来说明你的问题。