How to show cursor in unfocused WinForms TextBox/RichTextBox?

前端 未结 3 2031
渐次进展
渐次进展 2021-01-14 03:34

I need to show cursor in RichTextBox control in WinForms application even when it\'s not in focus. How can I do this? I found only the way for WPF ( How to keep WPF TextBox

相关标签:
3条回答
  • 2021-01-14 04:17

    You can't set focus to the two or more UI at same time however you can preserve the selection by setting HideSelection=false.

    0 讨论(0)
  • 2021-01-14 04:19

    I don't know what you are trying to achieve and how much is it really useful. But if it is just for visual purpose, write some thing like '|' in it. Its a bad, weird, awkward way or what ever you call it, for visual purpose it may work.

        public void blink()
        {
            while (true)
            {
                textBox1.Text = "|";
                Thread.Sleep(200);
                textBox1.Text = "";
                Thread.Sleep(200);
            }
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
            Thread t1 = new Thread(new ThreadStart(blink));
            t1.Start();
        }
    

    I am not sure if I am giving is what you are asking, but to get accurate answer, you have to expose your need of this requirement.

    Hope it helps.

    0 讨论(0)
  • 2021-01-14 04:37

    You can use WinAPI ..

     [DllImport("user32.dll", EntryPoint = "ShowCaret")]
     public static extern long ShowCaret(IntPtr hwnd);
     [DllImport("user32.dll", EntryPoint = "HideCaret")]
     public static extern long HideCaret(IntPtr hwnd);
    

    and call ShowCaret whenever you want

    0 讨论(0)
提交回复
热议问题