就像标题一样:我在网上搜索了答案,但我无法找到隐藏VB.NET中RichTextBox插入符号的方法。
我尝试将RichTextBox.Enabled属性设置为False,然后将背景颜色和前景色更改为非灰色属性,但这并不能解决问题。
提前致谢。
答案 0 :(得分:13)
解决方案:
来自:http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_21896403.html
using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Runtime.InteropServices;
public class ReadOnlyRichTextBox : System.Windows.Forms.RichTextBox
{
[DllImport("user32.dll")]
private static extern int HideCaret (IntPtr hwnd);
public ReadOnlyRichTextBox()
{
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.ReadOnlyRichTextBox_Mouse);
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.ReadOnlyRichTextBox_Mouse);
base.ReadOnly = true;
base.TabStop = false;
HideCaret(this.Handle);
}
protected override void OnGotFocus(EventArgs e)
{
HideCaret(this.Handle);
}
protected override void OnEnter(EventArgs e)
{
HideCaret(this.Handle);
}
[DefaultValue(true)]
public new bool ReadOnly
{
get { return true; }
set { }
}
[DefaultValue(false)]
public new bool TabStop
{
get { return false; }
set { }
}
private void ReadOnlyRichTextBox_Mouse(object sender, System.Windows.Forms.MouseEventArgs e)
{
HideCaret(this.Handle);
}
private void InitializeComponent()
{
//
// ReadOnlyRichTextBox
//
this.Resize += new System.EventHandler(this.ReadOnlyRichTextBox_Resize);
}
private void ReadOnlyRichTextBox_Resize(object sender, System.EventArgs e)
{
HideCaret(this.Handle);
}
}
答案 1 :(得分:2)
您可以使用HideCaret API函数,在www.pinvoke.net上查看。诀窍是知道何时调用它。一个非常简单和肮脏的解决方案是在RTF的Enter事件中启动一次性计时器。在WndProc中捕获正确的消息作为nobugs建议更好,遗憾的是被困的消息是错误的......
答案 2 :(得分:1)
这对我有用:
public class RichTextLabel : RichTextBox
{
public RichTextLabel()
{
base.ReadOnly = true;
base.BorderStyle = BorderStyle.None;
base.TabStop = false;
base.SetStyle(ControlStyles.Selectable, false);
base.SetStyle(ControlStyles.UserMouse, true);
base.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
base.MouseEnter += delegate(object sender, EventArgs e)
{
this.Cursor = Cursors.Default;
};
}
protected override void WndProc(ref Message m) {
if (m.Msg == 0x204) return; // WM_RBUTTONDOWN
if (m.Msg == 0x205) return; // WM_RBUTTONUP
base.WndProc(ref m);
}
}
我希望它有所帮助
答案 3 :(得分:1)
这里我有一个名为txtMessage的Rich Text控件,它的事件被处理以隐藏显示它的事件的插入符号。
<System.Runtime.InteropServices.DllImport("user32.dll")>
Private Shared Function HideCaret(ByVal hWnd As IntPtr) As Boolean
End Function
Public Sub New()
txtMessage.ReadOnly = True
txtMessage.TabStop = False
End Sub
Private Sub txtMessage_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtMessage.KeyUp
HideCaret(txtMessage.Handle)
End Sub
Private Sub txtMessage_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles txtMessage.MouseDown
HideCaret(txtMessage.Handle)
End Sub
Private Sub txtMessage_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs) Handles txtMessage.SelectionChanged
HideCaret(txtMessage.Handle)
End Sub
答案 4 :(得分:1)
将richTextBox控件放在表单
上将表单名称设置为Form1
将richTextBox名称设置为richTextBox1
如果您不想允许用户将文本集richTextBox1 ShortcutsEnabled属性复制为False
转到项目 - >添加组件,输入组件名称ReadOnlyRichTextBox.cs
然后打开ReadOnlyRichTextBox.cs并粘贴以下代码:
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace <Replace with your app namespace>
{
public partial class ReadOnlyRichTextBox : RichTextBox
{
[DllImport("user32.dll")]
static extern bool HideCaret(IntPtr hWnd);
public ReadOnlyRichTextBox()
{
this.ReadOnly = true;
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
HideCaret(this.Handle);
}
}
}
从解决方案资源管理器中打开你的&#34; Form1.Designer.cs&#34;并在此文件中替换以下行:
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
与
this.richTextBox1 = new ReadOnlyRichTextBox();
和
私有System.Windows.Forms.RichTextBox richTextBox1;
与
private ReadOnlyRichTextBox richTextBox1;
答案 5 :(得分:1)
这适用于C#...
this.textRichBox.TabStop = false;
更新:它可防止在打开表单后出现插入符号。单击RichTextBox后,将出现插入符号。
答案 6 :(得分:0)
做一些事情以防止它获得“输入焦点”:它将具有插入符号,并且只有在具有焦点时才可编辑。
答案 7 :(得分:0)
/// <summary>
/// Transparent RichTextBox
/// To change BackColor add a Panel control as holder of RichTextLabel
/// </summary>
public class RichTextLabel : RichTextBox
{
public RichTextLabel()
{
base.Enabled = false;
base.ReadOnly = true;
base.ScrollBars = RichTextBoxScrollBars.None;
base.ForeColor = Color.FromArgb(0, 0, 1);
}
override protected CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x20;
return cp;
}
}
override protected void OnPaintBackground(PaintEventArgs e)
{
}
}
答案 8 :(得分:0)
最小版本(优化):
public class ReadOnlyRichTextBox : RichTextBox
{
[DllImport("user32.dll")]
static extern bool HideCaret(IntPtr hWnd);
public ReadOnlyRichTextBox()
{
ReadOnly = true;
SetStyle(ControlStyles.Selectable, false);
}
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
HideCaret(this.Handle);
}
protected override void OnEnter(EventArgs e)
{
base.OnEnter(e);
HideCaret(this.Handle);
}
protected override void OnGotFocus(EventArgs e)
{
base.OnGotFocus(e);
HideCaret(this.Handle);
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
if (e.Button == MouseButtons.Left)
HideCaret(this.Handle);
}
}
答案 9 :(得分:0)
我知道它很旧,但是我在这里看到很多不同的选择。 我必须为我补充一点,这可以解决问题:
this.textRichBox.ReadOnly = false;
this.textRichBox.TabStop = false;