我正在尝试编写一个按钮,单击该按钮会删除多行文本框中的最后一行文本。如果文本框中有两行文本,它可以删除文本的最后一行,但如果文本框中只有一行,则会导致错误。
EG。 将其添加到框中:
line of text 1
line of text 2
使用删除按钮两次,我可以删除两行文本
但如果我这样做: 将此行添加到文本框中:
line of text 1
然后尝试删除它,它出错。
我知道错误,因为我使用了两次
' remove box text
QuoteHistoryBox.Text = QuoteHistoryBox.Text.Remove(QuoteHistoryBox.Text.LastIndexOf(Environment.NewLine))
QuoteHistoryBox.Text = QuoteHistoryBox.Text.Remove(QuoteHistoryBox.Text.LastIndexOf(Environment.NewLine))
但是由于程序的工作方式,这段代码是必须保留的,并且想知道是否有解决方法。
这是删除最后一个按钮代码:
Private Sub Button12_Click(sender As Object, e As EventArgs) Handles RemoveLastBtn.Click
' do absolutelty nothing if the box is already empty
If QuoteHistoryBox.Lines.Count = 1 Or QuoteHistoryBox.Text = "" Then
QuoteHistoryBox.Text = ""
FinalQuoteBox.Text = 0
removeOne = 0
' else, remove the last line of text, which would be the last item
ElseIf removeOne = 0 Then
' create a place to store the last line in the texbox
Dim lastBoxLine As String
'get the last line in the textbox
lastBoxLine = QuoteHistoryBox.Lines(QuoteHistoryBox.Lines.Length - 1)
'if the last line is empt, get the second line instead
If (lastBoxLine = String.Empty Or lastBoxLine = "") Then
lastBoxLine = QuoteHistoryBox.Lines(QuoteHistoryBox.Lines.Length - 2)
End If
' compare the gotten line with the number of strings
' if the last line matches this string, delete it, and then minus the number associated with this string, or plus if it is the contract string
If lastBoxLine.Equals("+ Base 3D Printer: $799") Then
lastNum = 799
FinalQuoteBox.Text = Val(FinalQuoteBox.Text) - lastNum
ElseIf lastBoxLine.Equals("+ High-end Printer: $3999") Then
lastNum = 3999
FinalQuoteBox.Text = Val(FinalQuoteBox.Text) - lastNum
ElseIf lastBoxLine.Equals("+ Basic small print: $30") Then
lastNum = 30
FinalQuoteBox.Text = Val(FinalQuoteBox.Text) - lastNum
ElseIf lastBoxLine.Equals("+ Basic medium print: $50") Then
lastNum = 50
FinalQuoteBox.Text = Val(FinalQuoteBox.Text) - lastNum
ElseIf lastBoxLine.Equals("+ Basic large print: $110") Then
lastNum = 110
FinalQuoteBox.Text = Val(FinalQuoteBox.Text) - lastNum
ElseIf lastBoxLine.Equals("+ High quality fdm print: %110") Then
Dim currentVal As Double = Val(FinalQuoteBox.Text)
lastNum = currentVal / 210
lastNum = lastNum * 100
FinalQuoteBox.Text = lastNum
ElseIf lastBoxLine.Equals("+ Fdm print using ABS: %20") Then
Dim currentVal As Double = Val(FinalQuoteBox.Text)
lastNum = currentVal / 120
lastNum = lastNum * 100
FinalQuoteBox.Text = lastNum
ElseIf lastBoxLine.Equals("+ High quality SLA print: %210") Then
Dim currentVal As Double = Val(FinalQuoteBox.Text)
lastNum = currentVal / 310
lastNum = lastNum * 100
FinalQuoteBox.Text = lastNum
ElseIf lastBoxLine.Equals("+ Contract: - %15") Then
Dim currentVal As Double = Val(FinalQuoteBox.Text)
lastNum = currentVal / 85
lastNum = lastNum * 100
FinalQuoteBox.Text = lastNum
ElseIf lastBoxLine.Equals("+ Prioritise request: $20") Then
lastNum = 20
FinalQuoteBox.Text = Val(FinalQuoteBox.Text) - lastNum
End If
' remove box text
QuoteHistoryBox.Text = QuoteHistoryBox.Text.Remove(QuoteHistoryBox.Text.LastIndexOf(Environment.NewLine))
QuoteHistoryBox.Text = QuoteHistoryBox.Text.Remove(QuoteHistoryBox.Text.LastIndexOf(Environment.NewLine))
removeOne = removeOne + 1
Else
' create a place to store the last line in the texbox
Dim lastBoxLine As String
'get the last line in the textbox
lastBoxLine = QuoteHistoryBox.Lines(QuoteHistoryBox.Lines.Length - 1)
'if the last line is empt, get the second line instead
If (lastBoxLine = String.Empty Or lastBoxLine = "") Then
lastBoxLine = QuoteHistoryBox.Lines(QuoteHistoryBox.Lines.Length - 2)
End If
' compare the gotten line with the number of strings
' if the last line matches this string, delete it, and then minus the number associated with this string, or plus if it is the contract string
If lastBoxLine.Equals("+ Base 3D Printer: $799") Then
lastNum = 799
FinalQuoteBox.Text = Val(FinalQuoteBox.Text) - lastNum
ElseIf lastBoxLine.Equals("+ High-end Printer: $3999") Then
lastNum = 3999
FinalQuoteBox.Text = Val(FinalQuoteBox.Text) - lastNum
ElseIf lastBoxLine.Equals("+ Basic small print: $30") Then
lastNum = 30
FinalQuoteBox.Text = Val(FinalQuoteBox.Text) - lastNum
ElseIf lastBoxLine.Equals("+ Basic medium print: $50") Then
lastNum = 50
FinalQuoteBox.Text = Val(FinalQuoteBox.Text) - lastNum
ElseIf lastBoxLine.Equals("+ Basic large print: $110") Then
lastNum = 110
FinalQuoteBox.Text = Val(FinalQuoteBox.Text) - lastNum
FinalQuoteBox.Text = Val(FinalQuoteBox.Text) - lastNum
ElseIf lastBoxLine.Equals("+ High quality fdm print: %110") Then
' correctly gets the old money value before the increase
Dim currentVal As Double = Val(FinalQuoteBox.Text)
lastNum = currentVal / 210
lastNum = lastNum * 100
FinalQuoteBox.Text = lastNum
ElseIf lastBoxLine.Equals("+ Fdm print using ABS: %20") Then
Dim currentVal As Double = Val(FinalQuoteBox.Text)
lastNum = currentVal / 120
lastNum = lastNum * 100
FinalQuoteBox.Text = lastNum
ElseIf lastBoxLine.Equals("+ High quality SLA print: %210") Then
Dim currentVal As Double = Val(FinalQuoteBox.Text)
lastNum = currentVal / 310
lastNum = lastNum * 100
FinalQuoteBox.Text = lastNum
ElseIf lastBoxLine.Equals("+ Contract: - %15") Then
Dim currentVal As Double = Val(FinalQuoteBox.Text)
lastNum = currentVal / 85
lastNum = lastNum * 100
FinalQuoteBox.Text = lastNum
FinalQuoteBox.Text = Val(FinalQuoteBox.Text) + lastNum
ElseIf lastBoxLine.Equals("+ Prioritise request: $20") Then
lastNum = 20
FinalQuoteBox.Text = Val(FinalQuoteBox.Text) - lastNum
End If
QuoteHistoryBox.Text = QuoteHistoryBox.Text.Remove(QuoteHistoryBox.Text.LastIndexOf(Environment.NewLine))
removeOne = removeOne + 1
End If
End Sub
感谢任何帮助。
答案 0 :(得分:0)
没关系,我找到了答案。我只是将它放在try catch块中并添加:
Catch ex As ArgumentOutOfRangeException
QuoteHistoryBox.Text = ""
FinalQuoteBox.Text = 0
removeOne = 0
End Try
捕获行只有一个项目时,如果它在catch块中则执行特定操作。问题解决了。