哪位大侠能够帮到小弟?C#中金额文本框中金额加逗号分隔并且处理???顶者有分

priscill 2005-07-08 04:30:31
由于客户要求,金额的输入文本框中需要自动加逗号分隔,现在的思路是在KEYPRESS事件中,首先屏蔽非金额格式的字符,只允许数字与一个.可以输入,且首位不为0,同时允许“Backspace”、“Delete”、“Enter”、“Left”、“Right”五键,每发生一次KEYPRESS事件,即读取输入的值,在整数部分进行分隔后重新放入文本框。这个小弟已经实现。

现在的问题是如果用户按了"Backspace"、"Delete"、"Left"、"Right"等键,该事件如何处理?同时想达到如下的目的:
假如输入后的金额是:"1,234,567,890.01"当前输入的光标在数字8后面,我按了"Left"键后,希望光标直接跑到数字7后面,跳过",",同理,"Right"键也是一样。
还有,如何处理直接通过鼠标右键中的粘贴贴入的金额?

哪位大侠做过C#中的金额处理的,请尽快帮帮我啊,分不够可再加!!
...全文
662 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
joe_yuan 2005-07-25
  • 打赏
  • 举报
回复

private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
int KeyAscii = (int)e.KeyCode;
//MessageBox.Show(KeyAscii.ToString());

if (KeyAscii == 46)
e.Handled = true;

//oPos = this.textBox1.SelectionStart;//有逗号金额中光标的位置
if (e.Shift == true)
{
if (KeyAscii == 39)
{
if (-oPos <= iSelectionLength && iSelectionLength < strBefRep.Length - oPos)
{
if (iSelectionLength < 0)
{
iSelectionLength += 1;
this.textBox1.SelectionStart = iSelectionStart + iSelectionLength;
this.textBox1.SelectionLength = Math.Abs(iSelectionLength);
Console.WriteLine(iSelectionStart);
Console.WriteLine(iSelectionLength);
}
else
{
iSelectionLength += 1;
this.textBox1.SelectionStart = iSelectionStart;
this.textBox1.SelectionLength = Math.Abs(iSelectionLength);
Console.WriteLine(iSelectionStart);
Console.WriteLine(iSelectionLength);
}
}
}
else if (KeyAscii == 37)
{
if (-oPos < iSelectionLength && iSelectionLength <= strBefRep.Length - oPos)
{
if (iSelectionLength <= 0)
{
iSelectionLength -= 1;
this.textBox1.SelectionStart = iSelectionStart + iSelectionLength;
this.textBox1.SelectionLength = Math.Abs(iSelectionLength);
Console.WriteLine(iSelectionStart);
Console.WriteLine(iSelectionLength);
}
else
{
iSelectionLength -= 1;
this.textBox1.SelectionStart = iSelectionStart;
this.textBox1.SelectionLength = Math.Abs(iSelectionLength);
Console.WriteLine(iSelectionStart);
Console.WriteLine(iSelectionLength);
}
}
}
if (iSelectionLength < 0)
strSelectionTxt = this.strBefRep.Substring(oPos + iSelectionLength, Math.Abs(iSelectionLength));
else if (iSelectionLength > 0)
strSelectionTxt = this.strBefRep.Substring(oPos, Math.Abs(iSelectionLength));
}
else
{
if (iSelectionLength != 0)
{
if (!((KeyAscii >= 48 && KeyAscii <= 57) || (KeyAscii >= 96 && KeyAscii <= 105) || KeyAscii == 8 || KeyAscii == 46))
{
iSelectionStart = oPos;
iSelectionLength = 0;
strSelectionTxt = "";
this.textBox1.SelectionStart = iSelectionStart;
this.textBox1.SelectionLength = iSelectionLength;
}
}

if (KeyAscii == 37 || KeyAscii == 39 || KeyAscii == 36 || KeyAscii == 35)
{
if (!(test1(KeyAscii)))
return;
}
}

