简体   繁体   English

每次按下C#循环增量

[英]C# Increment for loop every time button is pressed

I googled a few things before posting, but I couldn't find anything like this. 发布前,我在Google上搜索了一些内容,但找不到类似的内容。 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. 基本上,我想从文本框中获取文本,另存为变量(例如history1),以便将来可以调用它以显示文本。 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. 我可以做到,但是我坚持的是我想要3个变量(例如,history1,history2和history3),并且每次按下按钮时,字符串都将移至下一个变量。

For example, the button is pressed, the text is saved as variable history1. 例如,按下按钮,文本将保存为变量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. 更改文本并再次按下按钮,将history1中的文本移至变量history2,并将新文本另存为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. 不过,这只需要在3个实例中使用,而不是无限地工作,因此当文本存储在history3中并且按下按钮时,文本将被覆盖。

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. 但是,这不会起作用,因为btnToFile_Click不接受任何争论。 Is there an easier way to go about this or just a way to fix the method not taking arguements ? 有没有更简单的方法可以解决此问题,或者只是一种解决方法而不需要争论?

Thanks in advance! 提前致谢!

Make sure that you delcare history1 , history2 , and history3 on the form level (not inside any method). 确保在表单级别(不在任何方法内)delcare history1history2history3

Then, have the following code inside the handler of the click event of the button: 然后,在按钮的click事件的处理程序中包含以下代码:

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

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. 您无需在循环中多次调用btnToFile_Click()方法,只需以相反的顺序将文本从结束文本框移至另一个文本框即可。 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;

您可以尝试保存在字符串数组中并在调用按钮单击事件时在其中移动字符串

btnToFile_Click() is a Click event handler for btnToFile (a button). btnToFile_Click()是btnToFile的Click事件处理程序(一个按钮)。 You're not supposed to call that method yourself, it's called by the UI framework (say WPF or WinForms etc.). 您不应该自己调用该方法,而是由UI框架(例如WPF或WinForms等)调用。 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) 顺便说一句,它确实会接收一个参数,然后接收事件源(因为您可以将相同的事件处理程序分配给多个按钮,并根据发送事件的方式进行操作)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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