对winform的TextBox,如何以覆盖(overwrite)而不是插入的方式更新其内容? RT 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在keypress事件中txtbox.SelectionLength= 1; 晕~~~~~~~~~这个也可以写前清空文本框或者直接 Text=保证世界清净了 using System; http://www.faq-it.org/apache/">faq-it.org/apache/ using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; namespace Cmic.Controls { /// <summary> /// TextBoxEx 的摘要说明。 /// </summary> public class TextBoxInsert : System.Windows.Forms.TextBox { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; enum TypeMode{InsertMode, OvertypeMode}; private TypeMode typeMode; public TextBoxInsert() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); this.typeMode = TypeMode.InsertMode; // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } /// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { // // TextBoxInsert // this.Text = "TextBoxEx"; } #endregion protected override void OnTextChanged(EventArgs e) { base.OnTextChanged (e); // if(this.typeMode == TypeMode.InsertMode && this.SelectionLength == 0) // { // this.SelectionLength = 1; // } if(this.typeMode== TypeMode.OvertypeMode) { this.SelectionLength = 1; } } protected override void OnKeyPress(KeyPressEventArgs e) { base.OnKeyPress (e); if((e.KeyChar == (char)8) && (typeMode == TypeMode.OvertypeMode)) { this.SelectionLength = 0; } } protected override void OnKeyDown(KeyEventArgs e) { base.OnKeyDown(e); switch(e.KeyCode) { case Keys.Insert: if(this.typeMode == TypeMode.OvertypeMode) { this.typeMode = TypeMode.InsertMode; } else { this.SelectionLength = 1; this.typeMode = TypeMode.OvertypeMode; } break; case Keys.Left: if(this.typeMode == TypeMode.OvertypeMode) { e.Handled=true; if(this.SelectionStart!=0) { this.SelectionStart=this.SelectionStart-1; this.SelectionLength=1; } } break; case Keys.Right: if(this.typeMode == TypeMode.OvertypeMode) { e.Handled=true; if(this.SelectionStart!=this.Text.Length) { this.SelectionStart=this.SelectionStart+1; this.SelectionLength=1; } } break; default: break; } } } }http://www.faq-it.org/archives/csharp/f33832690ae4b722c81c6f2569fc0b03.php 关于学Ado.net的疑惑[非技术] C# 如何在钩子中截取模拟的键盘输入 List<T>数据显示 怎样避免两个TextBox的TextChanged事件相互触发? 容器控件组里TabControl是灰色 关于winform窗体关闭的问题 dataGridView 使用报错:AccessViolationException,尝试读取或写入受保护的内存 访问ORACLE数据库 在C#如何使用原来C的函数,scanf ,printf,fwrite等等!急 c#与Matlab混合编程问题 如果用C#对串口进行操作? 检索和显示的问题
txtbox.SelectionLength= 1;
这个也可以
写前清空文本框
或者直接 Text=
保证世界清净了
http://www.faq-it.org/apache/">faq-it.org/apache/ using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace Cmic.Controls
{
/// <summary>
/// TextBoxEx 的摘要说明。
/// </summary>
public class TextBoxInsert : System.Windows.Forms.TextBox
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
enum TypeMode{InsertMode, OvertypeMode};
private TypeMode typeMode;
public TextBoxInsert()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
this.typeMode = TypeMode.InsertMode;
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
//
// TextBoxInsert
//
this.Text = "TextBoxEx";
}
#endregion
protected override void OnTextChanged(EventArgs e)
{
base.OnTextChanged (e);
// if(this.typeMode == TypeMode.InsertMode && this.SelectionLength == 0)
// {
// this.SelectionLength = 1;
// }
if(this.typeMode== TypeMode.OvertypeMode)
{
this.SelectionLength = 1;
}
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress (e);
if((e.KeyChar == (char)8) && (typeMode == TypeMode.OvertypeMode))
{
this.SelectionLength = 0;
}
}
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
switch(e.KeyCode)
{
case Keys.Insert:
if(this.typeMode == TypeMode.OvertypeMode)
{
this.typeMode = TypeMode.InsertMode;
}
else
{
this.SelectionLength = 1;
this.typeMode = TypeMode.OvertypeMode;
}
break;
case Keys.Left:
if(this.typeMode == TypeMode.OvertypeMode)
{
e.Handled=true;
if(this.SelectionStart!=0)
{
this.SelectionStart=this.SelectionStart-1;
this.SelectionLength=1;
}
}
break;
case Keys.Right:
if(this.typeMode == TypeMode.OvertypeMode)
{
e.Handled=true;
if(this.SelectionStart!=this.Text.Length)
{
this.SelectionStart=this.SelectionStart+1;
this.SelectionLength=1;
}
}
break;
default:
break;
}
}
}
}
http://www.faq-it.org/archives/csharp/f33832690ae4b722c81c6f2569fc0b03.php