C#的TextBox不支持插入和改写切换

MFC里为 SetOverwriteMode  也许用API可以 但是没试

有人用以下代码实现

使用以下代码,此代码由 CMIC(大象)提供。
详情请见帖:
http://expert.csdn.net/Expert/topic/2677/2677274.xml?temp=.1031153


// 代码
using System;
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;
}
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值