Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
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
Posted
Updated 26-Nov-14 5:25am
v2
Comments
/\jmot 26-Nov-14 11:05am    
now, see my Answer,Just Updated.

Use a custom format string as described in http://msdn.microsoft.com/en-us/library/0c899ak8(v=vs.110).aspx[^].
 
Share this answer
 
Comments
Maciej Los 26-Nov-14 12:06pm    
+5!
Try this:
C#
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[^]
 
Share this answer
 
try this..
C#
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..
C#
string str1 = TextBox1.Text;
Int32 intVal = Convert.ToInt32(str1);
TextBox1.Text = (intVal + 1).ToString("00#");
 
Share this answer
 
v3
Comments
Richard MacCutchan 26-Nov-14 12:27pm    
Not a good suggestion. Use format strings for numbers (and StringBuilder for building strings).
/\jmot 26-Nov-14 12:52pm    
thank you @Richard MacCutchan for the suggestion,I really missed that.
thank you very much. :)
MaroofQaiser 27-Nov-14 0:11am    
thanx Aajmot Sk , but i want last insert number from database, i mean max id, no to define like that : Textbox1.text = "001";
/\jmot 27-Nov-14 0:19am    
so what, just run a sql query to get the max ID from the table.
Qry=Select max(isnull(ID,0))+1 as NextID from Table_name;

and insert next row as..
insert into Table(Id,Name) values(NextId,'NAME HERE')
MaroofQaiser 27-Nov-14 8:29am    
thanx alot.. i have finished the task
Using the "zero custom specifier" [^]:
C#
private string intToPaddedZeroString(int i, int nZeroes)
{
    return i.ToString(zeroes.Substring(0, nZeroes));
}

// test in some method or EventHandler

for (int i = 1; i < 101; i++)
{
   SomeTextBox.AppendText(intToPaddedZeroString(i, 3) + Environment.NewLine);
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900