Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all

i have one problem with code below, i try to click load to call one textfile load into datagridview by "Line", inside the textfile it has the website URL example www.live.com, i want to using Str() array to compare with function FindMyText(), does the website URL inside textfile isn't correct with value of str() array or not, if yes , just return the index of it back, but i have got problem with function FindMyText(). please help me if you know how to. thanks in advance

What I have tried:

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        If My.Computer.FileSystem.FileExists(MyFileName) Then

            Try
                Dim line As String
                Dim readFile As System.IO.TextReader = New StreamReader(MyFileName)

                While True
                    line = readFile.ReadLine()
                    If line Is Nothing Then
                        Exit While
                    Else
                        Dim str() As String = {"ABA.com", "live.com", "chat.com"}
                        dim v as string

                        If FindMyText(str(0)) = True Then
                           WebType = str(0)
                        ElseIf FindMyText(str(1)) Then
                           WebType = str(1)
                        ElseIf FindMyText(str(2)) Then
                           WebType = str(2)
                        Else
                           MsgBox("No have this string")
                        End If

                        Dim Row As String() = New String() {MName,, WebType, line}
                        DataGridView1.Rows.Add(Row)
                    End If
                End While
                readFile.Close()
                readFile = Nothing
                txtStatuse.Text = "State : File is load completed."
            Catch ex As Exception
                txtStatuse.Text = ex.ToString
            End Try
            
        Else
            Try
                TextBinaryWrite = New BinaryWriter(New FileStream(MyFileName, FileMode.Create))
                txtStatuse.Text = "Status : " & MyFileName & " is created."
                TextBinaryWrite.Close()
            Catch ex As Exception
                txtStatuse.Text = ex.Message + "Cannot create file."
                Return
            End Try
        End If
    End Sub



Public Function FindMyText(text As String) As Boolean

' Initialize the return value to false by default.
        Dim returnValue As Boolean = False 

' Ensure a search string has been specified.
        If text.Length > 0 Then  

' Here is the Error Code to Obtain the location of the search string in text.
            Dim indexToText As Integer = text 

' Determine whether the text was found in richTextBox1.
            If indexToText >= 0 Then 
                returnValue = True
            End If
        End If
        Return returnValue
    End Function


End Class
Posted
Updated 14-May-21 2:26am
Comments
CHill60 13-May-21 10:36am    
What is the problem with FindMyTextx() ?
Mr.Kim2050 13-May-21 10:47am    
hi bro, my problem is when i try to read data "www.live.com " in text file load into datagridview when formload by using function FindMyText to compare "www.live.com" from text file isn't have any values match with Str() array or not? if it has value match, just return the index. but i have problem can't compare text , i dont know how to do with it , pls help me if you know how to compare string like this

Dim indexToText As Integer = text
Mr.Kim2050 13-May-21 10:53am    
I used to search inside the internet, i found this code is work with richtextbox search , i want to edit inside this code below by change from using richtexbox.find to using value "Line" which get from loading textfile "www.live.com" ,


Dim str() As String = {"ABA.com", "live.com", "chat.com"}
Dim WebType as string

If FindMyText(str(0)) = True Then 'chaturbate.com
WebType = str(0)

TextAppend(MName, WebType, WebURL, MyFileName)
ElseIf FindMyText(str(1)) Then
WebType = str(1)
End if


Public Function FindMyText(text As String) As Boolean

Dim returnValue As Boolean = False ' Initialize the return value to false by default.
If text.Length > 0 Then ' Ensure a search string has been specified.
Dim indexToText As Integer = RichTextBox1.Find(text) ' Obtain the location of the search string in richTextBox1.
If indexToText >= 0 Then ' Determine whether the text was found in richTextBox1.
returnValue = True
End If
End If
Return returnValue
End Function

Have you considered using IndexOf: String.IndexOf Method (System) | Microsoft Docs[^] If it finds it, it returns the index. If it doesn't, it returns -1.