e.Handled = true;
}

public void returnMessage(ref int iPos1,ref int oPos1,ref string strAftRep1,ref string strBefRep1)
{
iPos1 = iPos;
oPos1 = oPos;
strAftRep1 = strAftRep;
strBefRep1 = strBefRep;
}

private void textBox1_DoubleClick(object sender, System.EventArgs e)
{
this.textBox1.SelectAll();
iPos = strAftRep.Length;
oPos = strBefRep.Length;
}

public void setValue(string strValue)
{
this.strAftRep = strValue;
iPos = strAftRep.Length;
this.strBefRep = objAAC.AddComma(strAftRep,ref iPos,ref oPos);
oPos = strBefRep.Length;
this.textBox1.Text = strBefRep;
this.textBox1.SelectionStart = oPos;
}

public string getValue()
{
return strAftRep.ToString().Trim();
}





}
}
joe_yuan 2005-07-25
  • 打赏
  • 举报
回复
else if (int.Parse(strAmtIntPart) >= 10 && iRecyle > 0)
{
if (strBefRep.Substring(oPos, 1).Equals(","))
{
strAftRep = strAftRep.Remove(iPos, 1);
iPos = iPos;
oPos = oPos + 1;
}
else
{
strAftRep = strAftRep.Remove(iPos, 1);
iPos = iPos;
oPos = oPos;
}
}
else if (iRecyle == 0)
{
strAftRep = strAftRep.Remove(iPos, 1);
iPos = iPos;
oPos = oPos;
}
}
if (oPos == 0 && iPos == 0)
{
strAftRep = strAftRep.Remove(iPos, 1);
iPos = 0;
oPos = 0;
}
}
this.textBox1.Text = objAAC.AddComma(strAftRep,ref oPos,ref iPos);
this.textBox1.SelectionStart = oPos;
iSelectionStart = oPos;
}
catch(Exception ex)
{
System.Console.WriteLine(ex.Message.ToString());
}
return true;
}

private void textBox1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
int KeyAscii;
KeyAscii = (int)e.KeyValue;
//MessageBox.Show(KeyAscii.ToString());

if ((KeyAscii >= 48 && KeyAscii <= 57) || (KeyAscii >= 96 && KeyAscii <= 105) || KeyAscii == 110 || KeyAscii == 190 || KeyAscii == 46 || KeyAscii == 8 || KeyAscii == 13)
{
//格式化
if (!(test1(KeyAscii)))
return;
}
e.Handled = true;
}
joe_yuan 2005-07-25
  • 打赏
  • 举报
回复
priscill 2005-07-25
  • 打赏
  • 举报
