dcsimg
Results 1 to 7 of 7

Thread: HELP Log in using text file

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2018
    Posts
    4

    HELP Log in using text file

    can anyone help me with my code
    i have this on my text file
    01111
    01112
    01113
    01114
    01115

    why only the second line which is 01112 show login success, what should i do
    Code:
    Public Class Form1
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
    
            FileOpen(1, "C:\\studentid.txt", OpenMode.Input)
            While Not EOF(1) And TextBox1.Text = LineInput(1)
    
            End While
            If TextBox1.Text <> LineInput(1) Then
                MessageBox.Show("Invalid ID Number")
            Else
                MessageBox.Show("Login success")
            End If
            Dim form2 As New Form2
            form2.ShowDialog()
            FileClose(1)
        End Sub

  2. #2

    Thread Starter
    New Member
    Join Date
    Oct 2018
    Posts
    4

    Re: HELP Log in using text file

    and also what should i do, form2 still show even its should not because of invalid id number

  3. #3
    Fanatic Member PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Newport, UK
    Posts
    861

    Re: HELP Log in using text file

    The form is being shown regardless because the code to display the form is outside of the If Block, it will run regardless of the comparison being true or false.

    Regarding your original question I would first ask if there is a reason you are using the legacy VB6 file handling methods instead of the VB.Net file handling methods?

    IIRC the LineInput method just reads a line from the input file, therefore your while loop will be reading and discarding lines until the contents of the textbox equal the line being read, that means the If comparison immediately afterwards doesn't make sense as it will be looping until TextBox1.Text equals the line being read.....

    Have you tried setting a breakpoint in your code and stepping through it a line at a time to see if things are working the way you assume they are? Attempting to debug should be the first thing you do and if you have done it then telling us what you have tried really makes it easier for people to offer help.

  4. #4
    Member
    Join Date
    Jul 2017
    Location
    Exeter, UK
    Posts
    61

    Re: HELP Log in using text file

    Don't you think the test for whether the user entry matches the file entry should be inside your While loop, then only show the form if a match is found, perhaps using a Boolean to indicate that, something like,


    Code:
    dim foundit as boolean
    While Not EOF(1) or foundit = true
    if TextBox1.Text = LineInput(1)
       foundit = true
    End While
    FileClose(1)
    If  foundit Then
         MessageBox.Show("Login success")
         Dim form2 As New Form2
         form2.ShowDialog()
    Else
         MessageBox.Show("Invalid ID Number")
    End If
    Perhaps also look at the .NET methods of file processing which have surpassed the ones you seem to learning.

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,479

    Re: HELP Log in using text file

    Rather than reading the text file every time that the Button is clicked, load it once into a collection of some sort (preferably an array) in your Form's Load event. Then you'd check if the index is found in the collection by using the IndexOf method.

    Take a look at this example:
    Code:
    Public Class Form1
    
        Private ids() As String
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ids = IO.File.ReadAllLines("C:\\studentid.txt")
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If ids.IndexOf(TextBox1.Text) = -1 Then
                MessageBox.Show("Invalid ID Number")
            Else
                MessageBox.Show("Login success")
                Using frm2 As From2 = New Form2
                    frm2.ShowDialog()
                End Using
            End If
        End Sub
    
    End Class

  6. #6

    Thread Starter
    New Member
    Join Date
    Oct 2018
    Posts
    4

    Re: HELP Log in using text file

    thank you guys for the help, much appreciated
    i've solve it now

    Code:
    Public Class Form1
    
        Private ids() As String
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ids = IO.File.ReadAllLines("C:\\studentid.txt")
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If ids.IndexOf(ids, TextBox1.Text) = -1 Then
                MessageBox.Show("Invalid ID Number")
            Else
                MessageBox.Show("Login success")
                Using frm2 As From2 = New Form2
                    frm2.ShowDialog()
                End Using
            End If
        End Sub
    
    End Class

  7. #7

    Thread Starter
    New Member
    Join Date
    Oct 2018
    Posts
    4

    Re: HELP Log in using text file

    thank you guys for the help, much appreciated
    i've solve it now

    Code:
    Public Class Form1
    
        Private ids() As String
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ids = IO.File.ReadAllLines("C:\\studentid.txt")
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If ids.IndexOf(ids, TextBox1.Text) = -1 Then
                MessageBox.Show("Invalid ID Number")
            Else
                MessageBox.Show("Login success")
                Using frm2 As From2 = New Form2
                    frm2.ShowDialog()
                End Using
            End If
        End Sub
    
    End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width