Making text go to a new line when in a while loop - vb.net

I have a richTextbox, and a while loop, x = 0, and every time this loop runs, x += 1 till it reaches a certain value.
Heres what I want to happen:
while x <> 10 then
it will say item 0 +1 on a new line, and then item 1 + 1 on the line under it, etc, so you will see all 10 values after.
What happens is, it will change that line into the new value.
My question is: How do I make it put the words on a new line instead?
Here is my code:
While readListItem <> PaymentList.Items.Count
If readListItem > PaymentList.Items.Count Then
readListItem = 0
Exit While
End If
readListItem += 1
MessageBox.Show(readListItem)
resultBox.Text = vbNewLine + PaymentList.Items.Item(readListItem) + " costs " + enteredCost + "." + vbNewLine
End While
readListItem is "x", and that is being iterated by 1 every time the loop runs
PaymentList is my listbox containing an unknown value (the user sets the number of items in this list)
The If if there because, after x = lets say 10, it would add another to x (so now it = 11) and try to print out the 11th item, as it doesnt exist, it crashes. This is to attempt to prevent that. I didnt want to go with a try catch

Try adding the new value instead of replacing the entire value:
resultBox.Text &= Environment.NewLine() & PaymentList.Items.Item(readListItem) + " costs " + enteredCost + "."
The expression &= is equivalent, in this case, to resultBox.Text = resultBox.Text & "...".
And once advice, you can simplify your loop by using this condition:
While readListItem < PaymentList.Items.Count
'...
End While

Related

How to output number of occurrences of a positive negative number and Zero in Console Application?

I have a doubt on how to do this:
The user has to input 10 numbers, and the program has to output how many numbers are Positive, how many numbers are Negative, and how many are Zero, out of those 10 numbers. I remember getting it right once, but forgot the logic and so the code too. I tried this, but it doesn't seem to work. Can anyone hlep me out by giving me the solution? Thanks!
Code:
Dim counter, pluscount, minuscount, zerocount As Decimal
For counter = 1 To 10
Console.WriteLine("Enter 10 numbers")
counter = Console.ReadLine
If counter > 0 Then
pluscount = pluscount + 1
ElseIf counter < 0 Then
minuscount = minuscount + 1
Else
zerocount = zerocount + 1
End If
counter = counter + 1
Console.WriteLine(pluscount & " number/s is/are positive.")
Console.WriteLine(minuscount & " number/s is/are negative.")
Console.WriteLine(zerocount & " number/s is/are zero")
Next
This code simply implements the solution submitted by #jmcilhinney with the addition of a ReadLine and a CDec. The correct answer is #jmcilhinney's
Module Module1
Sub Main()
'Add a variable to hold user input
Dim counter, input, pluscount, minuscount, zerocount As Decimal
For counter = 1 To 10
Console.WriteLine("Enter a number")
'do not change the value of counter in the loop
'use a different variable to hold user input
'Turn on Option Strict
input = CDec(Console.ReadLine)
If input > 0 Then
pluscount = pluscount + 1
ElseIf input < 0 Then
minuscount = minuscount + 1
Else
zerocount = zerocount + 1
End If
'Do not increment the counter
'Next increments the counter automaticly
'counter = counter + 1
Next
Console.WriteLine(pluscount & " number/s is/are positive.")
Console.WriteLine(minuscount & " number/s is/are negative.")
Console.WriteLine(zerocount & " number/s is/are zero")
'Stop the program from closing with a .ReadLine so you can see your results
Console.ReadLine()
End Sub
End Module
The issue is how you're using counter. It is the loop counter and thus should be nothing else. Don't assign anything to it in the loop and don't increment it manually. When you call ReadLine, you need some other variable to assign the result to.
Also, there's no array there. Your loop counter should go from 0 to 9 and that should be the index into the array when you set an element.
Finally, don't tell the user to enter 10 numbers inside the loop. If you do that, you'll be teeming then to enter 10 numbers 10 times.
You Need To Findout the Count Right...Then You Have to Mention that Console.Writeline after Completing the For Loop Like This..
Dim counter, pluscount, minuscount, zerocount As Decimal
For counter = 1 To 10
Console.WriteLine("Enter 10 numbers")
counter = Console.ReadLine
If counter > 0 Then
pluscount = pluscount + 1
ElseIf counter < 0 Then
minuscount = minuscount + 1
Else
zerocount = zerocount + 1
End If
counter = counter + 1
Next
Console.WriteLine(pluscount & " number/s is/are positive.")
Console.WriteLine(minuscount & " number/s is/are negative.")
Console.WriteLine(zerocount & " number/s is/are zero")
First you need to take user input in an array. This array will have 10 elements which will be input by the user.
After the user has finished the input, you loop over the array and find which of the numbers entered are positive, negative or zero.
So in short, you need one array with 10 elements, 1 counter for the loop and 3 variables to hold the number of positive, negative or zero numbers entered.

