设置 ComboBox.Cursor 不适用于默认/箭头光标

问题描述 投票:0回答:1

在 C# 的 Windows 窗体中,在设计器文件中,这些都可以正常工作:

this.someComboBox.Cursor = System.Windows.Forms.Cursors.AppStarting;
this.someComboBox.Cursor = System.Windows.Forms.Cursors.Cross;

但这些不是:

this.someComboBox.Cursor = System.Windows.Forms.Cursors.Arrow;
this.someComboBox.Cursor = System.Windows.Forms.Cursors.Default;

也就是说,当尝试使用

Cursors.Arrow
Cursors.Default
时,如果您将鼠标光标移动到组合框的文本部分上,而不是向右移动箭头,光标将被设置为
Cursors.IBeam 
,不是箭头。

为什么?如何强制它(正确地)使用箭头?

如果它只是对

Cursors.Default
执行此操作,那么也许这仅意味着默认光标让控件可以针对不同情况从一组不同的默认值中提取。如果它对所有游标都这样做,那么该属性的名称似乎具有误导性,并且实际上应该将其设置在其他地方。但对于前两个它工作得很好,而对于另外两个则失败了。

如果有帮助,这是设计器文件和主文件中能够重现此问题的代码:

// designer file:

this.someComboBox.Cursor = System.Windows.Forms.Cursors.Arrow;
this.someComboBox.FormattingEnabled = true;
this.someComboBox.Location = new System.Drawing.Point(130, 73);
this.someComboBox.Name = "someComboBox";
this.someComboBox.Size = new System.Drawing.Size(128, 21);
this.someComboBox.TabIndex = 2;
this.someComboBox.Click += new System.EventHandler(this.ComboBox_Click);
this.someComboBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(
        this.ComboBox_KeyPress);

//..........................................................................

// main file:

private void ComboBox_Click(object sender, System.EventArgs e)
{
    ((ComboBox)(sender)).DroppedDown = true;
}

private void ComboBox_KeyPress(object sender, KeyPressEventArgs e)
{
    e.Handled = true;
}

以及组合框的图形:

c# winforms combobox
1个回答
0
投票

我找到了解决方案。

创建一个继承自 ComboBox 的新类并添加以下代码:

// To change de Cursor to Arrow, it is required to change DefaultCursor to another cursor thant the default Arrow.
Protected Overrides ReadOnly Property DefaultCursor As Cursor = Cursors.Hand
Public Overrides Property Cursor As Cursor = Cursors.Arrow
© www.soinside.com 2019 - 2024. All rights reserved.