Results 1 to 7 of 7

Thread: help with vb 2008 express

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2010
    Posts
    3

    help with vb 2008 express

    Hi, i'm new on the forum and new in programing with visual basic...i have one problem and i'm sure that it will be piece of cake for you... I need to make a program which will for every word written in textbox search it's first letter and when i click on the button it will give me different message in the text. it would be the best that i can make that it gives one message for words thats starts from a to h, second message for letters from i to o, and so on..

  2. #2
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: help with vb 2008 express

    So, split it by words first of all:
    Code:
    Dim words() As String = Me.TextBox1.Text.Split(New Char() {vbCr,vbLf,vbTab," "c}, StringSplitOptions.RemoveEmptyEntries)
    Then, loop through each one and find its first letter:
    Code:
    For Each w As String In words
         Dim firstChar As Char = Char.ToLower(w.Chars(0)) 'Make sure it's lowercase
    Finally, compare the character:
    Code:
         Select Case firstChar
              Case "a"c To "h"c
                   ' Message for a to h
              Case "i"c To "o"c
                   ' Message for i to o
              'Etcetera.
    Next

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2010
    Posts
    3

    Re: help with vb 2008 express

    Thanks, I have tried this but whatever i wrote in the textbox it gives me the first message from select case (letter from a to h). Can you please check this out, because i can't figure out what's wrong.


    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim words() As String = Me.TextBox1.Text.Split(New Char() {vbCr, vbLf, vbTab, " "c}, StringSplitOptions.RemoveEmptyEntries)
    
            For Each w As String In words
                Dim firstChar As Char = Char.ToLower(w.Chars(0))
            Next
    
            Select Case "firstChar"
    
                Case "a"c To "h"c
                    MsgBox("letter from a to h")
                Case "i"c To "x"c
                    MsgBox("letter from i to x")
                Case Else
                    MsgBox("everything else..")
            End Select

  4. #4
    Member
    Join Date
    Jan 2010
    Posts
    42

    Re: help with vb 2008 express

    In your code 'firstChar' is local to the loop.

    Try this:
    Code:
    For Each w As String In words
            Dim firstChar As Char = Char.ToLower(w.Chars(0))
           
            Select Case "firstChar"
    
                Case "a"c To "h"c
                    MsgBox("letter from a to h")
                Case "i"c To "x"c
                    MsgBox("letter from i to x")
                Case Else
                    MsgBox("everything else..")
            End Select
     Next

  5. #5
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: help with vb 2008 express

    Quote Originally Posted by smra9 View Post
    Thanks, I have tried this but whatever i wrote in the textbox it gives me the first message from select case (letter from a to h). Can you please check this out, because i can't figure out what's wrong.
    Why did you put quotes around firstChar in the Select Case line, aren't you trying to find/compare words from the text box?

    Code:
        Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            ' test words
            TextBox1.Text = "Zoo apple Ipod"
    
            Dim words() As String = Me.TextBox1.Text.Split(New Char() {vbCr, vbLf, vbTab, " "c}, StringSplitOptions.RemoveEmptyEntries)
            For Each w As String In words
                Dim firstChar As Char = Char.ToLower(w.Chars(0))
                Select Case firstChar
                    Case "a"c To "h"c
                        MsgBox("Starts with a to h: Word = " & w)
                    Case "i"c To "x"c
                        MsgBox("Starts with i to x: Word = " & w)
                    Case Else
                        MsgBox("other: Word = " & w)
                End Select
            Next
    
        End Sub

  6. #6
    Member
    Join Date
    Jan 2010
    Posts
    42

    Re: help with vb 2008 express

    Why did you put quotes around firstChar in the Select Case line, aren't you trying to find/compare words from the text box?
    Good suggestion. Sorry, I didn't noticed that.

  7. #7

    Thread Starter
    New Member
    Join Date
    Aug 2010
    Posts
    3

    Re: help with vb 2008 express

    Thanks to everyone, especially to Edgemeal...i changed your code a little bit which is best for me and it finally works...so here is the code if someone else need it:
    Code:
    Dim words() As String = Me.TextBox1.Text.Split(New Char() {vbCr, vbLf, vbTab, " "c}, StringSplitOptions.RemoveEmptyEntries)
            For Each w As String In words
                Dim firstChar As Char = Char.ToLower(w.Chars(0))
    
                Select Case firstChar
                    Case "a"c To "h"c
                        MsgBox("Starts with a to h")
                    Case "i"c To "x"c
                        MsgBox("Starts with i to x")
                    Case Else
                        MsgBox("other")
                End Select
            Next

Posting Permissions

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



Click Here to Expand Forum to Full Width