For loop not working right unless i add Msgbox inside of loop - vb.net

Hello there everyone.
I have little problem, didn't make sense at all. So i have kinda simple for loop. I want to create random integers and remove index of specific array by that integer.
Working perfect:
For i = 1 To CInt(rastgelesoru.Text)
Dim Rand As New Random()
Dim xIndex As Integer = Rand.Next(0, AList.Count - 1)
Dim SelectedValue = AList(xIndex)
Dim eklepanelrnd As Panel = CType(containerpanel.Controls(SelectedValue), Panel)
If eklepanelrnd.Tag = "1" Then
MsgBox(xIndex)
containerpanelrastgele.Controls.Add(eklepanelrnd)
End If
AList.RemoveAt(xIndex)
Next
For example i have 500 element in array. When i add message box like above, it works perfect. I get random numbers. (100,65,355,27,472 last output for 5). But when i remove msgbox line i get Consecutive numbers everytime. First i thought it might be really 'random' but no. Everytime i get Consecutives. (23,24,25,160,161 last output for 5 without msgbox line.)
Not working properly without msgbox line.
For i = 1 To CInt(rastgelesoru.Text)
Dim Rand As New Random()
Dim xIndex As Integer = Rand.Next(0, AList.Count - 1)
Dim SelectedValue = AList(xIndex)
Dim eklepanelrnd As Panel = CType(containerpanel.Controls(SelectedValue), Panel)
If eklepanelrnd.Tag = "1" Then
containerpanelrastgele.Controls.Add(eklepanelrnd)
End If
AList.RemoveAt(xIndex)
Next

#AlexB. on comments.
DonĀ“t create Random objects in your loop but only create one. So move Dim Rand As New Random() before the loop.
Working perfect now. Thanks <3 Have a wonderful day.

Related

Using visual basic, what do I need to add to my for next loop to make my application display only even numbers?

My question is not the same as the one it is flagged as similar to. I need my application to display the actual numbers that are even in a range of numbers entered by the user. The other question prints "Even" or "Odd" based on a number.
I am working on a homework assignment where I have to make my application take numbers that are input by the user in text boxes and display the even numbers between them in a list box using a For...Next statement.
(So if the user enters 2 in the From box and 10 in the To box it, the application needs to output 2, 4, 6, 8, and 10 without commas in the list box.)
This is my current interface (Showing how the application runs with the current code):
Numbers App Test
This is my current code:
Dim intFrom As Integer
Dim intTo As Integer
Integer.TryParse(txtFrom.Text, intFrom)
Integer.TryParse(txtTo.Text, intTo)
lstNumbers.Items.Clear()
For intList As Integer = intFrom To intTo
If intFrom >= intTo Then
Exit For
End If
lstNumbers.Items.Add(intList)
Next intList
End Sub
Replace your For cycle with this:
For intList As Integer = intFrom To intTo
Dim result As Integer = intList Mod 2
If result = 0 Then
lstNumbers.Items.Add(intList)
End If
Next
Alternatives
lstNumbers.Items.Clear()
'alternative 1
Dim ie As IEnumerable(Of String)
ie = From i In Enumerable.Range(intFrom, intTo - intFrom + 1)
Where (i And 1) = 0 Select CType(i, String)
lstNumbers.Items.AddRange(ie.ToArray)
lstNumbers.Items.Clear()
'alternative 2
For intList As Integer = intFrom To intTo
If (intList And 1) = 0 Then
lstNumbers.Items.Add(intList)
End If
Next

Readline Error While Reading From .txt file in vb.net

