2017-04-09 26 views
0

我怎么能回到以前观察到的字符串

String context = string.Emty; 

private void BtnForward_Click(object sender, RoutedEventArgs e) 
{ 
    //First Click 
    context = Abcd; 
    TextboxText.Text = context; //Abcd 
    //Second Click 
    TextboxText.Text = context; //new context = SADASD 
    //Third Forth Fift click new string context 
} 

//Now how i can go back 5th string 4th string 3 2 1th string 
private void BtnBack_Click(object sender, RoutedEventArgs e) 
{ 
    //Fift Forth Third Second First Observed string will show in the textbox 
    // Reverse back previous string with sequence 
    TextBoxText.Text = context; // Will reverse/Go back in sequence 
} 

我怎么能回去串

即使前进前进字符串或和反向回来。我的英语不够好解释,但如果你不明白我说的话,请让我知道

+0

阅读关于堆栈数据结构。 –

回答

0

你需要保留之前值的参考。一种方法是在每次点击BtnForward_Click()和弹出(最近的一次)时,保存一堆字符串,以便在您点击新值时触击BtnBack_Click()。下面给出示例:

Stack context = new Stack(); 

    private void BtnForward_Click(object sender, RoutedEventArgs e) 
    { 
     // Here you would need to set the value of the Abcd based on your business logic 
     context.Push(Abcd); 
    } 

    private void BtnBack_Click(object sender, RoutedEventArgs e) 
    { 
     try 
     { 
      TextBoxText.Text = context.Peek(); // gets the most recently entered value of the stack that has not been removed (popped) 
      context.Pop();     
     } 
     catch (InvalidOperationException exc) 
     { 
      // Nothing to go back to 
     } 
    } 
+0

感谢它工作回去的字符串。但只有一回。假设string [] context.push(“Abcd”),第二个context.push(“Efgh”),第三个context.push(“Ijkl”),第四个context.push(“Mnop”)。结果返回context.pop; context.peek =我得到第四个到第三个context.peek =>(“Ijkl”),然后点击第三个到第二个不去,它保持堆栈在第4到第3个。怎么会是 –

+0

如果我理解正确,问题是你总是得到以前的字符串。我更新了我的答案,首先获取'context.Peek()',然后弹出该元素。希望能帮助到你。 – granit

+0

假设在context.count = 4。这意味着我得到1 2 3 4在堆栈上下文列表中的字符串组。如果我推动(“4”),当我扭转回来我得到流行和窥视3,如果我推(“3”)我得到流行和窥视2。有没有一种方法=>顺序返回字符串peek =>像这个组倒转[4]下一个点击组[3]下一个点击组[2]下一个点击组[1] ..其实我的意思是equability导航返回_request –