Your "code" doesn't do anything except cause a compilation error...
 
Share this answer
 
Comments
Mr.Kim2050 13-May-21 11:19am    
yes i know this code below is not work , that why i try to find other solution, do you have any example code for me beside Microsoft Docs , because i am not good with it .
OriginalGriff 13-May-21 11:41am    
Get "good with it" - it's your main source of information on the .NET framework, and if you can't use it then you aren't going to get far at all.

And what's the complication? It's just explains the variable overloads you can use - one of which is simple: past it a string to look for and it returns an index ...
Even students can read it, and work it out - and most of them are terminal lazy idiots who want all their work done for them! :laugh:
CHill60 13-May-21 15:12pm    
5'd. They lost me with "hi bro"
OriginalGriff 13-May-21 15:48pm    
They've clearly never met my brother ... :laugh:
Mr.Kim2050 14-May-21 8:11am    
I try to use String.Contains() like this but when i start to compile the program it show the message error

System.NullReferenceException: Object reference not set to an instance of an object.
at WindowsApp1.Form1.Form1_Load(Object sender, EventArgs e) in C:\Users\USER\source\repos\WindowsApp1\WindowsApp1\Form1.vb:line 20



Imports System.IO
Imports System.Text

Public Class Form1

Dim TextBinaryWrite As BinaryWriter
Dim MyFileName As String = "MyFavorith.txt"
Dim str() As String = {"chat.com", "ABC.com", "stripABC.com"}
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If My.Computer.FileSystem.FileExists(MyFileName) Then

Dim Loadline As String
Dim readFile As System.IO.TextReader = New StreamReader(MyFileName)
Dim MName As String
Dim WebType As String = ""
Try
While True
Loadline = readFile.ReadLine()

If Loadline.Contains("stripABC.com") = True Then
WebType = "stripABC.com"
End If

If Loadline Is Nothing Then
Exit While
End If

Dim Row As String() = New String() {MName, Loadline}
'Dim Row As String() = New String() {MName, WebType, "", Loadline}
DataGridView1.Rows.Add(Row)

End While
readFile.Close()
readFile = Nothing
LbStatuse.Text = "State : File is load completed."

Catch ex As Exception
TextBox1.Text = ex.ToString
LbStatuse.Text = "Error Try when load file" & ex.ToString
End Try

Else
'Create new file if no have
Try
TextBinaryWrite = New BinaryWriter(New FileStream(MyFileName, FileMode.Create))
LbStatuse.Text = "Statuse : " & MyFileName & " is created."
TextBinaryWrite.Close()
Catch ex As Exception
LbStatuse.Text = ex.Message + "Cannot create file."
Return
End Try
End If

End Sub


I dont know why can't use this code ??

If Loadline.Contains("stripABC.com") = True Then
WebType = "stripABC.com"
End If
Normally, I wouldn't open a second solution, but this is nothing to do with the original question ...

Quote:
I try to use String.Contains() like this but when i start to compile the program it show the message error

System.NullReferenceException: Object reference not set to an instance of an object.
at WindowsApp1.Form1.Form1_Load(Object sender, EventArgs e) in C:\Users\USER\source\repos\WindowsApp1\WindowsApp1\Form1.vb:line 20


No, it isn't. That isn't a compiler error, that's a runtime error. They are very different beasties. You only get compiler errors before you code runs, you get runtime errors after tha. Unless your code compiles without errors, it cannot run!
And the compiler can't spot runtime errors - they even have a different name: they are exceptions.

So ... let's look at a null reference exception, and why it happens, because this is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterday's shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, VS will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, VS will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
 
Share this answer
 
Comments
Mr.Kim2050 14-May-21 8:58am    
thanks with your answer , hope i could find any solution other to solve it.
Mr.Kim2050 14-May-21 21:39pm    
so, how could i using binary reader file text line by line in formload and using string.contain to check if text file really contain the value i need or not ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900