У меня есть следующий код. Я хочу изменить CheckVar и сделать это в одном текстовом поле, только строка будет изменена с I.
If TextBox3.Lines.contains(i)(word) Then
Else
TextBox3.Lines.contains(i)= tempTextBox.Text + " " + TxtbValAfterCompar.Text()
Я хочу изменить внешний вид кода с помощью textbox3.lines.
Вот как показывает следующий код.
For Each word In words
For i As Integer = 0 To TextBox2.Lines.Count - 1
TxtbValBeforeCompar.Text = TextBox1.Lines(i)
CompareNumbers()
If TextBox1.Lines(i).Contains(word) Then
found = True
Dim tempTextBox As TextBox = CType(Me.Controls("CheckVar" & i.ToString), TextBox)
On Error Resume Next
If TextBox2.Lines(i).Contains(word) Then
If tempTextBox.Text.Contains(word) Then
Else
tempTextBox.Text = tempTextBox.Text + " " + TxtbValAfterCompar.Text()
End If
Else
End If
End If
Next
Next
Избавьтесь от «При ошибке возобновить далее». Вместо этого исправьте код, если это ожидаемая ошибка, или вставьте «Попробовать ... Поймать ... Завершить попытку», если это неожиданно. Я не вижу здесь ничего неожиданного.
Я не уверен, что вы пытаетесь достичь с помощью своего кода, но я сделал несколько комментариев, которые могут помочь.
Private Sub DoSomething()
Dim words() = {"Mathew", "Mark", "Luke", "John"}
Dim found As Boolean
For Each word In words
'How about checking if the number of lines in TextBox1 is
'equal to or more than the the number of lines in TextBox2.
'If it is less than the code will fail with an index out of range error
If TextBox1.Lines.Count < TextBox2.Lines.Count Then
MessageBox.Show("Sorry TextBox1 does not have enough lines.")
Exit Sub
End If
For i As Integer = 0 To TextBox2.Lines.Count - 1
TxtbValBeforeCompar.Text = TextBox1.Lines(i)
CompareNumbers()
If TextBox1.Lines(i).Contains(word) Then
found = True
'What is the point of tempTextBox? If is never visible and falls
'out of scope at the end of the Sub.
'Check that the number of CheckVars is not less than the number
'of lines in TextBox2
'REMEMBER The first value of i is 0 so you need a CheckVar0 TextBox
Dim counter As Integer
For Each ctrl As Control In Controls
If ctrl.Name.StartsWith("CheckVar") Then
counter += 1
End If
Next
'Of course you
If counter < TextBox2.Lines.Count Then
MessageBox.Show("Sorry, not enough CheckVarX controls.")
Exit Sub
End If
'What is the point of tempTextBox? If is never visible and falls
'out of scope at the end of the Sub.
Dim tempTextBox As TextBox = CType(Me.Controls("CheckVar" & i.ToString), TextBox)
'You could include the nested If in the outer If
'with AndAlso
If TextBox2.Lines(i).Contains(word) Then
'Use the Not keyword an get rid of the empty If
If Not tempTextBox.Text.Contains(word) Then
tempTextBox.Text = tempTextBox.Text + " " + TxtbValAfterCompar.Text()
End If
End If
End If
Next
Next
End Sub
Private Sub CompareNumbers()
'unknown code
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
DoSomething()
End Sub
Во-первых, этот первый фрагмент кода на самом деле не имеет смысла. Попробуйте объяснить, чего вы на самом деле пытаетесь достичь, вместо того, чтобы надеяться, что мы справимся с этим из кода, который этого не делает. Во-вторых, свойство
Lines
вTextBox
создает новый массивString
каждый раз, когда вы обращаетесь к нему, поэтому вам не следует использовать это свойство снова и снова в таком виде. Вы должны использовать свойство только один раз и присвоить результат переменной, а затем использовать эту переменную снова и снова. Если вы внесете какие-либо изменения в массив, вы можете вернуть его свойствуLines
, когда закончите.