Skipping code using GoTo (Recursion)

I have problem skipping some specific part of the code while execution.
Dim turn as Integer = 1
Function recurs1()
If turn = 9 Then
GoTo Endline
End If
For i = 0 To 5
For j = 0 To 5
If (arr(i, j) <> 10 And arr(i, j) <> 20) Then
If chance Mod 2 = 1 Then
MsgBox("Intialized (" & i & "," & j & ") To 10") 'Line X
arr(i, j) = 10
ElseIf chance Mod 2 = 0 Then
MsgBox("Intialized (" & i & "," & j & ") To 20") 'Line Y
arr(i, j) = 20
End If
turn += 1 'Updating turn
recurs1() 'Recursion takes place here
End If
Next
Next
Endline:
Return Nothing
End Function
According to my understanding, the code should stop assigning values to the array after value of turn hits 9 and should return flow to the calling function/sub. However, it continues to assign values and Line X or Line Y are printed depending on the value of turn.
If allowed, the value of turn increments up to 37.
I'd surely appreciate some help with this problem from someone with a deeper understanding of the code than me.
Thank you very much.
You have placed the recursive call inside two for loops. The function will be called recursively until turn = 9, but when it returns it's still in the loop where it will be increased anyway without further checks.
Probably it would work better if you checked the value of turn just before calling the recursive function (this way you get rid of GoTo, also).
Why not just put Return Nothing where you have Goto Endline? Return stops function execution, to the best of my knowledge. That would get rid of the dreaded Goto command (it can cause messy code and inscrutable bugs).

VB - Index outside of bounds?

Oh my god i hate this thing, i tried millions of ways but couldn't find a working one. Let me explain:
I'm testing each line and checking the first word to be "copy" alright ? After the word copy i want to see if the next word is "1" , the third is "<" and the last is ">" , if all these conditions are fullfilled then the text between "<" and ">" needs to be stored in the variable "copy1" (even if there is more than 1 word between them).
What my code is:
For i = 0 To lstCode.Items.Count - 1
Dim str As String = lstCode.Items.Item(i)
Dim strA() As String = Split(str)
Dim copy1 as string
Dim copy2 as string
Select Case strA(0)
Case copy
If strA(1) = "1" And strA(2) = "<" And strA(strA.Count - 1) = ">" Then
copy1 = ""
For lr As Integer = 3 To strA.Count - 2
copy1 &= (strA(lr) & " ")
Next
End if
End select
And, when i debug it i get the error: Index was outside the bounds of the array ... Does anybody have any idea ?
There is something important i forgot to add, this is the whole code:
Case "copy"
If strA(1) = "1" And strA(2) = "<" And strA(strA.Count - 1) = ">" Then
copy1 = ""
For lr As Integer = 3 To strA.Count - 2
copy1 &= (strA(lr) & " ")
Next
ElseIf strA(1) = "2" And strA(2) = "<" And strA(strA.Count - 1) = ">" Then
copy2 = ""
For lrs As Integer = 3 To strA.Count - 2
copy2 &= (strA(lrs) & " ")
Next
ElseIf strA(1) = "run" Then
Try
IO.File.Copy(copy1, copy2)
Catch ex As IO.IOException
End Try
End If
End Select
So everything works like a charm: copy 1 < c:\csb.log > , copy 2 < c:\blabla.txt > but when the " copy run " statement comes in it gives me the error...
You need to change the operator And with AndAlso.
The second one applies Short Circuit Evaluation to your expression, meaning if the first expression is false the second, third and so on expressions on the same line are not evaluated.
In your line
If strA(1) = "1" And strA(2) = "<" And .......
when the value is "Run" you still evaluate the expression strA(2) = "<" but there is no element at index 2 so you get the error.

