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..
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
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
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
Re: help with vb 2008 express
Quote:
Originally Posted by
smra9
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
Re: help with vb 2008 express
Quote:
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.
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