回复
if (KeyAscii == 39)
{
if (iPos < strAftRep.Length)
{
if (iSelectionLength > 0)
{
if (!(strBefRep.Substring(oPos, 1).ToString().Trim().Equals(",")))
{
iPos = iPos + 1;
oPos = oPos + 1;
}
else
{
oPos = oPos + 1;
}
iSelectionLength = 0;
strSelectionTxt = "";
}
else if (iSelectionLength < 0)
{
iPos = iPos;
oPos = oPos;
iSelectionLength = 0;
strSelectionTxt = "";
}
else
{
if (!(strBefRep.Substring(oPos, 1).ToString().Trim().Equals(",")))
{
iPos = iPos + 1;
oPos = oPos + 1;
}
else
{
oPos = oPos + 1;
}
}
}
}
if (KeyAscii == 36)
{
iPos = 0;
oPos = 0;
iSelectionLength = 0;
strSelectionTxt = "";
}
if (KeyAscii == 35)
{
iPos = strAftRep.Length;
oPos = objAAC.AddComma(strAftRep,ref iPos,ref oPos).Length;
iSelectionLength = 0;
strSelectionTxt = "";
}
if (KeyAscii == 8)
{
if (oPos <= strBefRep.Length && oPos >= 1 && iPos >= 1)
{
int iRecyle = 0;
string strAmtIntPart = strAftRep.Substring(0,strAftRep.IndexOf("."));
while (ulong.Parse(strAmtIntPart) / 1000.00 > 1)
{
iRecyle += 1;
strAmtIntPart = strAmtIntPart.Substring(0,strAmtIntPart.Length - 3);
}
if (int.Parse(strAmtIntPart) >= 1 && int.Parse(strAmtIntPart) < 10 && iRecyle > 0)
{
if (strBefRep.Substring(oPos - 1, 1).Equals(","))
{
strAftRep = strAftRep.Remove(iPos - 1, 1);
iPos = iPos - 1;
if (oPos == 1)
oPos = oPos - 1;
else
oPos = oPos - 2;
}
else
{
strAftRep = strAftRep.Remove(iPos - 1, 1);
iPos = iPos - 1;
int iLastComma = strBefRep.LastIndexOf(",");
if (oPos <= iLastComma)
{
if (oPos == 1)
oPos = oPos - 1;
else
oPos = oPos - 2;
}
else
{
oPos = oPos - 1;
}
}
}
else if (int.Parse(strAmtIntPart) >= 10 && iRecyle > 0)
{
if (strBefRep.Substring(oPos - 1, 1).Equals(","))
{
strAftRep = strAftRep.Remove(iPos - 1, 1);
iPos = iPos - 1;
oPos = oPos - 2;
}
else
{
strAftRep = strAftRep.Remove(iPos - 1, 1);
iPos = iPos - 1;
oPos = oPos - 1;
}
}
else if (iRecyle == 0)
{
strAftRep = strAftRep.Remove(iPos - 1, 1);
iPos = iPos - 1;
oPos = oPos - 1;
}
}
}
else if (KeyAscii == 46)
{
if (oPos <= strBefRep.Length - 1 && oPos >= 1 && iPos >= 1)
{
int iRecyle = 0;
string strAmtIntPart = strAftRep.Substring(0,strAftRep.IndexOf("."));
while (ulong.Parse(strAmtIntPart) / 1000.00 > 1)
{
iRecyle += 1;
strAmtIntPart = strAmtIntPart.Substring(0,strAmtIntPart.Length - 3);
}
if (int.Parse(strAmtIntPart) >= 1 && int.Parse(strAmtIntPart) < 10 && iRecyle > 0)
{
if (strBefRep.Substring(oPos, 1).Equals(","))
{
strAftRep = strAftRep.Remove(iPos, 1);
iPos = iPos;
oPos = oPos;
}
else
{
strAftRep = strAftRep.Remove(iPos, 1);
iPos = iPos;
int iLastComma = strBefRep.LastIndexOf(",");
if (oPos <= iLastComma)
oPos = oPos - 1;
else
oPos = oPos;
}
}
priscill 2005-07-25
  • 打赏
  • 举报
回复
if (!(HaveDot))
{
if (iSelectionLength > 0)
{
strAftRep = strAftRep.Remove(iSelectionStart, strSelectionTxt.Replace(",","").Length);
string beCutRight = strBefRep.Substring(oPos, strBefRep.Length - oPos);
strAftRep = strAftRep.Insert(iPos, st);
strBefRep = objAAC.AddComma(strAftRep,ref oPos,ref iPos);
int iTmpoPos = strBefRep.LastIndexOf(beCutRight);
iPos = iPos;
oPos = iTmpoPos;
iSelectionLength = 0;
strSelectionTxt = "";
}
else if (iSelectionLength < 0)
{
strAftRep = strAftRep.Remove(iPos - strSelectionTxt.Replace(",","").Length, strSelectionTxt.Replace(",","").Length);
string beCutRight = strBefRep.Substring(oPos - iSelectionLength, strBefRep.Length - oPos + iSelectionLength);
strAftRep = strAftRep.Insert(iPos - strSelectionTxt.Replace(",","").Length, st);
strBefRep = objAAC.AddComma(strAftRep,ref oPos,ref iPos);
int iTmpoPos = strBefRep.LastIndexOf(beCutRight);
iPos = iPos - strSelectionTxt.Replace(",","").Length;
oPos = iTmpoPos - (Math.Abs(iSelectionLength) - strSelectionTxt.Replace(",","").Length);
iSelectionLength = 0;
strSelectionTxt = "";
}
else
{
strAftRep = strAftRep.Insert(iPos, st);
}
}
else
return false;

if (strAftRep.IndexOf(".") != -1)
{
int iRecyle = 0;
string strAmtIntPart = strAftRep.Substring(0,strAftRep.IndexOf("."));
while (ulong.Parse(strAmtIntPart) / 1000.00 > 1)
{
iRecyle += 1;
strAmtIntPart = strAmtIntPart.Substring(0,strAmtIntPart.Length - 3);
}
if (int.Parse(strAmtIntPart) >= 1 && int.Parse(strAmtIntPart) < 10 && iRecyle > 0)
{
iPos = iPos + 1;
oPos = oPos + 2;
}
else if (int.Parse(strAmtIntPart) >= 10 && int.Parse(strAmtIntPart) < 1000 && iRecyle > 0)
{
iPos = iPos + 1;
oPos = oPos + 1;
}
else
{
iPos = iPos + 1;
oPos = oPos + 1;
}
}
else
{
int iRecyle = 0;
string strAmtIntPart = strAftRep;
while (ulong.Parse(strAmtIntPart) / 1000.00 > 1)
{
iRecyle += 1;
strAmtIntPart = strAmtIntPart.Substring(0,strAmtIntPart.Length - 3);
}
if (int.Parse(strAmtIntPart) >= 1 && int.Parse(strAmtIntPart) < 10 && iRecyle > 0)
{
iPos = iPos + 1;
oPos = oPos + 2;
}
else if (int.Parse(strAmtIntPart) >= 10 && int.Parse(strAmtIntPart) < 1000 && iRecyle > 0)
{
iPos = iPos + 1;
oPos = oPos + 1;
}
else
{
iPos = iPos + 1;
oPos = oPos + 1;
}
}
strBefRep = objAAC.AddComma(strAftRep,ref oPos,ref iPos);
}
if (KeyAscii == 37)
{
if (iPos >= 1)
{
if (iSelectionLength > 0)
{
iPos = iPos;
oPos = oPos;
iSelectionLength = 0;
strSelectionTxt = "";
}
else if (iSelectionLength < 0)
{
if (!(strBefRep.Substring(oPos - 1, 1).ToString().Trim().Equals(",")))
{
iPos = iPos - 1;
//oPos = oPos - 1;
}
else
{
//oPos = oPos - 1;
}
iSelectionLength = 0;
strSelectionTxt = "";
}
else
{
if (!(strBefRep.Substring(oPos - 1, 1).ToString().Trim().Equals(",")))
{
iPos = iPos - 1;
oPos = oPos - 1;
}
else
{
oPos = oPos - 1;
}
}
}
}
priscill 2005-07-25
  • 打赏
  • 举报
回复
项目时间较紧,没法仔细地做下去,下面将部分代码贴出来,希望哪位可以帮忙继续完善或优化一下,目前针对SHIFT+左右键选中一部分后,按“DEL”或“Backspace”造成光标异位。

这是ctlTxtWrap.cs

一贴不够,分多贴:
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace TxtWrap.CSharp
{
/// <summary>
/// UserControl1 的摘要说明。
/// </summary>
public class ctlTxtWrap : System.Windows.Forms.UserControl
{
private AmountAddComma objAAC;
private int oPos = 0;
private int iPos = 0;
private string strAftRep = "";
private string strBefRep = "";
private System.Windows.Forms.TextBox textBox1;

private int iSelectionLength = 0;//用于记录选中的内容长度
private int iSelectionStart = 0;
private string strSelectionTxt = "";
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public ctlTxtWrap()
{
// 该调用是 Windows.Forms 窗体设计器所必需的。
InitializeComponent();

// TODO: 在 InitComponent 调用后添加任何初始化

}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if( components != null )
components.Dispose();
}
base.Dispose( disposing );
}

#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.textBox1.Location = new System.Drawing.Point(0, 0);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(144, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
this.textBox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
this.textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);
this.textBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.textBox1_MouseDown);
this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);
this.textBox1.DoubleClick += new System.EventHandler(this.textBox1_DoubleClick);
this.textBox1.KeyUp += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyUp);
//
// ctlTxtWrap
//
this.BackColor = System.Drawing.SystemColors.Control;
this.Controls.Add(this.textBox1);
this.Name = "ctlTxtWrap";
this.Size = new System.Drawing.Size(144, 21);
this.Load += new System.EventHandler(this.ctlTxtWrap_Load);
this.ResumeLayout(false);

}
#endregion

