简体   繁体   中英

Change and display text in the text box with same button

private void btnNext_Click(object sender, EventArgs e)
        {
            int i = 0;
            nameTxtBox.Text = employee[i].name;
            addTxtBox.Text = employee[i].address;
            payTxtBox.Text = ($"{employee[i].CalcSalary():c}");
            i++;

        }

What I'm trying to do is display the appropriate values for each object in the array of objects employee each time I click the Next button. How can I do this?

Your code is almost right, but you defined you i variable in the wrong scope. It needs to be a field on the class that way its previous value is maintained between each button click.

private int i = 0;

private void btnNext_Click(object sender, EventArgs e)
{
        nameTxtBox.Text = employee[i].name;
        addTxtBox.Text = employee[i].address;
        payTxtBox.Text = ($"{employee[i].CalcSalary():c}");
        i++;
        // Add logic to make sure 'i' does not go higher than 
        // the total number of items in the array or IndexOutOfBoundException occurs.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM