C#ComboBox禁用突出显示

时间:2014-09-17 21:56:51

标签: c# combobox highlighting

我有自定义的ComboBox,其中DropDownStyle = ComboBoxStyle.DropDown;。设置了DropDown样式,因为我想将ComboBox的Text属性设置为值列表之外的东西。一切都很好,除了ComboBox在它离开时突出显示文本,当我点击组合框编辑时可以使用。我怎么能应付这个? 举例说明:

enter image description here

第一张图片是一切看起来不错的地方,第二张是突出显示情况,第三张剪辑已开启。

8 个答案:

答案 0 :(得分:8)

在DropDown关闭之后尝试取消选择文本

void comboBox1_DropDownClosed(object sender, EventArgs e) {
  this.BeginInvoke(new Action(() => { comboBox1.Select(0, 0); }));
}

答案 1 :(得分:2)

为了解决这个问题,我几乎尝试了一切:

  • DropdownStyle属性设置为DropdownList
  • this.BeginInvoke(new Action(() => { comboBox1.Select(0, 0); }));
  • combobox1.SelectionLength = 0;
  • 更改comboBox.TabIndex
  • 未尝试SendKeys.Send("{ESC}");,因为它不是一个可靠的解决方案

没有任何帮助。 唯一稳定且有效的解决方案是将焦点转移到另一个Label控件上:

    label.Focus();

您也可以隐藏该标签。

答案 2 :(得分:2)

要解决的棘手问题。它似乎来自Resize事件。有很多解决方案可以做类似的事情,但是在我尝试这个之前,我见过的所有解决方案都没有。 (这是一个不需要从ComboBox继承的解决方案;继承可能是一个更直接的解决方案,但要求您始终使用继承的类,而不是实际的ComboBox类。)

comboBox.Resize += (s, e) => {
   if (!comboBox.IsHandleCreated)
      return;  // avoid possible exception

   comboBox.BeginInvoke(new Action(() => comboBox.SelectionLength = 0));
};

将选择长度设置为零以消除突出显示,但何时?其他示例在其他地方执行此操作,但问题似乎是由Resize特别引起的,因此在Resize之后执行此操作始终如一地修复它,至少对我而言。 (当你调整窗口大小时,仍然可以看到它闪烁,但它总是很好。)

BeginInvoke确保在Resize工作后充分发生,并且检查IsHandleCreated会阻止在创建句柄之前调用它,在这种情况下BeginInvoke会引发异常。

这个稍微复杂的版本包括一些检查,以防止重点控制失去高亮,因为它实际上应该有它。如果父母还没有存在,或者如果父母还没有主动控制,它也不会触发,两者都表明事情太早了。

comboBox.Resize += (s, e) => {
   if (!comboBox.IsHandleCreated)
      return;

   comboBox.BeginInvoke(new Action(() => {
      var parent = comboBox.FindForm();
      if (parent == null)
         return;

      if (parent.ActiveControl == null)
         return;

      if (parent.ActiveControl == comboBox)
         return;

      comboBox.SelectionLength = 0;
   }));
};

我尝试制作一个“保留”选择长度的版本,而不是总是将其设置为零,但我无法让它正确同步。许多Resize事件可以在BeginInvoke代理开始触发之前触发,因此保留的值将始终被破坏的值覆盖。我尝试将它们全部保存在QueueStack中,但在这两种情况下,我都无法撤消排序(不确定原因,因为这没有意义)。

答案 3 :(得分:1)

如果您指的是禁用突出显示和编辑,则可能需要考虑将DropdownStyle属性设置为DropdownList

yourComboBox.DropDownStyle = ComboBoxStyle.DropDownList;

答案 4 :(得分:1)

我知道这篇文章很老但最近我遇到了与组合框相同的问题。

情况:我有一个可编辑的组合框,当用户写一些字母时,它会提出完整的单词。

但是当我要输入一个字母时,组合框自动高亮显示文字,下一个字母自动替换上一个字母。

解决方案:我使用文本框来避免任何突出显示:

public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('role_id')->index()->unsigned()->nullable();
            $table->integer('is_active')->default(0);
            $table->string('name');
            $table->string('photo_id')->default('default.png');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

我生成文本框TextChanged事件:

 public function up()
    {
        Schema::create('blogs', function (Blueprint $table) {
            $table->increments('id');
            $table->string('user_id')->unsigned();
            $table->string('modelName')->index();
            $table->string('title');
            $table->integer('price')->unsigned();
            $table->text('description')->nullable(false);
            $table->integer('status');
            $table->string('photo_id');
            $table->string('company_id')->default(!null);
            $table->timestamps();

//            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->foreign('user_id')->refrences('id')->on('users');

        });
    }

我知道这个解决方案并不是真正的美丽,但对我来说,最简单的解决方案是避免突出显示文本,这篇文章中的所有解决方案对我都不起作用。

我希望这个解决方案有所帮助,感谢阅读。

数学式

Ps:道歉,我的英语不太好。我希望你能正确理解我。

答案 5 :(得分:1)

在我将组合框属性TabStop设置为false之前,我没有任何工作(我希望在任何组合框中都没有突出显示加载表单)。这意味着我的一个按钮采用了我不喜欢的标签突出显示,因此我将它们全部设置为false以启动并根据需要以编程方式对其进行调整。

答案 6 :(得分:0)

我知道这是一个旧线程,但是我的解决方案与其他线程类似,但是依赖于Form.ResizeEnd事件。在其事件处理程序中,我遍历ComboBoxes并将ComboBox.SelectionLength设置为0。

private void Form_ResizeEnd(object sender, EventArgs e)
{
    foreach(ComboBox comboBox in parentControl.Controls.OfType<ComboBox>
    {
        comboBox.SelectionLength = 0;
    }
}

答案 7 :(得分:0)

这对我有用:

  1. 将DrawMode设置为OwnerDrawFixed

  2. 将cbxSubsystems.DrawItem事件设置为下面的函数

private void cbxSubsystems_DrawItem(object sender, DrawItemEventArgs e)
{
    Color BgClr;
    Color TxClr;

    if( (e.State & DrawItemState.ComboBoxEdit) == DrawItemState.ComboBoxEdit )
    {
        // Do not highlight main display
        BgClr = cbxSubsystems.BackColor;
        TxClr = cbxSubsystems.ForeColor;
    }
    else
    {
        BgClr = e.BackColor;
        TxClr = e.ForeColor;
    }

    e.Graphics.FillRectangle(new SolidBrush(BgClr), e.Bounds);

    TextRenderer.DrawText(e.Graphics, cbxSubsystems.Items[e.Index].ToString(), e.Font, e.Bounds,
        TxClr, BgClr, TextFormatFlags.Left | TextFormatFlags.VerticalCenter );
}