格式化文本框以使用实时输入的值替换设置的占位符值 [英] Format the textbox to replace the set placeholder values with the entered values real time

查看:56
本文介绍了格式化文本框以使用实时输入的值替换设置的占位符值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我想用C#存档的内容。

我有一个文本框,我将用它来输入金额。



当前我手动输入全部金额,包括小数,例如10.05



我想要达到的目标是在文本框中设置0.00的默认值,这样如果我按下一个数字,例如55,文本框自动替换默认0.00值的前2个零,显示0.55而不受我的任何人工干扰。



如果我输入3355,它必须自动显示为33.55实时,因为我正在键入

解决方案





谢谢你花时间研究我的问题。我找到了答案,它在下面。



-----------------------



 使用系统; 
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Windows.Forms;
使用 System.Globalization;
使用 System.Text.RegularExpressions;
使用 Microsoft.VisualBasic;

命名空间 frmMain
{

public partial class frmMain:表格
{

public frmMain()
{
InitializeComponent();
}


私有 void textBox1_TextChanged(< span class =code-keyword> object sender,EventArgs e)
{
// 删除以前的格式,否则小数点检查将失败,包括前导零
string value = textBox1.Text.Replace(
.Replace(


)。替换( )。TrimStart(' 0');
decimal ul;
// 检查我们确实正在处理一个数字
if decimal .TryParse( value out ul))
{
ul / = 100 ;
// 取消显示事件,以便我们不输入循环
textBox1 .TextChanged - = textBox1_TextChanged;
// 将文本格式化为货币
textBox1.Text = string .Format(CultureInfo.CreateSpecificCulture( en-US), {0:C2},ul);
textBox1.TextChanged + = textBox1_TextChanged;
textBox1.Select(textBox1.Text.Length, 0 );
}


}

private bool TextisValid( string text)
{
Regex money = new 正则表达式( @ ^ \


(\d {1 ,3}(\,\d {3})* |(\d +))(\.\d {2})

Below is what I am trying to archive in C#.
I have a textbox that I will be using to enter an amount.

Current I am entering the full amount manually including the decimal e.g. "10.05"

What I want to achieve is to have a default value on the textbox of "0.00" such that if I press a number e.g. 55, the textbox automatically replaces the first 2 zeros of the default "0.00" value to display "0.55" without any manual interference from me.

If I enter 3355, it has to automatically get displayed as 33.55 real time as I am typing

解决方案

Hi,

Thank you for taking time to look into my problem. I have found the answer and it is below.

-----------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Text.RegularExpressions;
using Microsoft.VisualBasic;

namespace frmMain
{

    public partial class frmMain : Form
    {
        
        public frmMain()
        {
            InitializeComponent();
        }


        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            //Remove previous formatting, or the decimal check will fail including leading zeros
            string value = textBox1.Text.Replace(",", "")
                .Replace("


", "").Replace(".", "").TrimStart('0'); decimal ul; //Check we are indeed handling a number if (decimal.TryParse(value, out ul)) { ul /= 100; //Unsub the event so we don't enter a loop textBox1.TextChanged -= textBox1_TextChanged; //Format the text as currency textBox1.Text = string.Format(CultureInfo.CreateSpecificCulture("en-US"), "{0:C2}", ul); textBox1.TextChanged += textBox1_TextChanged; textBox1.Select(textBox1.Text.Length, 0); } } private bool TextisValid(string text) { Regex money = new Regex(@"^\


(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?


这篇关于格式化文本框以使用实时输入的值替换设置的占位符值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