private void ctlTxtWrap_Load(object sender, System.EventArgs e)
{
objAAC = new AmountAddComma();
}

private void textBox1AddMenu()
{
//创建ContextMenu并将其分配到TextBox控件
ContextMenu rightMouseButtonContextMenu = new ContextMenu();
textBox1.ContextMenu = rightMouseButtonContextMenu;
}

private void textBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
textBox1AddMenu();
else if (e.Button == MouseButtons.Left)
{
oPos = this.textBox1.SelectionStart;
iPos = this.textBox1.Text.Substring(0,oPos).Replace(",","").Length;
}
iSelectionStart = oPos;
iSelectionLength = 0;
strSelectionTxt = "";
this.textBox1.SelectionStart = iSelectionStart;
this.textBox1.SelectionLength = 0;
}

private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
//MessageBox.Show(((int)e.KeyChar).ToString());
e.Handled = true;
}

private bool test1(int KeyAscii)
{
try
{
oPos = this.textBox1.SelectionStart;//有逗号金额中光标的位置
strBefRep = objAAC.AddComma(strAftRep,ref oPos,ref iPos);

if ((KeyAscii >= 48 && KeyAscii <= 57) || (KeyAscii >= 96 && KeyAscii <= 105) || KeyAscii == 110 || KeyAscii == 190)
{
if (oPos == 0)
{
if (KeyAscii == 48 || KeyAscii == 96 || KeyAscii == 110 || KeyAscii == 190)
{
return false;
}
}

int iKeyAscii = 0;
if (KeyAscii >= 96 && KeyAscii <= 105)
iKeyAscii = KeyAscii - 48;
else if (KeyAscii == 110)
iKeyAscii = KeyAscii - 64;
else if (KeyAscii == 190)
iKeyAscii = KeyAscii - 144;
else
iKeyAscii = KeyAscii;

string st = ((char)iKeyAscii).ToString().Trim();
bool HaveDot = false;
if (st.Equals("."))
{
string str = strAftRep.ToString().Trim();
while (str.Length > 0)
{
char ch = char.Parse(str.Substring(0,1));
if (ch == 46)
{
HaveDot = true;
break;
}
str = str.Substring(1,str.Length - 1);
}
}
Alang_79 2005-07-12
  • 打赏
  • 举报
回复
「已注销」 2005-07-12
  • 打赏
  • 举报
回复
太多,不好
cf1944 2005-07-12
  • 打赏
  • 举报
回复
....鼎,慢慢看
priscill 2005-07-12
  • 打赏
  • 举报
回复
以下是AmountAddComma.cs文件

using System;
using System.Data;

namespace TxtWrap.CSharp
{
/// <summary>
/// AmountAddComma 的摘要说明。
/// </summary>
public class AmountAddComma
{
public AmountAddComma()
{
//
// TODO: 在此处添加构造函数逻辑
//
}

public string AddComma(string strAmt)
{
string strAmtIntegralPart = "";
string strAmtAfterAddComma = "";
string strAmtDecimalPart = "";
int iPointPosition;
if ((iPointPosition = strAmt.IndexOf(".")) == -1)
{
strAmtIntegralPart = strAmt;
}
else
{
strAmtIntegralPart = strAmt.Substring(0,iPointPosition);
strAmtDecimalPart = strAmt.Substring(iPointPosition, strAmt.Length - iPointPosition);
}

if (strAmtIntegralPart.Length <= 3)
{
strAmtAfterAddComma = strAmtIntegralPart;
if (strAmt.IndexOf(".") == -1)
return strAmtAfterAddComma;
else
return strAmtAfterAddComma + strAmtDecimalPart;
}
else
{
while ((ulong.Parse(strAmtIntegralPart) / 1000.0) > 1)
{
strAmtAfterAddComma = "," + strAmtIntegralPart.Substring(strAmtIntegralPart.Length - 3, 3) + strAmtAfterAddComma;
strAmtIntegralPart = strAmtIntegralPart.Substring(0,strAmtIntegralPart.Length -3);
}
if (strAmt.IndexOf(".") == -1)
strAmtAfterAddComma = strAmtIntegralPart + strAmtAfterAddComma;
else
strAmtAfterAddComma = strAmtIntegralPart + strAmtAfterAddComma + strAmtDecimalPart;

return strAmtAfterAddComma;
}
}
}
}
using System;
using System.Data;

namespace TxtWrap.CSharp
{
/// <summary>
/// AmountAddComma 的摘要说明。
/// </summary>
public class AmountAddComma
{
public AmountAddComma()
{
//
// TODO: 在此处添加构造函数逻辑
//
}

public string AddComma(string strAmt)
{
string strAmtIntegralPart = "";
string strAmtAfterAddComma = "";
string strAmtDecimalPart = "";
int iPointPosition;
if ((iPointPosition = strAmt.IndexOf(".")) == -1)
{
strAmtIntegralPart = strAmt;
}
else
{
strAmtIntegralPart = strAmt.Substring(0,iPointPosition);
strAmtDecimalPart = strAmt.Substring(iPointPosition, strAmt.Length - iPointPosition);
}

if (strAmtIntegralPart.Length <= 3)
{
strAmtAfterAddComma = strAmtIntegralPart;
if (strAmt.IndexOf(".") == -1)
return strAmtAfterAddComma;
else
return strAmtAfterAddComma + strAmtDecimalPart;
}
else
{
while ((ulong.Parse(strAmtIntegralPart) / 1000.0) > 1)
{
strAmtAfterAddComma = "," + strAmtIntegralPart.Substring(strAmtIntegralPart.Length - 3, 3) + strAmtAfterAddComma;
strAmtIntegralPart = strAmtIntegralPart.Substring(0,strAmtIntegralPart.Length -3);
}
if (strAmt.IndexOf(".") == -1)
strAmtAfterAddComma = strAmtIntegralPart + strAmtAfterAddComma;
else
strAmtAfterAddComma = strAmtIntegralPart + strAmtAfterAddComma + strAmtDecimalPart;

return strAmtAfterAddComma;
}
}
}
}
shliger 2005-07-12
  • 打赏
  • 举报
回复
要是有现成的控件就好了。
priscill 2005-07-12
  • 打赏
  • 举报
回复

private bool test1(int KeyAscii)
{
try
{
oPos = this.textBox1.SelectionStart;//有逗号金额中光标的位置
strBefRep = objAAC.AddComma(strAftRep);

if ((KeyAscii >= 48 && KeyAscii <= 57) || (KeyAscii >= 96 && KeyAscii <= 105) || KeyAscii == 110 || KeyAscii == 190)
{
if (oPos == 0)
{
if (KeyAscii == 48 || KeyAscii == 96 || KeyAscii == 110 || KeyAscii == 190)
{
return false;
}
}

int iKeyAscii = 0;
if (KeyAscii >= 96 && KeyAscii <= 105)
iKeyAscii = KeyAscii - 48;
if (KeyAscii == 110)
iKeyAscii = KeyAscii - 64;
if (KeyAscii == 190)
iKeyAscii = KeyAscii - 144;

string st = ((char)iKeyAscii).ToString().Trim();
bool HaveDot = false;
if (st.Equals("."))
{
string str = strAftRep.ToString().Trim();
while (str.Length > 0)
{
char ch = char.Parse(str.Substring(0,1));
if (ch == 46)
{
HaveDot = true;
break;
}
str = str.Substring(1,str.Length - 1);
}
}
if (!(HaveDot))
strAftRep = strAftRep.Insert(iPos,st);
else
return false;

if (strAftRep.IndexOf(".") != -1)
{
int iRecyle = 0;
string strAmtIntPart = strAftRep.Substring(0,strAftRep.IndexOf("."));
while (ulong.Parse(strAmtIntPart) / 1000.00 > 1)
{
iRecyle += 1;
strAmtIntPart = strAmtIntPart.Substring(0,strAmtIntPart.Length - 3);
}
if (int.Parse(strAmtIntPart) >= 1 && int.Parse(strAmtIntPart) < 10 && iRecyle > 0)
{
iPos = iPos + 1;
oPos = oPos + 1;
}
else
{
iPos = iPos + 1;
oPos = oPos + 1;
}
}
else
{
int iRecyle = 0;
string strAmtIntPart = strAftRep;
while (ulong.Parse(strAmtIntPart) / 1000.00 > 1)
{
iRecyle += 1;
strAmtIntPart = strAmtIntPart.Substring(0,strAmtIntPart.Length - 3);
}
if (int.Parse(strAmtIntPart) >= 1 && int.Parse(strAmtIntPart) < 10 && iRecyle > 0)
{
iPos = iPos + 1;
oPos = oPos + 2;
}
else
{
iPos = iPos + 1;
oPos = oPos + 1;
}
}
strBefRep = objAAC.AddComma(strAftRep);
}
if (KeyAscii == 37)
{
if (iPos >= 1)
{
if (!(strBefRep.Substring(oPos, 1).ToString().Trim().Equals(",")))
iPos = iPos - 1;
}
}
if (KeyAscii == 39)
{
if (iPos < strAftRep.Length)
{
if (!(strBefRep.Substring(oPos - 1, 1).ToString().Trim().Equals(",")))
iPos = iPos + 1;
}
}
if (KeyAscii == 8)
{
if (oPos <= strBefRep.Length && oPos >= 1 && iPos >= 1)
{
if (strBefRep.Substring(oPos - 1, 1).Equals(","))
{
oPos = oPos - 2;
strAftRep = strAftRep.Remove(iPos - 1, 1);
iPos = iPos - 1;
}
else
{
strAftRep = strAftRep.Remove(iPos - 1, 1);
iPos = iPos - 1;
}
}
}
else if (KeyAscii == 46)
{
if (oPos <= strBefRep.Length && oPos >= 1 && iPos >= 1)
{
if (strBefRep.Substring(oPos, 1).Equals(","))
{
strAftRep = strAftRep.Remove(iPos + 1, 1);
oPos = oPos;
iPos = iPos;
}
else
{
strAftRep = strAftRep.Remove(iPos, 1);
oPos = oPos;
iPos = iPos;
}
}
}
this.textBox1.Text = objAAC.AddComma(strAftRep);
this.textBox1.SelectionStart = oPos;
}
catch(Exception ex)
{
System.Console.WriteLine(ex.Message.ToString());
}
return true;
}

private void textBox1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
int KeyAscii;
KeyAscii = (int)e.KeyValue;

if ((KeyAscii >= 48 && KeyAscii <= 57) || (KeyAscii >= 96 && KeyAscii <= 105) || KeyAscii == 37 || KeyAscii == 39 || KeyAscii == 110 || KeyAscii == 190 || KeyAscii == 8 || KeyAscii == 13)
{
//格式化
if (!(test1(KeyAscii)))
{
return;
}
}
}

private void textBox1_TextChanged(object sender, System.EventArgs e)
{
if (this.textBox1.Text.ToString().Trim().Equals(strBefRep.Trim()))
{
//MessageBox.Show("Input Steply!");
}
else
{
//MessageBox.Show("Suddenly Import!");
string str = this.textBox1.Text.ToString().Trim();
strAftRep = str.Replace(",","");
strBefRep = objAAC.AddComma(strAftRep);
iPos = str.Length;
oPos = strBefRep.Length;
this.textBox1.Text = strBefRep;
this.textBox1.SelectionStart = oPos;
}
}
}
}
priscill 2005-07-12
  • 打赏
  • 举报
