在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和文本,但当我移动到下一个控件或简单地关闭下拉列表时,组合框将更改前一个值。
谢谢
发布于 2013-04-26 01:05:21
我只是试着用下面的代码复制你的问题(但在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目录,然后双击可执行文件在PC上启动您的SmartDevice应用程序。正常情况下(没有引用特殊的DLL),它也应该在PC上运行,因为CF向下兼容FF。
如果您仍然有问题,请发布一个最小化的代码示例来演示您的问题。
https://stackoverflow.com/questions/7658167
复制相似问题