我自己写的代码:
protected override void OnKeyPress(KeyPressEventArgs e)
{
if( !e.KeyChar.Equals((char)13) )
return ;
if( !this.DroppedDown )
{
this.DroppedDown = true ;
//this.SelectedIndex = -1;
//this.SelectionStart = 0;
}}但现在按回车时只能<<闪现>>出下拉列表,根本不能选择!!
请高手相救!!!
protected override void OnKeyPress(KeyPressEventArgs e)
{
if( !e.KeyChar.Equals((char)13) )
return ;
if( !this.DroppedDown )
{
this.DroppedDown = true ;
//this.SelectedIndex = -1;
//this.SelectionStart = 0;
}}但现在按回车时只能<<闪现>>出下拉列表,根本不能选择!!
请高手相救!!!
你的代码
------------------------------
if( !e.KeyChar.Equals((char)13) )
return ;
if( !this.DroppedDown )
{
this.DroppedDown = true ;
//this.SelectedIndex = -1;
//this.SelectionStart = 0;
}
--------------------------------
根本没错.完全能实现你所说的回车下拉以供用户选择的功能.
不明白的是,为什么把代码写在 protected override ComboBox 的 OnKeyPress(KeyPressEventArgs e)方法里.欢迎来信, camel3000#msn.com
并且感谢使用 .net 产品.
我改写后代码:
protected override void OnKeyPress(KeyPressEventArgs e)
{
if(e.KeyChar.Equals((char)13))
{
e.Handled = true;
SendKeys.Send("{F4}");
return ;
}
else
{
base.OnKeyPress( e );
}
}但现在当出现下拉列表后却不能按回车的选择了,即总是回车被转成F4,总是出现下拉列表,请问高手如何解决???
对于这种情况,我就随便写了一个,你运行看看.原理一样.
你textBox1中按回车__感谢使用.net 产品
----------using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace WindowsApplication25
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.ComboBox comboBox1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(80, 168);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);
//
// comboBox1
//
this.comboBox1.Items.AddRange(new object[] {
"one",
"two",
"three",
"four"});
this.comboBox1.Location = new System.Drawing.Point(64, 40);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(144, 21);
this.comboBox1.TabIndex = 1;
this.comboBox1.Text = "comboBox1";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.comboBox1,
this.textBox1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if(e.KeyChar==(char)13)
{
this.comboBox1.DroppedDown=true;
}
}
}
}
重载Form的ProcessDialogKey.(将下面的代码拷贝到Form class中,comboBox1要相应修改).
protected override bool ProcessDialogKey(System.Windows.Forms.Keys aKey)
{
if (aKey == Keys.Enter & this.ActiveControl == this.comboBox1)
{
this.comboBox1.DroppedDown = true;
base.ProcessDialogKey(aKey);
return true;
} base.ProcessDialogKey(aKey);
return false;
}
经常说"感谢使用微软的产品"的那些大拿都好长时间不来了...
你们都是通过改变ComboBox的属性从而来调相应的函数是吧!
我原先也是这么做的,但在我的机子上总是一闪而过.不过现在解决了,是通过把Enter换成F4键处理.很感谢大家的回答,我新近又有一个问题:
http://expert.csdn.net/Expert/TopicView1.asp?id=1237247
请大家帮忙出点主意!!