none
Check if some text exists in a text file RRS feed

  • Question

  • Hello!

    I have some kind of database with questions and answers and i'm stuck at this part:

    I want to read the question from that db to a textbox and the answer to another textbox

    (the q&a look like this ex: "what color is the sky>blue")(after the ">" symbol is the answer)

    Hope that someone will understand my problem and help me with a solution.

    Thanks in advance! 


    • Edited by Quinthus Wednesday, March 21, 2018 8:40 PM
    Wednesday, March 21, 2018 8:39 PM

Answers

  • Hi

    Here is some very simplistic code that might help. Regarding the DB you mention, and the fact that it means nothing,this example uses a Text File for the questions/answers. Here, the file is called Questions and is located on my desktop.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim path As String = "C:\Users\lesha\Desktop\Questions.txt"
      Dim current As Integer = -1
      Dim lines() As String = Nothing
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    	lines = IO.File.ReadAllLines(path)
      End Sub
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    	current += 1
    	If current > lines.Count - 1 Then current = 0
    	TextBox1.Text = lines(current).Split(">"c)(0)
    	TextBox2.Text = lines(current).Split(">"c)(1)
      End Sub
    End Class

    the text file for this example contains:

    What colour is the sky?>Blue
    What colour is the sea?>Blue
    What is the darkest colour in the GB flag?>Blue
    What is the colour of Blue Paint?>Blue


    Regards Les, Livingston, Scotland

    That's what I'm looking for, but the problem is that my questions are on 2 or 3 rows, and that's the only way I can make my app working, is there a way that this code you gave me should read the question like that? 

    Thanks.

    • Marked as answer by Quinthus Wednesday, March 21, 2018 10:00 PM
    Wednesday, March 21, 2018 9:51 PM
  • Hi

    Here is an example that *may* help further. It will combine several lines together to form one question.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim path As String = "C:\Users\lesha\Desktop\Questions.txt"
      Dim current As Integer = -1
      Dim List As New List(Of String)
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    	Dim a As String = Nothing
    	For Each s As String In IO.File.ReadAllLines(path)
    	  If s.Contains(">") Then
    		List.Add(a & Trim(s))
    		a = Nothing
    	  Else
    		a &= Trim(s) & Space(1)
    	  End If
    	Next
      End Sub
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    	current += 1
    	If current > List.Count - 1 Then current = 0
    	TextBox1.Text = List(current).Split(">"c)(0)
    	TextBox2.Text = List(current).Split(">"c)(1)
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by Quinthus Thursday, March 22, 2018 11:37 AM
    Wednesday, March 21, 2018 10:57 PM

