VB Net Check a only word in Textbox

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 995 Views - Last Post: 24 October 2018 - 12:17 PM Rate Topic: -----

#1 cris1707   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 22-October 18

VB Net Check a only word in Textbox

Posted 22 October 2018 - 06:38 AM

I have the next program and the following code: I want to make a lottery program with numbers and make the schemes automatically. I'm about to do that, but I still have very little to find the winning variants on our numbers according to the numbers we extracted.

 ' Split string based on space
        Dim textsrtring As String = TextBox1.Text
        Dim words As String() = textsrtring.Split(New Char() {" "c})
        Dim found As Boolean = False
        ' Use For Each loop over words
        Dim word As String
        For Each word In words
            If TextBox2.Lines(0).Contains(word) Then
                found = True
                If CheckVar1.Text.Contains(word) Then
                Else
                    CheckVar1.Text = CheckVar1.Text + " " + TextBox1.Text()
                End If
            End If


The problem occurs when I want to check if there is a number 1 and has a choice between numbers 1, 2, 10, 13, 16 etc. My algorithm sees more than 1, 1 from number 10, 1 from number 13 but I only want to see 1 just once. An improvement to the existing code would help me or some guidance as to how I could do this. I have doubled using (not to display the same number twice), but if the number corresponds to the word according to the code, then it will display and I do not want that.

Example: 1 3 11 19 20 - My algoritm extract more 1, 1 from 11, 1 from 19, etc.

Is This A Good Question/Topic? 0
  • +

Replies To: VB Net Check a only word in Textbox

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14487
  • View blog
  • Posts: 58,070
  • Joined: 12-June 08

Re: VB Net Check a only word in Textbox

Posted 22 October 2018 - 06:42 AM

Perhaps use an integer list and not a string array.
Was This Post Helpful? 0
  • +
  • -

#3 cris1707   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 22-October 18

Re: VB Net Check a only word in Textbox

Posted 22 October 2018 - 07:25 AM

Can you tell me how to improve the code? Replace Dim Word as string with integer? I tried this but it still does not work. yes something wrong :)
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14487
  • View blog
  • Posts: 58,070
  • Joined: 12-June 08

Re: VB Net Check a only word in Textbox

Posted 22 October 2018 - 07:46 AM

Where you are checking if something contains another. You need to rework that logic to stop using strings and use integers.

I guess identifying _where_ you are doing that is your first step.

        Dim foo() As Int32 = {1, 4, 6, 7, 11}

        Console.WriteLine(foo.Contains(1))
        Console.WriteLine(Array.IndexOf(foo, 1))

        Dim bar As New List(Of Int32)

        bar.Add(4)
        bar.Add(6)
        bar.Add(7)
        bar.Add(1)
        bar.Add(11)
        Console.WriteLine(bar.Contains(1))
        Console.WriteLine(bar.IndexOf(1))

Was This Post Helpful? 0
  • +
  • -

#5 cris1707   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 22-October 18

Re: VB Net Check a only word in Textbox

Posted 22 October 2018 - 07:51 AM

Enter the number in TextBox2. If you find the number in TextBox1 then it will be displayed.

'Split string based on space
Dim textsrtring As String = TextBox2.Text
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 Every word In words
If TextBox1.Lines (0) .Contains (word) Then
found = True
If CheckVar1.Text.Contains (word) Then
else
CheckVar1.Text = CheckVar1.Text + "" + TextBox1.Text ()
End If
End If
Exit For
Next

I'll send a video to see.
Was This Post Helpful? 0
  • +
  • -

#6 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2682
  • View blog
  • Posts: 10,717
  • Joined: 03-December 12

Re: VB Net Check a only word in Textbox

Posted 22 October 2018 - 07:52 AM

#1 name your controls. TextBox1 doesn't say what it should relate to.

From what I see in the code, you just enter something in textbox2 and it says if it is in textbox1?
Was This Post Helpful? 0
  • +
  • -

#7 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14487
  • View blog
  • Posts: 58,070
  • Joined: 12-June 08

Re: VB Net Check a only word in Textbox

Posted 22 October 2018 - 07:54 AM

I don't need a video. You need to examine the flow of your code.

This is a string.. not an integer.
TextBox1.Lines (0)

That means you are searching, with 'contains', for a string here.
If TextBox1.Lines (0) .Contains (word)

You need to convert that all in integer collection to use the '.contains'... that or write loop to examine each individual number string (one or two digits) for an exact match.
Was This Post Helpful? 1
  • +
  • -

#8 cris1707   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 22-October 18

Re: VB Net Check a only word in Textbox

Posted 22 October 2018 - 08:15 AM

From what I see in the code, you just enter something in textbox2 and it says if it is in textbox1?

Yes

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        MYDP.Text = My.Application.Info.DirectoryPath + ("\Number.txt")
        TextBox1.Text = System.IO.File.ReadAllText(MYDP.Text)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' Split string based on space
        Dim textsrtring As String = TextBox2.Text
        Dim words As String() = textsrtring.Split(New Char() {" "c})
        Dim found As Boolean = False

        ' Use For Each loop over words
        Dim word As String
        For Each word In words
            If TextBox1.Lines(0).Contains(word) Then
                found = True
                If CheckVar1.Text.Contains(word) Then
                Else
                    CheckVar1.Text = CheckVar1.Text + " " + TextBox2.Text()
                End If
            End If
            Exit For
        Next
    End Sub
End Class



Not working. I have number:

 11 2 3 6 8
 11 2 6 7 9
 11 2 7 8 10
 11 3 4 5 7


I enter 1 in the box on the first line, and it appears to me as if it exists. But it's about 11 not 1.
Was This Post Helpful? 0
  • +
  • -

#9 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14487
  • View blog
  • Posts: 58,070
  • Joined: 12-June 08

Re: VB Net Check a only word in Textbox

Posted 22 October 2018 - 08:18 AM

I do not understand what you wrote.
Was This Post Helpful? 0
  • +
  • -

#10 cris1707   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 22-October 18

Re: VB Net Check a only word in Textbox

Posted 22 October 2018 - 08:27 AM

I enter the number 1 in TextBox2, and it appears to me as if it were in Textbox1. But it's about number 11, not 1.

Posted Image

LINK IMAGEBAM
Was This Post Helpful? 0
  • +
  • -

#11 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14487
  • View blog
  • Posts: 58,070
  • Joined: 12-June 08

Re: VB Net Check a only word in Textbox

Posted 22 October 2018 - 08:28 AM

Are you restating the first post? Yeah, I get what your issue is. Seeing if a string is in another string doesn't care if you think they should be numbers or not. 1 shows up in 1 or 11, or even 101. Convert what you are searching to a collection of integers and use that for searching.
Was This Post Helpful? 0
  • +
  • -

#12 cris1707   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 22-October 18

Re: VB Net Check a only word in Textbox

Posted 22 October 2018 - 08:36 AM

it's just 2 numbers. I will only use numbers from 1 to 80 with space between them. example: 24 30 46 70 80. When I enter number 8, I do not want to display it as if it were 80, because it's about 80.

I received an answer but I do not know exactly how to apply.

>>> If you textbox number will be always integer then rather than comparing string you can convert it to integer then compare both integer value. as string.contains() will check as word. So in 13 as string it contains 1 but you compare integer value 13=1 then it will return you false. You can convert TextBox2.Lines(0) to integer value and also word to Integer and compare both value with equal (=) Kevin Shah 32 mins ago
Was This Post Helpful? 0
  • +
  • -

#13 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14487
  • View blog
  • Posts: 58,070
  • Joined: 12-June 08

Re: VB Net Check a only word in Textbox

Posted 22 October 2018 - 08:39 AM

That's what I have been saying.

Fun - cross posted here.
https://stackoverflo...word-in-textbox

I mean - it's not like you do not know how to convert a string to an array.

you have this line:
  Dim words As String() = textsrtring.Split(New Char() {" "c})

Was This Post Helpful? 0
  • +
  • -

#14 cris1707   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 22-October 18

Re: VB Net Check a only word in Textbox

Posted 22 October 2018 - 08:53 AM

Unfortunately, I got stuck. But I hope to get it done ... with a little help
Was This Post Helpful? 0
  • +
  • -

#15 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14487
  • View blog
  • Posts: 58,070
  • Joined: 12-June 08

Re: VB Net Check a only word in Textbox

Posted 22 October 2018 - 08:56 AM

Quote

. But I hope to get it done ... with a little help

Are you looking for people to just do the work for you?


Expanding on my example of above, this shows taking a string, splitting it around the space, and converting that to an list of integers. From there you can check for numbers.

        Dim bar() As String = "5 4 6 7 1 11".Split(" ") '-- split makes an array of strings broken apart by the space.

        Dim temp1 As New List(Of Int32)
        For Each i As String In bar '-- go through each string in the array and add it to the list of integers.
            temp1.Add(i)
        Next

        Console.WriteLine(temp1.Contains(1)) '-- access list of functions
        Console.WriteLine(temp1.IndexOf(1))

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2