Check if each line from a text file contains a certain string, and add the ones that do to a listbox?

The following code works, to simply get each line as a line in the listbox.
Reader = IO.File.OpenText(textlocation)
Dim bookmarks() As String = Reader.ReadToEnd.Split(vbNewLine)
Dim i As Integer = 0
Do Until i = bookmarks.Length
lstFavorites.Items.Add(bookmarks(i))
i += 1
Loop
But I don't want every line to go into the text box. I only want the lines that contain the text "Bookmark" to go into the listbox. What can I do to achieve this? I've tried everything I can think of.
Heres some code I tried, I can't see the problem in it, but it seems to just crash my program.
Do Until i = bookmarks.Length
If bookmarks(i).Contains("at") Then
If radBookmarks.Checked Then
If bookmarks(i).Contains("Bookmark") Then
Original = bookmarks(i)
BeginningOfDemoName = Original.Substring(Original.LastIndexOf("(") + 2)
TickWithParenthesis = BeginningOfDemoName.Substring(BeginningOfDemoName.IndexOf(Chr(34)) + 4)
Tick = TickWithParenthesis.Split(" ")(1).Split(")")(0)
DemoName = BeginningOfDemoName.Split(Chr(34))(0)
ToList = DemoName + " at " + Tick
lstFavorites.Items.Add(ToList)
i += 1
Else
i += 1
End If
ElseIf radEverything.Checked Then
Original = bookmarks(i)
BeginningOfDemoName = Original.Substring(Original.LastIndexOf("(") + 2)
TickWithParenthesis = BeginningOfDemoName.Substring(BeginningOfDemoName.IndexOf(Chr(34)) + 4)
Tick = TickWithParenthesis.Split(" ")(1).Split(")")(0)
DemoName = BeginningOfDemoName.Split(Chr(34))(0)
ToList = DemoName + " at " + Tick
lstFavorites.Items.Add(ToList)
i += 1
End If
End If
Loop
Try to change this line
If bookmarks(i).Contains("Bookmark") Then
to
If bookmarks(i).IndexOf("Bookmark",
StringComparison.CurrentCultureIgnoreCase) >= 0 Then
Contains do a case sensitive comparison and your input string contains a lower case 'bookmark'

Way to Jump to Next i in For..Next Loop?

I'm reverse engineering in QuickBasic and I have code like this:
FOR i = star TO fin
IF a < 1 THEN
CALL clrbot
COLOR 15
PRINT force$(side); " army in "; city$(armyloc(i)); " is CUT OFF !";
TICK turbo!
GOTO alone
END IF
size = size + 1
max = 11: IF LEN(armyname$(i)) < 11 THEN max = LEN(armyname$(i))
mtx$(size) = LEFT$(armyname$(i), max)
array(size) = i
alone:
NEXT i
I'd like to get rid of the line label (alone) and instead do something like:
IF a < 1 THEN
CALL clrbot
COLOR 15
PRINT force$(side); " army in "; city$(armyloc(i)); " is CUT OFF !";
TICK turbo!
NEXT i
END IF
You could replace the GOTO with an Else:
For i = star To Fin
If a < 1 Then
' Do something
Else
' Do Something else
End If
Next
This would follow the same logic - the Else takes the place of the GOTO alone statement.
In the original code (QuickBASIC) if the If block is entered, everything after then GOTO alone statement is ignored.
If the If block is not entered (i.e., a >= 1) then everything after the If block is executed.
The Else statement in the VB.NET code will produce the same behavior. If a < 1, the first block will be executed and the Else block will be ignored, and the loop will advance to the next increment of i.
If a >= 1, then the Else block will be executed and the loop will then advance to the next increment of i.
The above assumes labels in QuickBASIC are like labels in DOS batch files.