0

I googled a few things before posting, but I couldn't find anything like this. Basically, I want to take text from a textbox, save as a variable (say history1) to then be able to call that in the future to display the text. I can do that, but what I'm stuck with is that I want 3 variables (history1, history2 and history3, for example) and each time the button is pressed the string is moved to the next variable.

For example, the button is pressed, the text is saved as variable history1. The text is changed and the button is pressed again, the text from history1 is moved to variable history2, and the new text is saved as history1. This would only need to work for 3 instances though, not infinitely, so when text is stored in history3 and the button is pressed the text is just overwritten.

The way I had thought of approaching this was:

string history1;
string history2;
string history3;

        for (int i = 1; i < 4; i++)
        {
            history1 = txtOutput.Text;
            btnToFile_Click()
            {
                history2=history1;
                btnToFile_Click()
                {
                  history3=history2;
                }

            }
        }

However, this isn't going to work because the btnToFile_Click doesn't take any arguements. Is there an easier way to go about this or just a way to fix the method not taking arguements ?

Thanks in advance!

0

4 Answers 4

6

Make sure that you delcare history1, history2, and history3 on the form level (not inside any method).

Then, have the following code inside the handler of the click event of the button:

history3 = history2;
history2 = history1;
history1 = txtOutput.Text;
Sign up to request clarification or add additional context in comments.

1 Comment

That's ridiculously simple, knew I was over-complicating it. Thanks!
4

You don't need to call the btnToFile_Click() method multiple times in your loop, just move the text from end textbox to another in reverse order. Nor do you need a loop because you only have three textboxes.

Why reverse order? So you move the value to the next textbox before it is overwritten by the new value.

So:

history3 = history2;
history2 = history1;
history1 = txtOutput.Text;

1 Comment

Thanks for explaining why it's done in reverse, that makes sense!
1

btnToFile_Click() is a Click event handler for btnToFile (a button). You're not supposed to call that method yourself, it's called by the UI framework (say WPF or WinForms etc.). By the way, it does receive a parameter, then event source (since you can assign the same event handler to multiple buttons and do something based on which one sent the event)

Comments

0

You can try saving in a string array and move the strings within it when you call the button clicked event

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.