question

Fairwinds-3146 avatar image
0 Votes"
Fairwinds-3146 asked XingyuZhao-MSFT commented

How to Validate text box entry ends data from .txt file

Newbie question:

I am new to using Visual Studio (I was decent with MS Access) and I'm trying to validate user entries into a textbox, and there are a number of conditions, but I'm only really struggling with the last part.

I need to ensure that the entry ends with any of the strings that are stored in a .txt file (which supervisors can edit at any time to keep up with future updates)

Can I compare against the file or do I need to somehow open it first? Do I need to store them in an array before I can compare against them? How do I write my code to use the file/array like a wildcard?

I assume the comparison part could look similar to this:

If (txtInput Like "*[<.txtfile>]" = True) Then

dotnet-visual-basic
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Viorel-1 avatar image
0 Votes"
Viorel-1 answered Fairwinds-3146 commented

Try something like this:

 If File.ReadLines(txtFilePath).Any(Function(line) txtInput.TrimEnd.EndsWith(line.TrimEnd, StringComparison.CurrentCultureIgnoreCase)) Then
    
    . . .
    
 End If


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

That is almost exactly what I was missing. However I need it to only return a true result if the end of txtInput is the exact same as an entire line from the text file, and currently it will accept a portion of a line.

i.e. I need it to only take "supervisor", and not allow "ervis"

I can probably do more research on what you've given me and come up with what I need though. Thank you so much, you've helped immensely!

0 Votes 0 ·
Fairwinds-3146 avatar image
0 Votes"
Fairwinds-3146 answered XingyuZhao-MSFT commented

Sadness. I was wrong. I am still getting stumped, and now I realize that it has to match exactly with the entire portion of the txtInput(user entry) that follows "00", which is another layer of complexity I wasn't expecting.

i.e. with LEAD in the text file, "...00LEAD" would be accepted, but "...00SLEAD", "...00LEADS", "...00EAD", and "..00LEA" would all fail.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @Fairwinds-3146 ,

with LEAD in the text file, "...00LEAD" would be accepted

You can splice 00 to LEAD directly, the code looks like

 Dim line As String = "00" + txtInput 'LEAD

If I have any misunderstanding, please provide more details here.

0 Votes 0 ·
Fairwinds-3146 avatar image
0 Votes"
Fairwinds-3146 answered XingyuZhao-MSFT commented

I figured it out eventually, and I was thinking about it wrong. I needed to trim my entry data down to just the part I needed to compare, then treat each line in the text file as an .equals test against the trimmed entry as a whole.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @Fairwinds-3146 ,
I'm glad your problem has been solved. Please click "Accept Answer" to your solution, so that it will help others find the answer quickly if they meet similar problems.

0 Votes 0 ·