在 VB.Net 中创建 undo/redo 文本框算法
Create a undo/redo textboxes algorithm in VB.Net
我很想在 VB.Net 中创建一个类型算法 Undo/Redo,首先至少我想撤消。
如果我们有例子Textbox1.Text = a,b,c
然后改成=a,b,c,e
然后改为=a,b,c,f
当我点击undo
时我要显示(也就是之前的值。之前a,b, c, f
之后,是a, b, c,e
a,b,c,e
当我再次点击时我想显示
a,b,c
即undo / redo algorithm
的附加值
我首先创建了一个全局列表。
Public Module Globals
Public Undo As New List(Of String)
End Module
如果列表中不存在文本框值,那么我会添加它。
Private Sub Button1(sender As Object, e As EventArgs) Handles Button1.Click
If Undo IsNot TextBox1.Text Then
Undo.Add(TextBox1.Text)
End If
End Sub
嘿,这是棘手的部分
Private Sub Undo_Click(sender As Object, e As EventArgs) Handles Undo.Click
Dim i as integer = Undo.Count
While TextBox1.Text IsNot Undo(i)
TextBox1.Text = Undo(i)
End While
End Sub
For i As Integer = Undo.Count To 0 Step -1
TextBox1.Text = Undo(i)
Next
不幸的是,在这种情况下没有这样的循环。
试试这个:
Private Undos As List(Of String) = New List(Of String)()
Private AddUndo As Boolean = True
Private Sub textBox1_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
If AddUndo = True Then
Undos.Add(textBox1.Text)
End If
End Sub
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
If Undos.Count <> 0 Then
AddUndo = False
If Undos.Count > 1 Then
textBox1.Text = Undos(Undos.Count - 2)
Undos.RemoveAt(Undos.Count - 1)
Else
textBox1.Text = ""
Undos.RemoveAt(0)
End If
AddUndo = True
End If
End Sub
我很想在 VB.Net 中创建一个类型算法 Undo/Redo,首先至少我想撤消。
如果我们有例子Textbox1.Text = a,b,c
然后改成=a,b,c,e
然后改为=a,b,c,f
当我点击undo
时我要显示(也就是之前的值。之前a,b, c, f
之后,是a, b, c,e
a,b,c,e
当我再次点击时我想显示
a,b,c
即undo / redo algorithm
的附加值
我首先创建了一个全局列表。
Public Module Globals
Public Undo As New List(Of String)
End Module
如果列表中不存在文本框值,那么我会添加它。
Private Sub Button1(sender As Object, e As EventArgs) Handles Button1.Click
If Undo IsNot TextBox1.Text Then
Undo.Add(TextBox1.Text)
End If
End Sub
嘿,这是棘手的部分
Private Sub Undo_Click(sender As Object, e As EventArgs) Handles Undo.Click
Dim i as integer = Undo.Count
While TextBox1.Text IsNot Undo(i)
TextBox1.Text = Undo(i)
End While
End Sub
For i As Integer = Undo.Count To 0 Step -1
TextBox1.Text = Undo(i)
Next
不幸的是,在这种情况下没有这样的循环。
试试这个:
Private Undos As List(Of String) = New List(Of String)()
Private AddUndo As Boolean = True
Private Sub textBox1_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
If AddUndo = True Then
Undos.Add(textBox1.Text)
End If
End Sub
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
If Undos.Count <> 0 Then
AddUndo = False
If Undos.Count > 1 Then
textBox1.Text = Undos(Undos.Count - 2)
Undos.RemoveAt(Undos.Count - 1)
Else
textBox1.Text = ""
Undos.RemoveAt(0)
End If
AddUndo = True
End If
End Sub