在文本框中添加一个数字 [英] Add a number in textbox

查看:142
本文介绍了在文本框中添加一个数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在文本框中添加序列号,但格式为001以后,主要问题是当数字变为009时,它变为0010,四位数字符号。我想从它减少零,数字应该看起来像010 ..请帮助我解决这个问题

解决方案

使用自定义格式字符串,如 http://msdn.microsoft.com/en-us/library/0c899ak8(ⅴ = vs.110).aspx [ ^ ]。


试试这个:

 使用系统; 
public class 计划
{
public static void Main()
{
int x = 1 ;
string s = x.ToString( 000\" );
Console.WriteLine(s);
x = 1 + 9;
s = x.ToString( 000);
Console.WriteLine(s);
x = 1 + 99;
s = x.ToString( 000);
Console.WriteLine(s);
}
}



参考:自定义数字格式字符串 [ ^ ]


试试这个..

  protected   void  Page_Load( object  sender,EventArgs e)
{
if (!IsPostBack)
{
TextBox1.Text = 001;
}
}
受保护 void btnAdd_Click( object sender,EventArgs e)
{
string print = ;
string str1 = TextBox1.Text;
Int32 minLen = 3 ;
Int32 intVal = Convert.ToInt32(str1);
Int32 valLen = intVal.ToString()。Length;

int i =(intVal + 1 )。ToString()。长度;
while (i < minLen)
{
print = print + 0;
i ++;
}
print = print +(intVal + 1 )。ToString();
TextBox1.Text = print;
}





更新..

  string  str1 = TextBox1.Text; 
Int32 intVal = Convert.ToInt32(str1);
TextBox1.Text =(intVal + 1 )。ToString( 00#);


i want to add sequence numbers in textbox but the format is 001 to onward, the main problem is when the number comes to 009, after it became 0010, four digit character. i want to reduce a zero from it, the number should look like 010.. please help me for this problem

解决方案

Use a custom format string as described in http://msdn.microsoft.com/en-us/library/0c899ak8(v=vs.110).aspx[^].


Try this:

using System;
public class Program
{
    public static void Main()
    {
        int x = 1;
        string s = x.ToString("000");
        Console.WriteLine(s);
        x = 1+9;
        s = x.ToString("000");
        Console.WriteLine(s);
        x = 1+99;
        s = x.ToString("000");
        Console.WriteLine(s);
    }
}


Refer: Custom Numeric Format Strings[^]


try this..

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        TextBox1.Text = "001";
    }
}
protected void btnAdd_Click(object sender, EventArgs e)
{
    string print = "";
    string str1 = TextBox1.Text;
    Int32 minLen = 3;
    Int32 intVal = Convert.ToInt32(str1);
    Int32 valLen = intVal.ToString().Length;

    int i = (intVal + 1).ToString().Length;
    while(i < minLen)
    {
        print = print + "0";
        i++;
    }
    print = print + (intVal + 1).ToString();
    TextBox1.Text = print;
}



Updated..

string str1 = TextBox1.Text;
Int32 intVal = Convert.ToInt32(str1);
TextBox1.Text = (intVal + 1).ToString("00#");


这篇关于在文本框中添加一个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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