简体   繁体   中英

Change text of button multiple times c#

I am trying to do this:

The GUI uses only one textbox to accept the three integers. Create two controls: A textbox and a button. The button s hould appear next to the textbox and is used to input the integer value entered in the textbox. The button indicates the integer to be entered. For example, when entering the first integer, the button (Content) displays “Input first integer.” After successfully entering the first integer, the Content changes to “Input second integer.” After entering the second integer, the Content changes to “Input third integer.” After the third integer has been entered , the bu tton should become disabled and the button should display “Input first integer” again.

This is what I have managed so far


private void button1_Click(object sender, EventArgs e) {

        if (button1.Text == "Input First Integer")
        {
            button1.Text = "Input Second Integer";
        }
        else
        {
            button1.Text = "Input Third Integer";

        }
        if (button1.Text == "Input Third Integer");
        {
            button1.Text = "Input First Integer";
            button1.Enabled = false;

Leaving aside the parts with the integers, if-else is the closest I have gotten to getting this to work. I cant seem to phrase it so that the if does not interfere with its other parts. The above example disables it after I click the button the first time and if I remove the bit with enabling it just leaves me at "Input First Integer"


My question for you: Am I going about this the wrong way? Should I be using something other than if/else for this?

I have been trying to find something similar to this to see what I am doing wrong(button that cycles through multiple textbox entrys + doing stuff via the click event) and have found nothing. Is this a odd thing with few occurrences or am I just googling the wrong phrases?

Presumably when the user enters a number, you are putting it somewhere. How are you deciding where to put it? Preferably you have an array, which of course you're using an index into to determine where the new number goes. But even if you are putting the numbers into individually-named variables, an index is still best for keeping track of where in the input sequence you are.

Then you just need something like this:

private static readonly string[] _buttonText =
{
    "Input First Integer",
    "Input Second Integer",
    "Input Third Integer",
};

private int _inputState;

private void button1_Click(object sender, EventArgs e)
{
    // Process current input here.

    // Then update the button text for the next input number
    // (wraps around to the first button text after the last
    // button text was used)
    _inputState = (_inputState + 1) % _buttonText.Length;
    button1.Text = _buttonText[_inputState];
}

Note that the _inputState index can also be used to index an array where you store the values, or as the value for a switch statement that determines which named variable takes the currently-entered value.

If you want to get fancy, put the string[] into a resource.

You should use if-else if-else(else only if needed)

    if (button1.Text == "Input First Integer")
    {
        button1.Text = "Input Second Integer";
    }
    else if (button1.Text == "Input Second Integer")
    {
        button1.Text = "Input Third Integer";

    }
    else if (button1.Text == "Input Third Integer")
    {
        button1.Text = "Input First Integer";
        button1.Enabled = false;

    }

The above will only run one at a time. The problem with your original code is that it went from second to third then the next if ran and it returned to one.

It is not a good idea to compare strings (and copy-paste strings in the code), use an int variable to store the step number instead:

private const int TotalSteps = 3;
private int step;

private void nextButton1Text()
{
    switch (step % TotalSteps)
    {
        case 0:
            button1.Text = "Input First Integer";
            break;
        case 1:
            button1.Text = "Input Second Integer";
            break;
        case 2:
            button1.Text = "Input Third Integer";
            break;
    }
    button1.Enabled = step >= TotalSteps;
    step++;
}

private void button1_Click(object sender, EventArgs e)
{
    nextButton1Text();
}

I would store the strings as constants, although that's not necessary if you aren't going to compare them directly or use them in more than one place. And I would keep track of state using an integer rather than string, because string comparison is prone to failure (typos, casing, etc). And finally, I would use some kind of simple validation for the int, like int.Parse() to see if the entry was valid before moving on.

Example:

private const string InputFirst = "Input first integer";
private const string InputSecond = "Input second integer";
private const string InputThird = "Input third integer";
private int integersEntered;

private void Form1_Load(object sender, EventArgs e)
{
    button1.Text = InputFirst;
}

private void button1_Click(object sender, EventArgs e)
{
    if (ValidateInput(textBox1.Text))
    {
        textBox1.Clear();

        switch (integersEntered)
        {
            case 1:
                button1.Text = InputSecond;
                break;
            case 2:
                button1.Text = InputThird;
                break;
            case 3:
                button1.Text = InputFirst;
                button1.Enabled = false;
                break;
        }
    }
    else
    {                
        MessageBox.Show("You must enter a valid integer.", "Text Validation");
        textBox1.Focus();
        textBox1.SelectAll();
    }
}

private bool ValidateInput(string input)
{
    int value;
    var success = int.TryParse(input, out value);
    if(success) { integersEntered++; }
    return success;
}

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