All replies

  • Either you or someone else today asked the same question, hopefully that is not you.

    In any case "some kind of database" means nothing, you need to specify the database and show your code where your are having issues using the code block button for posting code.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, March 21, 2018 9:33 PM
    Moderator
  • Hi

    Here is some very simplistic code that might help. Regarding the DB you mention, and the fact that it means nothing,this example uses a Text File for the questions/answers. Here, the file is called Questions and is located on my desktop.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim path As String = "C:\Users\lesha\Desktop\Questions.txt"
      Dim current As Integer = -1
      Dim lines() As String = Nothing
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    	lines = IO.File.ReadAllLines(path)
      End Sub
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    	current += 1
    	If current > lines.Count - 1 Then current = 0
    	TextBox1.Text = lines(current).Split(">"c)(0)
    	TextBox2.Text = lines(current).Split(">"c)(1)
      End Sub
    End Class

    the text file for this example contains:

    What colour is the sky?>Blue
    What colour is the sea?>Blue
    What is the darkest colour in the GB flag?>Blue
    What is the colour of Blue Paint?>Blue


    Regards Les, Livingston, Scotland

    Wednesday, March 21, 2018 9:38 PM
  • Hi

    Here is some very simplistic code that might help. Regarding the DB you mention, and the fact that it means nothing,this example uses a Text File for the questions/answers. Here, the file is called Questions and is located on my desktop.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim path As String = "C:\Users\lesha\Desktop\Questions.txt"
      Dim current As Integer = -1
      Dim lines() As String = Nothing
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    	lines = IO.File.ReadAllLines(path)
      End Sub
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    	current += 1
    	If current > lines.Count - 1 Then current = 0
    	TextBox1.Text = lines(current).Split(">"c)(0)
    	TextBox2.Text = lines(current).Split(">"c)(1)
      End Sub
    End Class

    the text file for this example contains:

    What colour is the sky?>Blue
    What colour is the sea?>Blue
    What is the darkest colour in the GB flag?>Blue
    What is the colour of Blue Paint?>Blue


    Regards Les, Livingston, Scotland

    That's what I'm looking for, but the problem is that my questions are on 2 or 3 rows, and that's the only way I can make my app working, is there a way that this code you gave me should read the question like that? 

    Thanks.

    • Marked as answer by Quinthus Wednesday, March 21, 2018 10:00 PM
    Wednesday, March 21, 2018 9:51 PM
  • That's what I'm looking for, but the problem is that my questions are on 2 or 3 rows

    Then you need to read as many rows as are required to make up the question text.   Your data base design will tell you how you find out which rows you need to read in order to make one question.   If that's not possible then the database design is wrong. 

    Wednesday, March 21, 2018 9:57 PM
  • That's what I'm looking for, but the problem is that my questions are on 2 or 3 rows

    Then you need to read as many rows as are required to make up the question text.   Your data base design will tell you how you find out which rows you need to read in order to make one question.   If that's not possible then the database design is wrong. 

    It's not a database, I just named it like that, it is a text file that contains q&a's

    My questions are formulated like that some of them:

    How many legs 

    got a horse>4

    Hope that you will understand and maybe help me :)

    Thanks a lot!


    • Edited by Quinthus Wednesday, March 21, 2018 10:02 PM
    Wednesday, March 21, 2018 9:59 PM
  • Hi

    Here is some very simplistic code that might help. Regarding the DB you mention, and the fact that it means nothing,this example uses a Text File for the questions/answers. Here, the file is called Questions and is located on my desktop.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim path As String = "C:\Users\lesha\Desktop\Questions.txt"
      Dim current As Integer = -1
      Dim lines() As String = Nothing
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    	lines = IO.File.ReadAllLines(path)
      End Sub
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    	current += 1
    	If current > lines.Count - 1 Then current = 0
    	TextBox1.Text = lines(current).Split(">"c)(0)
    	TextBox2.Text = lines(current).Split(">"c)(1)
      End Sub
    End Class

    the text file for this example contains:

    What colour is the sky?>Blue
    What colour is the sea?>Blue
    What is the darkest colour in the GB flag?>Blue
    What is the colour of Blue Paint?>Blue


    Regards Les, Livingston, Scotland

    That's what I'm looking for, but the problem is that my questions are on 2 or 3 rows, and that's the only way I can make my app working, is there a way that this code you gave me should read the question like that? 

    Thanks.

    Hi

    Do you mean like this? Or something else? If something else, then you need to explain fully what you want.


    Regards Les, Livingston, Scotland

    Wednesday, March 21, 2018 10:01 PM
  • Hi

    Here is some very simplistic code that might help. Regarding the DB you mention, and the fact that it means nothing,this example uses a Text File for the questions/answers. Here, the file is called Questions and is located on my desktop.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim path As String = "C:\Users\lesha\Desktop\Questions.txt"
      Dim current As Integer = -1
      Dim lines() As String = Nothing
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    	lines = IO.File.ReadAllLines(path)
      End Sub
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    	current += 1
    	If current > lines.Count - 1 Then current = 0
    	TextBox1.Text = lines(current).Split(">"c)(0)
    	TextBox2.Text = lines(current).Split(">"c)(1)
      End Sub
    End Class

    the text file for this example contains:

    What colour is the sky?>Blue
    What colour is the sea?>Blue
    What is the darkest colour in the GB flag?>Blue
    What is the colour of Blue Paint?>Blue


    Regards Les, Livingston, Scotland

    That's what I'm looking for, but the problem is that my questions are on 2 or 3 rows, and that's the only way I can make my app working, is there a way that this code you gave me should read the question like that? 

    Thanks.

    Hi

    Do you mean like this? Or something else? If something else, then you need to explain fully what you want.


    Regards Les, Livingston, Scotland

    Yes!

    That's exactly what I want, but I want to auto-detect how many rows has every question, cuz it depends, some of them are on 2 rows, some on 3 rows


    • Edited by Quinthus Wednesday, March 21, 2018 10:05 PM
    Wednesday, March 21, 2018 10:03 PM
  • It's not a database, I just named it like that, it is a text file that contains q&a's

    It doesn't matter.   You still need something in the layout of the data that tells you which rows correspond to which questions.  It might be an index at the start of the file, it might be a marker at the end of some rows that indicates this row is the last row for this question, it might be a marker at the end of each row that indicates whether or not this question continues on the next row, it might be a tag at the start of the row that contains the question number that the row refers to, or it might be something else.  If it doesn't exist, you need to create it.
    Wednesday, March 21, 2018 10:04 PM
  • That's exactly what I want, but I want to auto-detect how many rows has every question, because it depends, some of them are on 2 rows, some on 3 rows


    Hi

    I don't know what you mean.

    Do you mean the source text file has several lines representing one question - or what?

    The way the code posted works doesn't worry about how many lines the question has it will fit the question to the textbox.

    The text file I used for the image in my last post was:

    What colour of the sky if there is a long sentence in the way of the viewer preventing the line from being contained all on one line?>Blue
    What colour is the sea?>Blue
    What is the darkest colour in the GB flag?>Blue
    What is the colour of Blue Paint?>Blue


    Regards Les, Livingston, Scotland

    Wednesday, March 21, 2018 10:11 PM
  • Hi

    I don't know what you mean.

    Do you mean the source text file has several lines representing one question - or what?

    The way the code posted works doesn't worry about how many lines the question has it will fit the question to the textbox.

    The text file I used for the image in my last post was:

    What colour of the sky if there is a long sentence in the way of the viewer preventing the line from being contained all on one line?>Blue
    What colour is the sea?>Blue
    What is the darkest colour in the GB flag?>Blue
    What is the colour of Blue Paint?>Blue


    Regards Les, Livingston, Scotland

    Hello

    Yes, the source text file has several lines representing one question, ex:

    "How many legs 

    got a horse>4"

    Your code works perfect, but only with 1 row per question&answer

    Wednesday, March 21, 2018 10:16 PM
  • Yes, the source text file has several lines representing one question, ex:

    "How many legs 

    got a horse>4"

    Your code works perfect, but only with 1 row per question&answer

    Hi

    Why is the text file arranged like that? Can you not remake the text file as per my example?


    Regards Les, Livingston, Scotland

    Wednesday, March 21, 2018 10:42 PM
  • Hi

    Here is an example that *may* help further. It will combine several lines together to form one question.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim path As String = "C:\Users\lesha\Desktop\Questions.txt"
      Dim current As Integer = -1
      Dim List As New List(Of String)
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    	Dim a As String = Nothing
    	For Each s As String In IO.File.ReadAllLines(path)
    	  If s.Contains(">") Then
    		List.Add(a & Trim(s))
    		a = Nothing
    	  Else
    		a &= Trim(s) & Space(1)
    	  End If
    	Next
      End Sub
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    	current += 1
    	If current > List.Count - 1 Then current = 0
    	TextBox1.Text = List(current).Split(">"c)(0)
    	TextBox2.Text = List(current).Split(">"c)(1)
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by Quinthus Thursday, March 22, 2018 11:37 AM
    Wednesday, March 21, 2018 10:57 PM
  • Hi

    Here is an example that *may* help further. It will combine several lines together to form one question.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim path As String = "C:\Users\lesha\Desktop\Questions.txt"
      Dim current As Integer = -1
      Dim List As New List(Of String)
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    	Dim a As String = Nothing
    	For Each s As String In IO.File.ReadAllLines(path)
    	  If s.Contains(">") Then
    		List.Add(a & Trim(s))
    		a = Nothing
    	  Else
    		a &= Trim(s) & Space(1)
    	  End If
    	Next
      End Sub
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    	current += 1
    	If current > List.Count - 1 Then current = 0
    	TextBox1.Text = List(current).Split(">"c)(0)
    	TextBox2.Text = List(current).Split(">"c)(1)
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    Hello!

    Yes, that's what I was looking for, it helped me a lot.



    Now, I'm stuck again, I want to read the text from that "database" something like that:

    If textbox1.text = "What colour is the sea?" Then

    Read that from the line that the question is from the database

    Hope that you will understand 


    Thanks a lot again !

    Edit: to help you understand my second problem, I'll explain it here.

    My program reads a question from an image, then, the readed question it's writed to a textbox, so, I want to read that question from textbox then verify if it exists in that "db", if exists, then read it and also display then answer. Hope you will understand better

    • Edited by Quinthus Thursday, March 22, 2018 11:57 AM
    Thursday, March 22, 2018 11:39 AM

  • Now, I'm stuck again, I want to read the text from that "database" something like that:

    If textbox1.text = "What colour is the sea?" Then

    Read that from the line that the question is from the database

    Hope that you will understand 


    Thanks a lot again !

    Then you need to create a new question. 

    Success
    Cor

    Thursday, March 22, 2018 11:54 AM