回复
这是ctlTxtWrap.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace TxtWrap.CSharp
{
/// <summary>
/// UserControl1 的摘要说明。
/// </summary>
public class ctlTxtWrap : System.Windows.Forms.UserControl
{
private AmountAddComma objAAC;
private int oPos = 0;
private int iPos = 0;
private string strAftRep = "";
private string strBefRep = "";
private System.Windows.Forms.TextBox textBox1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public ctlTxtWrap()
{
// 该调用是 Windows.Forms 窗体设计器所必需的。
InitializeComponent();

// TODO: 在 InitComponent 调用后添加任何初始化

}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if( components != null )
components.Dispose();
}
base.Dispose( disposing );
}

#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.textBox1.Location = new System.Drawing.Point(0, 0);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(144, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
this.textBox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
this.textBox1.KeyUp += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyUp);
//
// ctlTxtWrap
//
this.BackColor = System.Drawing.SystemColors.Control;
this.Controls.Add(this.textBox1);
this.Name = "ctlTxtWrap";
this.Size = new System.Drawing.Size(144, 21);
this.Load += new System.EventHandler(this.ctlTxtWrap_Load);
this.ResumeLayout(false);

}
#endregion

private void ctlTxtWrap_Load(object sender, System.EventArgs e)
{
objAAC = new AmountAddComma();
}

