【问题标题】:C# Prevent Blinking Cursor in Textbox, Solution not WorkingC#防止文本框中的光标闪烁,解决方案不起作用
【发布时间】:2019-09-17 09:59:39
【问题描述】:

我发现many 页面描述了如何防止闪烁的插入符号。看起来很简单。

[DllImport("user32")]
public static extern bool HideCaret(IntPtr hWnd);

private void OnFocusEnterSpecificTextbox(object sender, EventArgs e)
{ HideCaret(SpecificTextbox.Handle); }

它不工作。当我单击文本框时,出现插入符号。我可以断点并看到我正在运行该代码。

我犯了什么愚蠢的错误?

【问题讨论】:

  • 你想通过隐藏插入符号来达到什么目的?你检查了HideCaret(和GetLastError)的返回值吗?
  • 感谢您的快速回复。当我单击“运行”按钮时,焦点会跳转到表单上的一个只读文本框,甚至会突出显示文本。我觉得这很不和谐,我希望我的用户也会如此。我从 HideCaret 收到了错误的回报。试图弄清楚如何 GetLastError,但我的第一次刺伤导致了异常。晚了。明天我会回到这个。
  • 试试这样的。 textBox1.GotFocus += (s1, e1) => { HideCaret(textBox1.Handle); };
  • 您实际使用的是哪个事件?方法名称有点模棱两可。您需要使用GotFocus,而不是Enter

标签: c# winforms caret


【解决方案1】:

这可行(Windows 7 上的 VS 2008):

public partial class Form1 : Form
{
    [DllImport("user32")]
    public static extern bool HideCaret(IntPtr hWnd);


    public Form1()
    {
        InitializeComponent();
    }


    private void Form1_Load(object sender, EventArgs e)
    {
        textBox1.GotFocus += new EventHandler(textBox1_GotFocus);
    }


    void textBox1_GotFocus(object sender, EventArgs e)
    {
        HideCaret(textBox1.Handle);
    }
}

【讨论】:

  • 谢谢。我被 Visual Studio 表面化的 Enter 吸引住了,但没有被 GotFocus 吸引。我需要研究一下细微的差异。
【解决方案2】:

这是在TextBox 中停止闪烁光标的另一种方法:

public partial class Form1 : Form
{
    [DllImport("user32.dll")]
    static extern bool HideCaret(IntPtr hWnd);
    public Form1()
    {
        InitializeComponent();
        textBox1.GotFocus += (s1, e1) => { HideCaret(textBox1.Handle); };
    }
}

【讨论】:

    猜你喜欢
    • 2010-10-10
    • 2011-12-22
    • 2014-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多