如何获取C#自定义文本框以在鼠标单击时更改光标位置
我有一个定制的文本框,它只是一个标准的文本框,还有一些附加的小功能,并且一切正常。我遇到的问题是,在字段中单击以更改光标位置实际上并没有更改光标的位置,光标只是停留在字段的开头 下面是我正在使用的代码,我希望有人能告诉我我缺少了什么:如何获取C#自定义文本框以在鼠标单击时更改光标位置,c#,textbox,controls,C#,Textbox,Controls,我有一个定制的文本框,它只是一个标准的文本框,还有一些附加的小功能,并且一切正常。我遇到的问题是,在字段中单击以更改光标位置实际上并没有更改光标的位置,光标只是停留在字段的开头 下面是我正在使用的代码,我希望有人能告诉我我缺少了什么: using System; using System.ComponentModel; using System.Drawing; using System.Runtime.InteropServices; using System.Windows.Forms;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace Test.UI.Controls
{
public partial class TestTextBox : TextBox
{
private Color normalForegroundColor = Color.Gray;
private Color textChangedForegroundColor = Color.Red;
private string startingText = string.Empty;
[Description("TextBox border color when text is changed"), Category("Appearance")]
public Color TextChangedColor
{
get { return textChangedForegroundColor; }
set { textChangedForegroundColor = value; }
}
[Description("Set starting text of textbox, as well as the Text property"), Category("Appearance")]
public String StartingText
{
get { return startingText; }
set
{
startingText = value;
this.Text = startingText;
}
}
public TestTextBox()
{
InitializeComponent();
normalForegroundColor = this.ForeColor;
}
protected override void OnTextChanged(EventArgs e)
{
base.OnTextChanged(e);
this.ForeColor = this.Text == startingText ? normalForegroundColor : textChangedForegroundColor;
}
}
}
下面是自定义文本框的屏幕抓图,其中包含数据:
光标代码到底在哪里?代码对我很有用。我只是去掉了TestReflectionTextBox方法。看起来您想要作为构造函数,但它与类名不匹配。@Charlie我没有任何光标代码,我正在继承TextBox。我认为这样做会对我没有覆盖的任何内容使用标准文本框代码?@Diego很抱歉,构造函数应该是public TestTextBox(),我已经对代码进行了编辑。如果我键入文本到字段中,然后单击中间的一个字,则游标不会转到那个位置。当你这么做的时候,它对你有用吗?也许这只是我这边的一个工作站,但是标准的文本框工作得很好?@kenneth bray是的,你描述的光标行为对我很有用。。。用户界面上有什么东西吗?您只需删除类定义上的partial语句,然后像从textbox中继承一样进行继承。我认为这样也可以删除initializeComponent调用。