My problem
Index was outside the bounds of the array. when i try to run the code , it generates this error
i have two forms : SIGN IN and SIGN UP , my problem is they don't work together and generates the error attached below
Dim fs As New FileStream("C:\Users\Selmen\Desktop\vb\logs.txt", FileMode.Open, FileAccess.ReadWrite)
Dim sr As New StreamReader(fs)
Dim sw As New StreamWriter(fs)
Dim s As String
Dim t() As String
Dim trouve As Integer = 0
Dim tt() As String
Dim ch As String
ch = TextBox1.Text + "#" + TextBox2.Text + "#" + TextBox3.Text + "#" + TextBox4.Text + "#" + TextBox5.Text
tt = ch.Split("#")
Do While (trouve = 0) And (sr.Peek > -1)
s = sr.ReadLine
t = s.Split("#")
If String.Compare(t(2), tt(2)) = 0 Then
trouve = 1
End If
Loop
If (trouve = 1) Then
MsgBox("user existant")
Else
sw.WriteLine(ch)
Me.Hide()
Form4.Show()
End If
sw.Close()
sr.Close()
fs.Close()
End Sub
If String.Compare(t(2), tt(2)) = 0 Then I get:
IndexOutOfRangeException was unhandled / Index was outside the bounds of the array.
Streams need to be disposed. Instead of using streams you can easily access a text file with the .net File class.
File.ReadAllLines returns an array of lines in the file. We can loop through the lines in a For Each. The lower case c following the "#" tells the compiler that you intend a Char not a String. String.Split expects a Char. Normally, String.Compare is used to order strings in alphabetical order. You just need an =. As soon as we find a match we exit the loop with Exit For.
We don't actually need the array of the text boxes Text property unless there is no match. Putting the elements in braces intializes and fills the array of String.
File.AppendAllLines does what it says. It is expecting an array of strings. As with the text boxes, we put our line to append in braces.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim p = "path to file"
Dim lines = File.ReadAllLines(p)
Dim trouve As Integer
For Each line In lines
Dim thirdField = line.Split("#"c)(2)
If thirdField = TextBox3.Text Then
trouve = 1
Exit For
End If
Next
If trouve = 1 Then
MsgBox("user existant")
Else
Dim tt = {TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text, TextBox5.Text}
File.AppendAllLines(p, {String.Join("#", tt)})
Me.Hid3e()
Form4.Show()
End If
End Sub
I have textbox receives number by the user.
the problem is that I want to format this textbox to show the numbers as the next face:
15000.25 >> 15,000.25
I used FormatNumber Function:
dim x as double
x = FormatNumber(textbox1.text,2)
textbox1.text = x
but here the problem is I want to keep the decimal places as entered by the user, examples:
15000.225 >> 15,000.225 NOT 15,000.22
0.00083 >> 0.00083 NOT 0
I hope my problem is clear.
I found the next solution:
Dim xStr As String = CStr(Me.Text)
Dim xCount As Integer = Len(Split(xStr, ".")(1))
Me.textbox1.Text = FormatNumber(Me.Text, xCount)
it's working for me.
thanks
Try this out:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim dbl As Double
If Double.Parse(TextBox1.Text, dbl) Then
Dim parts() As String = dbl.ToString.Split(".")
Dim strDecimalPattern As String = ""
If parts.Length = 2 Then
strDecimalPattern = New String("#", parts(1).Length)
End If
Dim strPattern As String = "{0:#,##0" &
If(strDecimalPattern <> "", ".", "") &
strDecimalPattern & "}"
TextBox1.Text = String.Format(strPattern, dbl)
End If
End Sub
I want to compare the Textbox1 with TextBox2, or Textbox line 1 of the text box to the 2nd line, to show me the existing Character in another textbox, or show me how many characters are repeated. iI really like learning, so I would be helpful because I want to learn...
TextBox1.Text = 1,4,7,11,13,16,19,20,28,31,44,37,51,61,62,63,64,69,71,79,80
TextBox2.Text = 1,5,7,10,13,16,26,20,28,31,44,37,51,72,73,74,69,71,79,80
TextBox3.Text = Character Repeated: 1,7,13,16,20,28,31,44,37,51,69,71,79,80
TextBox4.Text = Number of Character Repeated = 14
TextBox5.Text = Number of Character which has not been repeated: 4,11,19,61,62,63,64 etc, you got to idea
TextBox6.Text = Number of Character isn't Repeated: 7
here are some codes: but I do not know how to apply them correctly.
Code 1: Show repetable character:
' Split string based on space
TextBox1.Text = System.IO.File.ReadAllText(Mydpi.Text)
TextBox2.Text = System.IO.File.ReadAllText(Mydpi.Text)
TextBox4.Text = System.IO.File.ReadAllText(Mydpi.Text)
For i As Integer = 0 To TextBox2.Lines.Count - 1
Dim textsrtring As String = TextBox4.Lines(i)
Dim words As String() = textsrtring.Split(New Char() {","c})
Dim found As Boolean = False
' Use For Each loop over words
Dim word As Integer
For Each word In words
TxtbValBeforeCompar.Text = TextBox1.Lines(i)
CompareNumbers()
If TextBox1.Lines(i).Contains(word) Then
found = True
Dim tempTextBox As TextBox = CType(Me.Controls("Checkertxt" & 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 CompareNumbers()
'First Textbox that is to be used for compare
Dim textBox1Numbers As List(Of Integer) = GetNumbersFromTextLine(N1Check.Text)
'Second Textbox that is to be used for compare
Dim textBox2Numbers As List(Of Integer) = GetNumbersFromTextLine(TxtbValBeforeCompar.Text)
'Union List of Common Numbers (this uses a lambda expression, it can be done using two For Each loops instead.)
Dim commonNumbers As List(Of Integer) = textBox1Numbers.Where(Function(num) textBox2Numbers.Contains(num)).ToList()
'This is purely for testing to see if it worked you can.
Dim sb As StringBuilder = New StringBuilder()
For Each foundNum As Integer In commonNumbers
sb.Append(foundNum.ToString()).Append(" ")
TxtbValAfterCompar.Text = (sb.ToString())
Next
End Sub
Private Function GetNumbersFromTextLine(ByVal sTextLine As String) As List(Of Integer)
Dim numberList As List(Of Integer) = New List(Of Integer)()
Dim sSplitNumbers As String() = sTextLine.Split(" ")
For Each sNumber As String In sSplitNumbers
If IsNumeric(sNumber) Then
Dim iNum As Integer = CInt(sNumber)
TxtbValAfterCompar.Text = iNum
If Not numberList.Contains(iNum) Then
TxtbValAfterCompar.Text = ("")
numberList.Add(iNum)
End If
Else
End If
Next
Return numberList
End Function
Code 2: Remove Duplicate Chars (Character)
Module Module1
Function RemoveDuplicateChars(ByVal value As String) As String
' This table stores characters we have encountered.
Dim table(value.Length) As Char
Dim tableLength As Integer = 0
' This is our result.
Dim result(value.Length) As Char
Dim resultLength As Integer = 0
For i As Integer = 0 To value.Length - 1
Dim current As Char = value(i)
Dim exists As Boolean = False
' Loop over all characters in the table of encountered chars.
For y As Integer = 0 To tableLength - 1
' See if we have already encountered this character.
If current = table(y) Then
' End the loop.
exists = True
y = tableLength
End If
Next
' If we have not encountered the character, add it.
If exists = False Then
' Add character to the table of encountered characters.
table(tableLength) = current
tableLength += 1
' Add character to our result string.
result(resultLength) = current
resultLength += 1
End If
Next
' Return the unique character string.
Return New String(result, 0, resultLength)
End Function
Sub Main()
' Test the method we wrote.
Dim test As String = "having a good day"
Dim result As String = RemoveDuplicateChars(test)
Console.WriteLine(result)
test = "areopagitica"
result = RemoveDuplicateChars(test)
Console.WriteLine(result)
End Sub
End Module
You could make use of some LINQ such as Intersect and Union.
Assuming your TextBox1 and TextBox2 contains the text you have provided.
Here's a simple method to find repeated and non repeated characters.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim firstBoxList = TextBox1.Text.Split(",").ToList()
Dim secondBoxList = TextBox2.Text.Split(",").ToList()
Dim intersectionList = firstBoxList.Intersect(secondBoxList)
For Each str As String In intersectionList
TextBox3.Text = TextBox3.Text & str & ","
Next
TextBox4.Text = intersectionList.Count()
Dim notRepeatedCharacter = firstBoxList.Union(secondBoxList).ToList
notRepeatedCharacter.RemoveAll(Function(x) intersectionList.Contains(x))
For each str As String In notRepeatedCharacter
TextBox5.Text = TextBox5.Text & str & ","
Next
TextBox6.Text = notRepeatedCharacter.Count()
End Sub
The output is something like that:
This consider both of the textboxes not repeated character.
If you just want to find the not repeated characters from first list to the second, this should do it:
firstBoxList.RemoveAll(Function(x) secondBoxList.Contains(x))
For Each str As String In firstBoxList
TextBox7.Text = TextBox7.Text & str & ","
Next
TextBox8.Text = firstBoxList.Count
And this is the output:
Here's the full code using String.Join to make the lists look smoother in the text boxes:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'First we grab all the numbers written inside the textboxes (I am not verifying anything)
Dim firstBoxList = TextBox1.Text.Split(",").ToList()
Dim secondBoxList = TextBox2.Text.Split(",").ToList()
'Second we intersect the two lists and show them
Dim intersectionList = firstBoxList.Intersect(secondBoxList)
TextBox3.Text = String.Join(",", intersectionList)
TextBox4.Text = intersectionList.Count()
'We're checking the distintc character from both lists
Dim notRepeatedCharacter = firstBoxList.Union(secondBoxList).ToList
notRepeatedCharacter.RemoveAll(Function(x) intersectionList.Contains(x))
TextBox5.Text = String.Join(",", notRepeatedCharacter)
TextBox6.Text = notRepeatedCharacter.Count()
'we're checkng the distinct character inside first list that doesn't show in second list
firstBoxList.RemoveAll(Function(x) secondBoxList.Contains(x))
TextBox7.Text = String.Join(",", firstBoxList)
TextBox8.Text = firstBoxList.Count
End Sub
I have a text file on drive C:, I need to open and read the first line and do the code to display, then read second line and do same, etc. etc. until it reads all the lines in the text file.
Not sure how to do it...
My code
I think I figure it out, below, BUT how do i do that For EACH line of the text file, meaning, apply the same evaluation per line?
'type of message triggers
Dim type1 As String = "d1err./"
Dim type2 As String = "d1jam./"
Dim type3 As String = "d1spe./"
Dim type4 As String = "d2err./"
Dim type5 As String = "d2jam./"
Dim type6 As String = "d2spe./"
Dim fileReader As StreamReader = My.Computer.FileSystem.OpenTextFileReader("C:\A\data\display.txt")
Dim stringReader As String = fileReader.ReadLine()
While stringReader <> Nothing
Dim LineStart As String = stringReader.Substring(0, 7)
Dim RestOfLine As String = stringReader.Substring(7)
If LineStart = type1 Then
Display1.Dis1RTB1.Clear()
Display1.Dis1RTB1.Text = RestOfLine
ElseIf LineStart = type2 Then
Display1.Dis1RTB2.Clear()
Display1.Dis1RTB2.Text = RestOfLine
ElseIf LineStart = type3 Then
Display1.Dis1RTB3.Clear()
Display1.Dis1RTB3.Text = RestOfLine
ElseIf LineStart = type4 Then
Display2.Dis2RTB1.Clear()
Display2.Dis2RTB1.Text = RestOfLine
ElseIf LineStart = type5 Then
Display2.Dis2RTB2.Clear()
Display2.Dis2RTB2.Text = RestOfLine
ElseIf LineStart = type6 Then
Display2.Dis2RTB3.Clear()
Display2.Dis2RTB3.Text = RestOfLine
End If
stringReader = fileReader.ReadLine()
End While
fileReader.Close()
End Sub
Maybe this will help you
Public Class Form1
Dim fileReader As New IO.StreamReader("C:\Users\User\Desktop\New Text Document.txt")
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Not fileReader.EndOfStream Then
TextBox1.Text += fileReader.ReadLine & vbNewLine
End If
'Or you could do it like this
While Not fileReader.EndOfStream
Dim tmpStr As String = fileReader.ReadLine
Select Case tmpStr
Case "Dis1err"
'Do Work here
End Select
End While
End Sub
End Class
After finally fixing my last problem, the code that I ended with was
Function MD5(ByVal strToHash As String) As String
Dim md5Obj As New Security.Cryptography.MD5CryptoServiceProvider
Dim bytesToHash() As Byte = System.Text.Encoding.ASCII.GetBytes(strToHash)
bytesToHash = md5Obj.ComputeHash(bytesToHash)
Dim strResult As String = ""
For Each b As Byte In bytesToHash
strResult += b.ToString("x2")
Next
Return strResult
End Function
Dim words As IEnumerable(Of String) = File.ReadLines(OpenFileDialog1.FileName)
For Each word As String In words
If String.Equals(MD5(word), hash.Text) Then
Label2.Text = word
Else : Label2.Text = "Hash Could Not Be Cracked"
End If
Next
Now I need to make it stop once the hashed word matches the hash I typed in!
To stop a loop, use Exit For:
Dim words As IEnumerable(Of String) = File.ReadLines(OpenFileDialog1.FileName)
For Each word As String In words
If MD5(word) = hash.Text Then
Label2.Text = word
Exit For
Else : Label2.Text = "Hash Could Not Be Cracked"
End If
Next
But note that the Else makes no sense here (the form won’t be updated until after the method is left) and that String.Equals can be replaced here by = (I’ve already done that).
You could use LINQ's FirstOrDefault, since you're using ReadLines instead of ReadAllLines anyway:
Dim firstWord = (From line In IO.File.ReadLines(OpenFileDialog1.FileName)
Where String.Equals(MD5(line), hash.Text)).FirstOrDefault()
If firstWord IsNot Nothing Then
Label2.Text = firstWord
Else
Label2.Text = "Hash Could Not Be Cracked"
End If
Another way is a simple loop:
Dim lines = IO.File.ReadAllLines(OpenFileDialog1.FileName)
Dim matchingLine As String = Nothing
For i = 0 To lines.Length -1
Dim line = lines(i)
If String.Equals(MD5(line), hash.Text)) Then
matchingLine = line
Exit For
End If
Next
If matchingLine IsNot Nothing Then
Label2.Text = matchingLine
Else
Label2.Text = "Hash Could Not Be Cracked"
End If