|
-
Aug 25th, 2010, 04:07 PM
#1
Thread Starter
New Member
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..
-
Aug 25th, 2010, 04:38 PM
#2
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
-
Aug 26th, 2010, 03:32 AM
#3
Thread Starter
New Member
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
-
Aug 26th, 2010, 04:04 AM
#4
Member
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
-
Aug 26th, 2010, 04:12 AM
#5
Re: help with vb 2008 express
 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
-
Aug 26th, 2010, 04:18 AM
#6
Member
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.
-
Aug 26th, 2010, 04:52 PM
#7
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|