views:

3015

answers:

3

In a textbox, how can u prevent the display of the blinking cursor when you click on it?

I did read in some forums that there is call to a particular api but when i tried it in my code, an error was shown. Please provide the complete code for this purpose if possible and let me know if there is a particular event where the code should be executed.

This textbox is part of a form window that am creating for the simulation of a lan messenger. I am using C#. The form has two textboxes in order to resemble that of google talk. It would be desirable to prevent displaying the blinking cursor on the upper textbox.

I tried:

[DllImport("user32")] 
private static extern bool HideCaret(IntPtr hWnd); 
public void HideCaret() { HideCaret(TextBox1.Handle); }

I get the error: "DllImport could not be found."

A: 

Set the ReadOnly property on the TextBox to true.

More answers to this question: http://stackoverflow.com/questions/589273/read-only-textbox

spoon16
+1  A: 

If you want to disallow editing on the textbox, set it's ReadOnly property to true.

If you want to allow editing but still hide the caret, call the Win32 API exactly as specified:

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

...

HideCaret(myTextBox.Handle);
Judah Himango
This is the code that I had come across before too.The problem here is that an error is being displayed saying that:"Error 1 The type or namespace name 'DllImport' could not be found (are you missing a using directive or an assembly reference?)"Is there any specific reference to made??
Avik
this works perfectly, thanks!
Mike
+2  A: 

Add using statement at the top.

using System.Runtime.InteropServices;

shahkalpesh
Finally worked.Thanks a ton..
Avik