Hi programmers,
I want to generate a 5 digit number starting from 11111... only the last digit gets incremented by 1 till it reaches 9 and after it reaches 9, the previous number starts getting incremented by 1.. This 5 digit number is unique.. I want to assign this number as a receipt number for customers...
Plzz help... thanx
Loading
theLizardPosted Jan 21, 2011, 8:04 PM
private void button2_Click(object sender, EventArgs e)
{
//put this in button click event FOR TESTING can be automated NO clicking involved.
string number = te.Text, tmp = ""; //in this case te (textBox1) contains 11111
int n = -1, pos = -1; //n gets the last char from string which is NOT a 9, pos gets the position of n
for (int i = te.Text.Length; i > 0; i--)
{
if (number[i-1].ToString() != "9") //char is NOT a 9
{
n = int.Parse(number[i-1].ToString()); //assign number[n] to the (int) n value
pos = i-1; //get the pos
break;
}
}
n++; //increment n by 1
for (int i = 0; i < te.Text.Length; i++) //loop until i = pos
{
if (i != pos)
tmp += number[i].ToString(); //rebuild string value should be "1111" until pos of n
else
tmp += n.ToString(); //string value should now be "11112" after first increment of n
}
te.Text = tmp;
}
this method will work for 11111 or any other length of 1's this -> 11111111111111111111111 makes no difference..
FroglegPosted Jan 21, 2011, 4:23 PM
Jaish MathewsPosted Jan 21, 2011, 1:47 PM
If your number 11111 moving towards the ultimate goal of 99999, Try below
public partial class Form1 : Form
{
int initNumber = 11111;
int increment = 0;
private void button1_Click_1(object sender, EventArgs e)
{
increment = increment + 1;
if(increment <=8)
{
initNumber = initNumber + 1;
}
else if (increment <= 16)
{
initNumber = initNumber + 10;
}
else if (increment <= 24)
{
initNumber = initNumber + 100;
}
else if (increment <= 32)
{
initNumber = initNumber + 1000;
}
else if (increment <= 40)
{
initNumber = initNumber + 10000;
}
MessageBox.Show(initNumber.ToString());
}
}
Ananya SuhasPosted Jan 21, 2011, 11:47 AM
Thanx for the reply but this is not wat i am looking for
FroglegPosted Jan 17, 2011, 1:57 PM
Add a button,textbox and a label to a form and have a look.
public partial class Form1 : Form
{
int[] mydigit =new int [5];
string strA , strB, strC , strD , strE;
int count = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
loadTxt();
}
void loadTxt()
{
mydigit[0] = 1;
mydigit[1] = 1;
mydigit[2] = 1;
mydigit[3] = 1;
mydigit[4] = 1;
strA = Convert.ToString(mydigit[0]);
strB = Convert.ToString(mydigit[1]);
strC = Convert.ToString(mydigit[2]);
strD = Convert.ToString(mydigit[3]);
strE = Convert.ToString(mydigit[4]);
textBox1.Text = (strA + strB + strC + strD + strE);
}
private void button1_Click(object sender, EventArgs e)
{
increaseByOne();
count++;
label1.Text = Convert.ToString(count);
}
private void increaseByOne()
{
if (mydigit[4] < 9)
{
mydigit[4] = mydigit[4] + 1;
}
else if (mydigit[3] < 9)
{
mydigit[3] = mydigit[3] + 1;
}
else if (mydigit[2] < 9)
{
mydigit[2] = mydigit[2] + 1;
}
else if (mydigit[1] < 9)
{
mydigit[1] = mydigit[1] + 1;
}
else if (mydigit[0] < 9)
{
mydigit[0] = mydigit[0] + 1;
}
strA = Convert.ToString(mydigit[0]);
strB = Convert.ToString(mydigit[1]);
strC = Convert.ToString(mydigit[2]);
strD = Convert.ToString(mydigit[3]);
strE = Convert.ToString(mydigit[4]);
textBox1.Text = (strA + strB + strC + strD + strE);
}
}