I have a Streamreader which is trowing an error after checking every line in Daycounts.txt. It is not a stable txt file. String lines in it are not stable. Count of lines increasing or decresing constantly. Thats why I am using a range 0 to 167. But
Here is the content of Daycounts.txt: Daycounts
Dim HourSum as integer
Private Sub Change()
Dim R As IO.StreamReader
R = New IO.StreamReader("Daycounts.txt")
Dim sum As Integer = 0
For p = 0 To 167
Dim a As String = R.ReadLine
If a.Substring(0, 2) <> "G." Then
sum += a.Substring(a.Length - 2, 2)
Else
End If
Next
HourSum = sum
R.Close()
End Sub
If you don't know how many lines are present in your text file then you could use the method File.ReadAllLines to load all lines in memory and then apply your logic
Dim HourSum As Integer
Private Sub Change()
Dim lines = File.ReadAllLines("Daycounts.txt")
Dim sum As Integer = 0
For Each line In lines
If line.Substring(0, 2) <> "G." Then
sum += Convert.ToInt32(line.Substring(line.Length - 2, 2))
Else
....
End If
Next
HourSum = sum
End Sub
This is somewhat inefficient because you loop over the lines two times (one to read them in, and one to apply your logic) but with a small set of lines this should be not a big problem
However, you could also use File.ReadLines that start the enumeration of your lines without loading them all in memory. According to this question, ReadLines locks writes your file until the end of your read loop, so, perhaps this could be a better option for you only if you don't have something external to your code writing concurrently to the file.
Dim HourSum As Integer
Private Sub Change()
Dim sum As Integer = 0
For Each line In File.ReadLines("Daycounts.txt")
If line.Substring(0, 2) <> "G." Then
sum += Convert.ToInt32(line.Substring(line.Length - 2, 2))
Else
....
End If
Next
HourSum = sum
End Sub
By the way, notice that I have added a conversion to an integer against the loaded line. In your code, the sum operation is applied directly on the string. This could work only if you have Option Strict set to Off for your project. This setting is a very bad practice maintained for VB6 compatibility and should be changed to Option Strict On for new VB.NET projects

VB.NET multiple picturebox randomizer

I want to create a form in which will be displayed a question and the user will have to chose the correct out of four images..
First of all I'm using an ArrayList and fill it with my questions, I'm also creating two Imagelists. I'm adding all the correct "answers" in order (ex the correct answer for the first question ( Arraylist1(0) ) will be the first Image ( Imagelist1 (0) ). The second Imagelist is full of "wrong" answers. Up to this point everything works fine.
I want the Images to appear in random order, so I tried to create a temporary list and fill it with the Correct Image and three wrong in randomly...
But I couldn't find out how to do that... Since "Contains" and "ImagelistA.Images.Add(ImagelistB(3))" are not working as I thought that they would...
PS: Following the same pattern for Arraylists instead of Imagelists worked perfectly... But I can't figure out a way to have the same outcome with Images...
Public Sub reloadForm()
'Filling the ArrayList with questions'
Dim questions As New ArrayList
questions.Add("Question1")
questions.Add("Question2")
questions.Add("Question3")
questions.Add("Question4")
questions.Add("Question5")
questions.Add("Question6")
Dim Randm As New Random
Dim rnd As New Integer
Dim temp As New ImageList
Dim flaG As Boolean
flaG = False
'Picking a random unanswered question from the ArrayList'
'Adds the Correct Image/Answer to a Temporary List'
Do
rnd = Randm.Next(0, (questions.Count))
If Not ListBox1.Items.Contains(rnd) Then
question.Text = questions(rnd)
temp.Images.Add(ImageListC(rnd))
ListBox1.Items.Add(rnd)
flaG = True
End If
Loop Until flaG = True
'Adding three wrong Images/Answers in to Temporary Imagelist'
Do
rnd = Randm.Next(0, (ImageListW.Images.Count))
If Not temp.Contains(ImageListW(rnd)) Then
temp.Images.Add(ImageListW(rnd))
End If
Loop Until temp.Count = 4
'Displays all four Images in a random order'
Dim quizlist As New Imagelist
Do
rnd = Randm.Next(0, 4)
If Not quizlist.Contains(tempList(rnd)) Then
quizlist.Images.Add(tempList(rnd))
End If
Loop Until quizList.Count = 4
PictureBox1.Image = quizlist(0)
PictureBox2.Image = quizlist(1)
PictureBox3.Image = quizlist(2)
PictureBox4.Image = quizlist(3)
End Sub
Can you suggest a solution for this one, or a different way to do it?
Thanks in advance

