C# 如何从组合框中永久删除文本选择突出显示?

C# 如何从组合框中永久删除文本选择突出显示?,c#,winforms,combobox,C#,Winforms,Combobox,我对使用DropDownStyle=DropDown删除组合框的文本选择感兴趣 当我添加/删除或关闭DropPown时,该项目被选中。 我无法清除所选文本。你知道怎么做吗 此代码不起作用: comboBox.SelectionLenght = 0; comboBox.SelectionStart = comboBox.Text.Legnth; comboBox.Select(0,0); 我可以看到文本在这行后面突出显示: selectedComboBox.Items.Add(redCompet

我对使用
DropDownStyle=DropDown
删除组合框的文本选择感兴趣
当我添加/删除或关闭DropPown时,该项目被选中。
我无法清除所选文本。你知道怎么做吗

此代码不起作用:

comboBox.SelectionLenght = 0;
comboBox.SelectionStart = comboBox.Text.Legnth;
comboBox.Select(0,0);
我可以看到文本在这行后面突出显示:

selectedComboBox.Items.Add(redCompetitorName);

选择新项后,使用组合框的
SelectedIndexChanged
事件将焦点设置为窗体中的另一个控件

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    button1.Focus();
}

选择新项后,使用组合框的
SelectedIndexChanged
事件将焦点设置为窗体中的另一个控件

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    button1.Focus();
}

您可以延迟执行
Select()
方法,调用
SelectedIndexChanged
事件处理程序(或者,如果您希望仅在用户手动选择项目时发生这种情况)

通过延迟执行(此操作在消息循环中排队),只有在设置并突出显示了
组合框.Text
之后才能执行
Select()
操作。因此,您的命令不会被默认行为覆盖

private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
    // Set the caret to the start of the ComboBox text
    BeginInvoke(new Action(()=> comboBox.Select(0, 0)));

    // OR - set the caret to the end of the text instead
    BeginInvoke(new Action(()=> comboBox.Select(int.MaxValue, 0)));
}
当然,这一概念也适用于其他情况。
当您需要在UI中执行操作时,您可以在其他情况下使用此方法,该操作仅在当前(或已计划操作的当前序列)完成后执行

如果您想要一个更复杂的解决方案来防止ComboBox中的所有类型的选择高亮显示,您可以使用从ComboBox派生的自定义控件,使用来截获其消息。重写WndProc以处理和调用以删除选择(仅当起始位置>0时,否则您可能会陷入一个奇怪的自动循环中,其效果通常称为StackOverflow:)

使用System.ComponentModel;
使用系统图;
使用System.Runtime.InteropServices;
使用System.Windows.Forms;
[设计分类(“代码”)]
公共类ComboBox NoFocus:ComboBox
{
IntPtr editHandle=IntPtr.Zero;
私有EditNativeWindow editControl=null;
公共ComboxNoFocus(){}
已创建受保护的重写无效OnHandleCreated(EventArgs e)
{
碱基。根据HandleCreated(e);
editHandle=GetComboBoxEditInternal(this.Handle);
editControl=新的EditNativeWindow(editHandle);
}
公共类EditNativeWindow:NativeWindow
{
私有常量int EM_setel=0x0B1;
public EditNativeWindow():this(IntPtr.Zero){}
公共编辑窗口(IntPtr句柄)
{
if(handle!=IntPtr.Zero){
这个.把手(把手);
}
}
受保护的覆盖无效WndProc(参考消息m)
{
开关(m.Msg){
案例EM_设置:
int pos=m.LParam.ToInt32();
如果(位置>0){
PostMessage(this.Handle,EM_setel,0,0);
}
返回;
违约:
//其他业务
打破
}
基准WndProc(参考m);
}
}
[DllImport(“user32.dll”,CharSet=CharSet.Unicode)]
内部静态外部bool GetComboBoxInfo(IntPtr hWnd,ref ComboxInfo pcbi);
[DllImport(“user32.dll”,SetLastError=true,CharSet=CharSet.Unicode)]
内部静态外部bool PostMessage(IntPtr hWnd、uint Msg、intwparam、intlparam);
[StructLayout(LayoutKind.Sequential)]
内部结构ComboxInfo
{
公共机构的规模;
公共项目;
公共矩形按钮;
公共int按钮状态;
公共IntPtr hwndCombo;
公共IntPtr hwndEdit;
公共IntPtr hwndList;
public void Init()=>this.cbSize=Marshal.SizeOf();
}
内部静态IntPtr GetComboBoxEditInternal(IntPtr cboHandle)
{
var cbInfo=新COMBOBOXINFO();
cbInfo.Init();
GetComboBoxInfo(cboHandle,参考cbInfo);
返回cbInfo.hwndeit;
}
}

您可以延迟执行
Select()
方法,调用
SelectedIndexChanged
事件处理程序(或者,如果您希望仅在用户手动选择项目时发生这种情况)

通过延迟执行(此操作在消息循环中排队),只有在设置并突出显示了
组合框.Text
之后才能执行
Select()
操作。因此,您的命令不会被默认行为覆盖

private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
    // Set the caret to the start of the ComboBox text
    BeginInvoke(new Action(()=> comboBox.Select(0, 0)));

    // OR - set the caret to the end of the text instead
    BeginInvoke(new Action(()=> comboBox.Select(int.MaxValue, 0)));
}
当然,这一概念也适用于其他情况。
当您需要在UI中执行操作时,您可以在其他情况下使用此方法,该操作仅在当前(或已计划操作的当前序列)完成后执行

如果您想要一个更复杂的解决方案来防止ComboBox中的所有类型的选择高亮显示,您可以使用从ComboBox派生的自定义控件,使用来截获其消息。重写WndProc以处理和调用以删除选择(仅当起始位置>0时,否则您可能会陷入一个奇怪的自动循环中,其效果通常称为StackOverflow:)

使用System.ComponentModel;
使用系统图;
使用System.Runtime.InteropServices;
使用System.Windows.Forms;
[设计分类(“代码”)]
公共类ComboBox NoFocus:ComboBox
{
IntPtr editHandle=IntPtr.Zero;
私有EditNativeWindow editControl=null;
公共ComboxNoFocus(){}
已创建受保护的重写无效OnHandleCreated(EventArgs e)
{
碱基。根据HandleCreated(e);
editHandle=GetComboBoxEditInternal(this.Handle);
editControl=新的EditNativeWindow(editHandle);
}
公共类EditNativeWindow:NativeWindow
{
私有常量int EM_setel=0x0B1;
public EditNativeWindow():this(IntPtr.Zero){}