Click here to Skip to main content
16,016,744 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello!




What I want is that, for each time that : 1,2,3,4 loop it will ad a number before, like its counting but it stops at 4. so: 1,2,3,4,11,12,13,14,21,22,23,24,31,32,33,34,41,42,43,44,111,112,113,114,121,122,123,124,131,132,133, 134,141,142,143,144,211........
just like counting but with 4 numbers instead of 9.

And i want it to work like this, for each time that i press the button i want it to show a new number.
String[] text = { "1", "2", "3", "4" };
        int count = 0;

        private void button1_Click(object sender, EventArgs e)
        {
            if (count == text.Length)
            {
                count = 0;
                
            }
            label1.Text = text[(count % text.Length)];
            count++;

        } 



Thank you!
Posted
Updated 15-Jan-11 5:41am
v5
Comments
Estys 15-Jan-11 8:16am    
If our answers didn't work, what do you want to do then? Insert it somewhere in the middle?
OriginalGriff 15-Jan-11 8:30am    
Answer updated
Estys 15-Jan-11 9:40am    
Can you explain what you want your label1 to show? The expression "count % text.Length" always equals "count" AFAICS.
OriginalGriff 15-Jan-11 10:12am    
Answer updated again - sorry there appears to be a problem with the site, emails aren't working. It has been reported and will be fixed soon (I hope).
Estys 15-Jan-11 11:05am    
Answer updated, hope you wanted this.

Why not just go:
myLabel.Text = "My new text: " + myLabel.Text;



"No, actually i found out that it doesn't work!... for my thing - euhiemf 31 mins ago"

So why not? What "doesn't work"?
String[] text = { "1", "2", "3", "4" };
int count = 0;

private void button1_Click(object sender, EventArgs e)
{
    if (count == text.Length)
    {
        count = 0;

    }
    label1.Text = text[(count % text.Length)];
    count++;

}
- euhiemf 35 mins ago


Ok, so it's a code fragment. It appears to have nothing to do with your question.
Your fragment just sets a label text to a different value each time a button is pressed. If it isn't intended to do that, what is it meant to do? :laugh:
"what i want is that for each time that : 1,2,3,4 loop it will ad a number before, like its counting but it stops at 4. so: 1,2,3,4,11,12,13,14,21,22,23,24,31,32,33,34,41,42,43,44,111,112,113,114,121,122,123,124,131,132,133, 134,141,142,143,144,211........ just like counting but with 4 numbers instead of 9. ;) - euhiemf 25 mins ago"

Oh, is that all:
You can't do that quite so simply, you have no zero to play with! So. it gets a bit complex...
C#
int[] currentValue = new int[3] { 0, 0, 0 };
private void button1_Click(object sender, EventArgs e)
    {
    for (int i = 0; i < currentValue.Length; i++)
        {
        if (currentValue[i] != 4)
            {
            currentValue[i]++;
            break;      // No need to move next digit.
            }
        else
            {
            // Move the next highest digit.
            currentValue[i] = 1;
            }
        }
    string s = "";
    for (int i = currentValue.Length - 1; i >=0; i--)
        {
        s += currentValue[i].ToString();
        }
    label1.Text = s;

This only handles three quadits (Can't call then digits, the "dig" comes from base ten) and the first press will set the label to "001", but I have to leave you some work!
 
Share this answer
 
v4
Comments
euhiemf 15-Jan-11 7:58am    
No, actually i found out that it doesn't work!... for my thing
euhiemf 15-Jan-11 9:29am    
String[] text = { "1", "2", "3", "4" };
int count = 0;

private void button1_Click(object sender, EventArgs e)
{
if (count == text.Length)
{
count = 0;

}
label1.Text = text[(count % text.Length)];
count++;

}
euhiemf 15-Jan-11 10:36am    
what i want is that for each time that : 1,2,3,4 loop it will ad a number before, like its counting but it stops at 4. so: 1,2,3,4,11,12,13,14,21,22,23,24,31,32,33,34,41,42,43,44,111,112,113,114,121,122,123,124,131,132,133, 134,141,142,143,144,211........ just like counting but with 4 numbers instead of 9. ;)
euhiemf 15-Jan-11 11:58am    
Thank you so much! Amazing, Thanks for all your help! Im sorry that i confused you before i just changed my mind a couple times.
Obviously you can't use the SelectionStart property.
The Insert should work though, it's a String method.

Why not simply
label1.Text = "labe" + label1.Text;

?

Did you see this : Convert to any base[^]

Change your array to
string quat = IntToString(42, 
            new char[] { '0', '1', '2', '3', '4' };


Put this function in your code :
public static string IntToString(int value, char[] baseChars)
{
    string result = string.Empty;
    int targetBase = baseChars.Length;

    do
    {
        result = baseChars[value % targetBase] + result;
        value = value / targetBase;
    }
    while (value > 0);

    return result;
}


Now rewrite your function :

private void button1_Click(object sender, EventArgs e)
{
    label1.Text = IntToString(count, quat);
    count++;

}


Close enough, not tested but about right :)
 
Share this answer
 
v2
Comments
euhiemf 15-Jan-11 7:57am    
No, actually i found out that it doesn't work!... for my thing
OriginalGriff 15-Jan-11 11:21am    
Did you look at his numbers? He has no zero! :laugh:
Estys 15-Jan-11 12:35pm    
Yes, I saw that, not really logical, I thought I'd better do it right :)
OriginalGriff 16-Jan-11 4:58am    
Correcting the OP problem as well as trying to solve it! :laugh:
Gets my 5!

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