这个特性确实有点麻烦→_→主要原因是光标位置发生改变了。
引用
评论
引用 acmilan:恩 mfc也这样吗?我现在用这个办法,能解决问题 但是还是会闪一下,有个文字被全选然后 取消全选的过程
这个特性确实有点麻烦→_→主要原因是光标位置发生改变了。
引用 张静茹:可能.NET的Text属性的实现有问题,建议尝试一下不改变Text属性,而是直接调用SetWindowText或者发送WM_SETTEXT设置文本。
恩 mfc也这样吗?我现在用这个办法,能解决问题 但是还是会闪一下,有个文字被全选然后 取消全选的过程
引用 acmilan:
可能.NET的Text属性的实现有问题,建议尝试一下不改变Text属性,而是直接调用SetWindowText或者发送WM_SETTEXT设置文本。
// System.Windows.Forms.TextBox
public override string Text
{
get
{
return base.Text;
}
set
{
base.Text = value;
this.selectionSet = false;
}
}
// System.Windows.Forms.Control
[Bindable(true), Localizable(true), DispId(-517), SRCategory("CatAppearance"), SRDescription("ControlTextDescr")]
public virtual string Text
{
get
{
if (!this.CacheTextInternal)
{
return this.WindowText;
}
if (this.text != null)
{
return this.text;
}
return "";
}
set
{
if (value == null)
{
value = "";
}
if (value == this.Text)
{
return;
}
if (this.CacheTextInternal)
{
this.text = value;
}
this.WindowText = value;
this.OnTextChanged(EventArgs.Empty);
if (this.IsMnemonicsListenerAxSourced)
{
for (Control control = this; control != null; control = control.ParentInternal)
{
Control.ActiveXImpl activeXImpl = (Control.ActiveXImpl)control.Properties.GetObject(Control.PropActiveXImpl);
if (activeXImpl != null)
{
activeXImpl.UpdateAccelTable();
return;
}
}
}
}
}引用 张静茹:this.selectionSet = false;
TextBox继承自ConTrol
// System.Windows.Forms.TextBox
public override string Text
{
get
{
return base....
引用 acmilan:我自己声明一个类然后继承TextBox,然后自己重写Text属性set方法里写base.base.text = value;?
mfc的CEditView实现了查找和替换功能,mfc的完整源代码可在vc/atlmfc/src找到。
引用 张静茹:[s::L] 没有base.base这种用法
我自己声明一个类然后继承TextBox,然后自己重写Text属性set方法里写base.base.text = value;?
引用 acmilan:
mfc的CEditView实现了查找和替换功能,mfc的完整源代码可在vc/atlmfc/src找到。
[DllImport("user32.dll", EntryPoint = "SetWindowText", CharSet = CharSet.Ansi)]
public static extern int SetWindowText(int hwnd, string lpString);
private void button1_Click(object sender, EventArgs e)
{
int TextBoxSouSelectionStart = textBox1.SelectionStart;//TextBoxSou光标位置
int TextBoxSouSelectionLength = textBox1.SelectionLength;//TextBoxSou选中长度
int TextBoxSouScrollPos = GetScrollPos((int)textBox1.Handle, 1);//TextBox滚动条位置
SetWindowText((int)textBox1.Handle, textBox1.Text + "\r\n" + (Convert.ToUInt32(textBox1.Lines[textBox1.Lines.Length - 1]) + 1));
textBox1.Focus();
textBox1.SelectionStart = TextBoxSouSelectionStart;
textBox1.SelectionLength = TextBoxSouSelectionLength;
SetScrollPos(textBox1.Handle, 1, TextBoxSouScrollPos, true);
SendMessage(textBox1.Handle, 0x00B6, 0, TextBoxSouScrollPos);
}引用 张静茹:c++中调用基类的基类的函数是常见语法(Control::setText)
没有base.base这种用法
引用 张静茹:这里应该改成CharSet = CharSet.Auto或CharSet.Unicode。。。不然很多字符都可能变成一堆问号
只用SetWindowText改变Text还是会自动把光标设置为0 滚动条在最上面,使用你的SetWindowText和我原来办法就完美了~~~
public static extern int Set...
引用 acmilan:恩 是的
这里应该改成CharSet = CharSet.Auto或CharSet.Unicode。。。不然很多字符都可能变成一堆问号
使用CharSet.Ansi的话,.NET要先把string从unicode转换为ansi字符串,然后调用Set...
引用 acmilan:这得是Control的setText方法是public吧
c++中调用基类的基类的函数是常见语法(Control::setText)
引用 acmilan:C#的多态特性,TextBox继承TextBoxBase,它再继承Control,TextBox里的Text属性是override修饰的,继承链中的text都被他覆盖了,好像不能访问Control的Text了
应该protected就行
引用 张静茹:c++中如果setText是虚函数(可以被子类重写的函数)的话,setText()是call this->vtable[n_setText],而Control::setText()则是call Control::setText。c#的base.Text应该也类似,只是可能不能随意跨级调用而已。
C#的多态特性,TextBox继承TextBoxBase,它再继承Control,TextBox里的Text属性是override修饰的,继承链中的text都被他覆盖了,好像不能访问Control的Text了
引用 acmilan:
c++中如果setText是虚函数(可以被子类重写的函数)的话,setText()是call this->vtable,而Control::setText()则是call Control::setText。c#的base.Text应该也类似...
typeof(Control).GetProperty("WindowText", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance).
SetValue(textBox1, "在TextBox里显示的内容");
引用 张静茹:this.selectionSet = false;应该就是全选的作用。mfc中替换文本的做法好像就是你这么做的。
typeof(Control).GetProperty("WindowText", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Publ...
引用 acmilan:应该是的,this.selectionSet = false;是在TextBox里的,跳过了TextBox确实不会全选了
this.selectionSet = false;应该就是全选的作用。mfc中替换文本的做法好像就是你这么做的。
引用 张静茹:→_→我不知道.net加上这一句有什么用
应该是的,this.selectionSet = false;是在TextBox里的,跳过了TextBox确实不会全选了
引用 acmilan:
→_→我不知道.net加上这一句有什么用
var SelectInternal = typeof(RichTextBox).GetMethod("SelectInternal", BindingFlags.Instance | BindingFlags.NonPublic);
SelectInternal.Invoke(richTextBox1, new object[]{1000,1000,1000});//要插入的位置,三个参数 好像必须一样原型:internal virtual void SelectInternal(int start, int length, int textLen)
richTextBox1.SelectedText = "要插入的文本";