private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
e.Handled = true;
}

priscill 2005-07-12
  • 打赏
  • 举报
回复
"Delete"键控制不了,不响应我的操作,哪位大侠能帮我分析一下,源代码如下:
fq_ln 2005-07-08
  • 打赏
  • 举报
回复
这个除非做个专用的控件,否则处理起来比较麻烦
cliff1002 2005-07-08
  • 打赏
  • 举报
回复
~~~~~~~~~~~~这个有点太。。。仔细了吧?

不过,也是能实现的。

“问题是如果用户按了"Backspace"、"Delete"、"Left"、"Right"等键,该事件如何处理?”

在keypress事件中根据其keycode判断按了"Backspace"、"Delete"、"Left"、"Right"等键中的哪一个,然后进行相应处理。

可以用case语句来写

比如:

select case e.KeyCode
case Keys.Backspace
.... //相应处理
case Keys.Delete
....
end select
priscill 2005-07-08
  • 打赏
  • 举报
回复
menghun1355(梦魂):

按你建议的那样做,只方便了用户查看,但是在更改此文本框时,还得仔细地数位啊。
否则焦点进进出出,用户用得也不方便啊

还有哪些大侠给些建议?
莫非把这个封装成一个控件?
sfanpu 2005-07-08
  • 打赏
  • 举报
回复
这样有些太复杂了,还是像 menghun1355(梦魂) 说的比较好一些。
shliger 2005-07-08
  • 打赏
  • 举报
回复
我鼎
menghun1355 2005-07-08
  • 打赏
  • 举报
回复
为什么要这样做呀?你不可以他输入的时候就给他普通模式,当焦点离开时再用逗号分隔吗.
就是用户获得焦点时去掉逗号,失去焦点时给他加上逗号!
我看到过的这类业务都是这么做的.

行吗?
加载更多回复(1)

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