Why does the program call to the function “CheckAnswer” before “DisplayRandomPicture()”?

    public scrLevel1()
    {
        InitializeComponent(GetRightToLeft());
        InitializePictures();
        DisplayRandomPicture();

    }
    

    private void InitializePictures()
    {
        // Initialize pictures with images and answers
        pictures = new List<Picture>
        {
            new(new string[] { @"D:4Picsdocumentbark1.jpg", @"D:4Picsdocumentbark2.jpg", @"D:4Picsdocumentbark3.jpg", @"D:4Picsdocumentbark4.jpg" }, "BARK"),
            new(new string[] { @"D:4Picsdocumentcomputer1.jpg", @"D:4Picsdocumentcomputer2.jpg", @"D:4Picsdocumentcomputer3.jpg", @"D:4Picsdocumentcomputer4.jpg" }, "COMPUTER"),
            new Picture(new string[] { @"D:4Picsdocumentfirm1.jpg", @"D:4Picsdocumentfirm2.jpg", @"D:4Picsdocumentfirm3.jpg", @"D:4Picsdocumentfirm4.jpg" }, "FIRM"),
            new Picture(new string[] { @"D:4Picsdocumenthero1.jpg", @"D:4Picsdocumenthero2.jpg", @"D:4Picsdocumenthero3.jpg", @"D:4Picsdocumenthero4.jpg" }, "HERO"),
            new Picture(new string[] { @"D:4Picsdocumentnavigate1.jpg", @"D:4Picsdocumentnavigate2.jpg", @"D:4Picsdocumentnavigate3.jpg", @"D:4Picsdocumentnavigate4.jpg" }, "NAVIGATE"),
            new Picture(new string[] { @"D:4Picsdocumentpollution1.jpg", @"D:4Picsdocumentpollution2.jpg", @"D:4Picsdocumentpollution3.jpg", @"D:4Picsdocumentpollution4.jpg" }, "POLLUTED"),
            new Picture(new string[] { @"D:4Picsdocumentschool1.jpg", @"D:4Picsdocumentschool2.jpg", @"D:4Picsdocumentschool3.jpg", @"D:4Picsdocumentschool4.jpg" }, "SCHOOL"),
            new Picture(new string[] { @"D:4Picsdocumenttriple1.jpg", @"D:4Picsdocumenttriple2.jpg", @"D:4Picsdocumenttriple3.jpg", @"D:4Picsdocumenttriple4.jpg" }, "TRIPLE"),
            // Add more pictures here
        };
    }
    public class Picture
    {
        public string[] ImagePaths { get; }
        public string CorrectAnswer { get; }

        public Picture(string[] imagePaths, string correctAnswer)
        {
            ImagePaths = imagePaths;
            CorrectAnswer = correctAnswer;
        }



    }
    
    private void DisplayRandomPicture()
    {
        position = 0;
        // Randomly select a picture from the list
        Random random = new Random();
        currentPicture = pictures[random.Next(pictures.Count)];
        answer = currentPicture.CorrectAnswer;

        // Display images of the current picture
        pic1.ImageLocation = currentPicture.ImagePaths[0];
        pic2.ImageLocation = currentPicture.ImagePaths[1];
        pic3.ImageLocation = currentPicture.ImagePaths[2];
        pic4.ImageLocation = currentPicture.ImagePaths[3];

        // Define the alphabet
        string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

        // Initialize the charArray with enough size
        charArray = new char[20];

        //put the letters of answers involved in the letter button
        for (int i = 0; i < answer.Length; i = i + 1)
        {
            charArray[i] = answer[i];
        }
        // Randomize more letters for the answer keys
        foreach (char letter in alphabet)
        {
            random = new Random();
            for (int i = answer.Length; i < 12; i++)
            {
                charArray[i] = alphabet[random.Next(alphabet.Length)];
            }

        }
        //Shuffle the letters
        for (int i = 11; i > -1; i--)
        {
            int j = random.Next(i + 1);
            var temp = charArray[i];
            charArray[i] = charArray[j];
            charArray[j] = temp;
        }
        // Set the text of the button key to character of charArray
        key1.Text = charArray[0].ToString();
        key2.Text = charArray[1].ToString();
        key3.Text = charArray[2].ToString();
        key4.Text = charArray[3].ToString();
        key5.Text = charArray[4].ToString();
        key6.Text = charArray[5].ToString();
        key7.Text = charArray[6].ToString();
        key8.Text = charArray[7].ToString();
        key9.Text = charArray[8].ToString();
        key10.Text = charArray[9].ToString();
        key11.Text = charArray[10].ToString();
        key12.Text = charArray[11].ToString();
        AnsButton = new Button[answer.Length];
        if (answer.Length == 3)
        {
            for (int i = 0; i < answer.Length; i++)
            {
                AnsButton[i] = new Button
                {
                    Name = "keyButton{i}",
                    Text = "",
                    Size = new Size(47, 48),
                    Location = new Point(269 + (i * 53), 495),
                    BackColor = Color.Black,
                    Font = new Font("Franklin Gothic Book", 12F, FontStyle.Bold),
                    ForeColor = Color.White
                };
                this.Controls.Add(AnsButton[i]);
              
            }
        }
        else if (answer.Length == 4)
        {
            for (int i = 0; i < answer.Length; i++)
            {
                AnsButton[i] = new Button
                {
                    Name = "KeyButton{i}",
                    Text = "",
                    Size = new Size(47, 48),
                    Location = new Point(238 + (i * 53), 495),
                    BackColor = Color.Black,
                    Font = new Font("Franklin Gothic Book", 12F, FontStyle.Bold),
                    ForeColor = Color.White
                };
                this.Controls.Add(AnsButton[i]);
                
            }
        }
        else if (answer.Length == 5)
        {
            for (int i = 0; i < answer.Length; i++)
            {
                AnsButton[i] = new Button
                {
                    Name = $"keyButton{i}",
                    Text = "",
                    Size = new Size(47, 48),
                    Location = new Point(215 + (i * 53), 495),
                    BackColor = Color.Black,
                    Font = new Font("Franklin Gothic Book", 12F, FontStyle.Bold),
                    ForeColor = Color.White
                };
                this.Controls.Add(AnsButton[i]);
                
            }
        }
        else if (answer.Length == 6)
        {
            for (int i = 0; i < answer.Length; i++)
            {
                AnsButton[i] = new Button
                {
                    Name = $"keyButton{i}",
                    Text = "",
                    Size = new Size(47, 48),
                    Location = new Point(184 + (i * 53), 495),
                    BackColor = Color.Black,
                    Font = new Font("Franklin Gothic Book", 12F, FontStyle.Bold),
                    ForeColor = Color.White
                };
                this.Controls.Add(AnsButton[i]);
               
            }
        }
        else if (answer.Length == 7)
        {
            for (int i = 0; i < answer.Length; i++)
            {
                AnsButton[i] = new Button
                {
                    Name = $"keyButton{i}",
                    Text = "",
                    Size = new Size(47, 48),
                    Location = new Point(161 + (i * 53), 495),
                    BackColor = Color.Black,
                    Font = new Font("Franklin Gothic Book", 12F, FontStyle.Bold),
                    ForeColor = Color.White
                };
                this.Controls.Add(AnsButton[i]);
                
            }
        }
        else if (answer.Length == 8)
        {
            for (int i = 0; i < answer.Length; i++)
            {
                AnsButton[i] = new Button
                {
                    Name = "keyButton{i}",
                    Text = "",
                    Size = new Size(47, 48),
                    Location = new Point(138 + (i * 53), 495),
                    BackColor = Color.Black,
                    Font = new Font("Franklin Gothic Book", 12F, FontStyle.Bold),
                    ForeColor = Color.White
                };
                this.Controls.Add(AnsButton[i]);
                
            }
        }
        ansinput = "";
        key1.Click += key_Click;
        key2.Click += key_Click;
        key3.Click += key_Click;
        key4.Click += key_Click;
        key5.Click += key_Click;
        key6.Click += key_Click;
        key7.Click += key_Click;
        key8.Click += key_Click;
        key9.Click += key_Click;
        key10.Click += key_Click;
        key11.Click += key_Click;
        key12.Click += key_Click;

        CheckAnswer();
    }
    private void key_Click(object sender, EventArgs e)
    {
        if (position < AnsButton.Length)
        {
            Button clickedButton = sender as Button;
            if (clickedButton != null)
            {
                AnsButton[position].Text = clickedButton.Text;
                ansinput = ansinput + clickedButton.Text;
                clickedButton.ForeColor = Color.Black;
                position++;

            }
        }
    }
    private void CheckAnswer()
    {
        if (ansinput == answer) 
        {
            MessageBox.Show("Correct!");
            // Display a new picture
            DisplayRandomPicture();
            // Clear the answer input
            ClearAnswerInput();
        }
        else if (ansinput != answer) 
        {
            MessageBox.Show("Incorrect. Try again.");
            // Reset the position and clear the answer input
            position = 0;
            ClearAnswerInput();
        }
    }

    private void ClearAnswerInput()
    {
        // Clear the text of answer buttons
        foreach (Button button in AnsButton)
        {
            button.Text = "";
        }
        // Reset the position
        position = 0;
    }

I expected it to display random pictures, allow players to click on the keybutton, clicked buttons are showed in the ansbuttons. When all the ansbuttons are filled, use the function “Checkanswers” to check the answer and show the message box. If the answer is correct, display the new one. If not, redo the question. But this code checks the answer before display the question.

New contributor

Tăng Hoàng Xuân Hạnh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật