Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
See more:
Hi guys how do i put this a loop where if user clicks build button, "Plot 2" will change to "Plant 2" and "Plot 3" will appear below "Plant 2" with same style like Plot 2

This is PLOT 2
C#
message.AppendLine();
message.AppendLine("Plot 2");
message.Append(string.Format("$:{0} ", Cash), Color.Gray, TextMarkup.FontDecrease);
message.Append(string.Format("F:{0} ", Food), Color.Gray, TextMarkup.FontDecrease);
message.Append(string.Format("C:{0} ", Concrete), Color.Gray, TextMarkup.FontDecrease);
message.AppendLine(string.Format("M:{0}", Metal), Color.Gray, TextMarkup.FontDecrease);
message.AppendLine(string.Format("Time: {0}", Time), Color.Gray, TextMarkup.FontDecrease);
message.AppendLine(MessageBuilder.Elements.CreateLink("btnBuild", "Build", ""));
Posted
Comments
ZurdoDev 22-Apr-14 15:38pm    
Do you know how to do a loop? Just change ("Plot 2") with ("Plot " + i.ToString()) and do a loop.
Nico_Travassos 22-Apr-14 15:40pm    
I know how to do a loop but this one is rather confusing i'm still a beginner at this.
Sergey Alexandrovich Kryukov 22-Apr-14 15:56pm    
Than explain what is your problem. And learn more about loops, different forms of loops, loop variables, etc. Learn it all, then act accordingly.
—SA
Emre Ataseven 22-Apr-14 16:06pm    
Loop with just 2 iterations? Makes no sense.
[no name] 22-Apr-14 21:56pm    
What's your coding issue just explain it clearly

1 solution

You can keep a count of number of times you want to generate this pattern in a variable. (If this value is increasing everytime button is clicked, global variable can be used for keeping the count ... say maxCount) and then you can loop it as below:

C#
for (int i = 0; i < maxCount; i++)
            {
                message.AppendLine();

                if (i==maxCount-1)
                    message.AppendLine("Plot " + i+1);
                else
                    message.AppendLine("Plant " + i+1);

                message.Append(string.Format("$:{0} ", Cash), Color.Gray, TextMarkup.FontDecrease);
                message.Append(string.Format("F:{0} ", Food), Color.Gray, TextMarkup.FontDecrease);
                message.Append(string.Format("C:{0} ", Concrete), Color.Gray, TextMarkup.FontDecrease);
                message.AppendLine(string.Format("M:{0}", Metal), Color.Gray, TextMarkup.FontDecrease);
                message.AppendLine(string.Format("Time: {0}", Time), Color.Gray, TextMarkup.FontDecrease);
                message.AppendLine(MessageBuilder.Elements.CreateLink("btnBuild", "Build", ""));
            }
 
Share this answer
 
v2

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