在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和文本,但是当我移动到下一个控件或只是关闭下拉列表时,组合框会更改前一个值。
谢谢
答案 0 :(得分: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 ();
}
}
}
所以我假设你有一些额外的代码或事件附加到comboBox,或者它在FF中的行为确实不同。
您可以通过在PC的文件浏览器中转到bin \ Debug目录来测试您的应用程序是否也在FF中运行,然后通过双击exe文件在PC上启动SmartDevice应用程序。通常(没有引用特殊的DLL)它也应该在PC上运行,因为CF与FF的下行兼容。
如果您仍然遇到问题,请发布展示您问题的最小化代码示例。