Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm developing a project that is lucky system,but i don't know how to make label display like number counter animation. The label is employee id from text file.Below are my code.

What I have tried:

C#
public partial class _Default : System.Web.UI.Page
{
    private Random random = new Random();
    private string testFilePath = @"C:\\TestFile.txt";
    private List<string> testFileLines;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!File.Exists(testFilePath))
        {
            Response.Write($"The file does not exist: {testFilePath}");

        }
        else
        {
            try
            {
                testFileLines = File.ReadAllLines(testFilePath).ToList();
            }
            catch (Exception ex)
            {
                Response.Write($"Could not read file {testFilePath}. Exception details: {ex}");
            }
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (testFileLines != null && testFileLines.Count > 0)
        {
            var randomLine = testFileLines[random.Next(testFileLines.Count)];
            Label1.Text = randomLine;
            testFileLines.Remove(randomLine);
        }
    }
}
Posted
Comments
Maciej Los 12-Mar-18 4:15am    
And the question is...
Member 13700339 12-Mar-18 4:18am    
how to make label display like number counter animation?
abasalti 3-Jan-24 12:16pm    
123587987987987
alexvw 12-Mar-18 9:35am    
Hello Member 13700339,

In order for someone to help you, your question must be recomposed.

What do you mean by "counter animation"? your code does not provide obvious clues of any attempt to generate an animation per se.

Here's what we see:

1) On page_load, the existence of a file is checked.

a)If if does not exists, a "File does not exist" message is offered to the user.

b)If the File exists, its contents are read and used to populate the List<tring> testFileLines object.

2) On Button1_Click, an attempt to get a random item (string) from testFileLines object, is performed and its results shown by Label1. After this, said result is removed from the list.

/* Comments to Consider */

Since you are populating testFileLines on you Page_Load, it gets repopulated in full every time you hit the button. To avoid this, you need to implement/validate IsPostBack on Page_Load, otherwise, the "testFileLines.Remove(randomLine);" command is completely ineffective.

The community needs to understand your need so that it can help.
Member 13700339 12-Mar-18 23:27pm    
Can you show me how to implement/validate IsPostBack on Page_Load? thanks

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