How to change my code to work a certain number of times?

My code gets a list of words from a txt file and chooses the words randomly. However, the same word can appear more than once and i need to know how to stop this from happening?
Here is the code:
Dim aryName As String() = Nothing
aryName = File.ReadAllLines(Application.StartupPath & "\Random\fnames.txt")
Dim randomWords As New List(Of String)
For i = 0 To aryName.Length - 1
If randomWords.Contains(aryName(i)) = False Then
randomWords.Add(aryName(i))
End If
Next
Dim random As New Random
Label2.Text = (randomWords(random.Next(0, randomWords.Count - 1)).ToString)
Maybe this might work, although it's in english and not code :(
if label1.text is changed then
Get label1.text
if label.text becomes this word again then
run the random code
end if
end if
This should prevent immediate repeats:
Dim random As New Random
'Just create a temporary holder for comparison
Dim word As String = Label2.Text
'Run a while loop that works as long as there
'is no change to the word. This should prevent
'back to back repeats.
While word = Label2.Text
word = (randomWords(random.Next(0, randomWords.Count - 1)).ToString)
End While
Label2.Text = word
If you don't want it to repeat ever again, you should probably remove the used word from the randomWords List.
Dim random As New Random
Label2.Text = (randomWords(random.Next(0, randomWords.Count - 1)).ToString)
randomWords.Remove(Label2.Text)
You can a) remove the selected word from the list, or b) you can random sort the list first.
Option a) is already addressed in another answer
Option b) lets you retain all the words in memory. Here is the code:
Dim randomWords As New List(Of String)(File.ReadAllLines(Application.StartupPath & "\Random\fnames.txt"))
Dim random As New Random
randomWords.Sort(Function(s1 As String, s2 As String) random.Next(-1, 1))
For index As Integer = 0 To randomWords.Count - 1
Label2.Text = randomWords(index)
Next
Modify your For loop to prevent dupes in aryName from getting into randomWords:
For i = 0 To aryName.Length - 1
If randomWords.Contains(aryName(i)) = False Then
randomWords.Add(aryName(i))
End If
Next

Not able to add values in second Combobox

Here is my code.
for example TextBox1.Text= 12,34,45,67,67
Dim process_string As String() = TextBox1.Text.Split(New Char() {","})
Dim process As Integer
For Each process In process_string
Combo1.Items.Add(process)
count = count + 1
Next process
total_process.Text = count
End If
Dim array(count) As String
Dim a As Integer
For a = 0 To count - 1
array(a) = Combo1.Items(a).ToString
Next
a = 0
For a = count To 0
Combo2.Items.Add(array(a).ToString)
Next
i want to add values in reversed order in combobox2 that are available in combobox1
but when i run the application the second combobox remains empty and not showing any value.
You've specified this for loop
For a = count To 0
But you need to add STEP -1 to go backwards like that.
For a = count To 0 Step -1
2 things. First of all, K.I.S.S. Keep it simple stupid
For i As Integer = ComboBox1.Items.Count - 1 To 0 Step -1
ComboBox2.Items.Add(ComboBox1.Items(i))
Next
second: It didn't work because you forgot the Step -1 on your last loop
~~~~~~~~~~~~~~Edit~~~~~~~~~~~~~~
Sorting the data in a combo box should be done with the sorted property on a combo box
ComboBox3.Sorted = True
Sorting the data in reverse order should be done with arrays as you were trying to do before. The following code should suffice:
Dim List As ArrayList = ArrayList.Adapter(ComboBox3.Items)
List.Sort()
List.Reverse()
ComboBox4.Items.AddRange(List.ToArray)
If you wanted to get creative, you could potentially create your own combo box class and make your own version of the sorted property that allows for "sort alpha", "sort numeric", "sort alpha Desc", and "sort numeric desc" and perhaps some other options. But I'd only do that if you were going to use this in a lot of places.