Hb_Kai Posted December 4, 2008 Report Share Posted December 4, 2008 At the minute I'm using a form with a textbox which asks a user to input a name, then what I have to do is test the input to see if any characters are numeric, this part I have done using a For loop and a MsgBox saying "A number was found" followed by Exit Sub to end the program, but the problem is I can't think of how I would test the input to see how many characters are numbers and then display this count in a MsgBox.Anyway, here's the code I'm using if you'd need it.Dim OneCharacter As Char Dim FirstName As String Dim i As Integer Dim TextLength As Integer Dim NumberCount As Integer FirstName = Trim(txtChars.Text) TextLength = FirstName.Length For i = 0 To TextLength - 1 If IsNumeric(OneCharacter) Then MsgBox("A number was found") Exit Sub End If OneCharacter = FirstName.Chars(i) Next i End SubEnd ClassWould it be best to add another For loop inside the If statement? Quote Link to comment Share on other sites More sharing options...
ɹəuəllıʍ ʇɐb Posted December 4, 2008 Report Share Posted December 4, 2008 First, you need to think the thing through - what do you want to achieve?You want to knowif there are any numbers in the string?how many numbers there are in the string?This is basically the same question; if the result from the 2nd question above is 0, then there are none, otherwise there are.Meaning: you will have to complete the loop; not exit when you find a number, but count it instead.P.S. Your Programming sub-forum is being discussed by forum staff; I think it will be coming soon B) Quote Link to comment Share on other sites More sharing options...
ɹəuəllıʍ ʇɐb Posted December 4, 2008 Report Share Posted December 4, 2008 Also, watch where you initialize OneCharacter - does it contain any value when you test it the first time? Quote Link to comment Share on other sites More sharing options...
Hb_Kai Posted December 4, 2008 Author Report Share Posted December 4, 2008 P.S. Your Programming sub-forum is being discussed by forum staff; I think it will be coming soon B):) Ta for letting me know. :)I need to use OneCharacter As Char because it won't let me display the message box if I use FirstName As String.What I want to do is count how many numbers are in OneCharacter/FirstName and the display them in a message box. With the code I posted before, it finds numbers in OneCharacter and displays a message saying "A number was found", the only thing is I don't know how to count them.Edit:- I don't know why I put OneCharacter at the bottom there though. Quote Link to comment Share on other sites More sharing options...
ɹəuəllıʍ ʇɐb Posted December 4, 2008 Report Share Posted December 4, 2008 ... the only thing is I don't know how to count them.How about something like this For i = 0 To TextLength - 1 OneCharacter = FirstName.Chars(i) If IsNumeric(OneCharacter) Then NumberCount = NumberCount + 1 End If Next iThen at the end something like If NumberCount > 0 Then MsgBox(NumberCount, " number(s) found") End IfI don't know if the format is 100% right, as I have never done any VB before.P.S. I also don't know if NumberCount needs to be initialized to 0, or if VB does that automatically. Quote Link to comment Share on other sites More sharing options...
Hb_Kai Posted December 4, 2008 Author Report Share Posted December 4, 2008 Thanks, that looks like it should work. :)NumberCount begins at zero because VB begins counting at zero unless it's counting with chars() or some other things. Quote Link to comment Share on other sites More sharing options...
Hb_Kai Posted December 4, 2008 Author Report Share Posted December 4, 2008 I tried it but it didn't work how I needed it to.I have now done something which brings up a message box saying "There are 0 numbers"Dim OneCharacter As Char Dim FirstName As String Dim i As Integer Dim TextLength As Integer Dim NumberCount As Integer FirstName = Trim(txtChars.Text) TextLength = FirstName.Length For i = 0 To NumberCount OneCharacter = FirstName.Chars(i) NumberCount = IsNumeric(OneCharacter) MsgBox("There are " & NumberCount & " numbers") Next i End SubI don't know why I'm finding this so hard. :( I went through the tutorial really easy until now - Is it supposed to be this confusing? :DEdit:-Oh, and I have set the text property of the text box "txtChars" to "george123" so there are numbers in the FirstName Quote Link to comment Share on other sites More sharing options...
Hb_Kai Posted December 4, 2008 Author Report Share Posted December 4, 2008 Sorry for the multi-posting! :(I've finally done it after drinking lots of tea, and smoking plenty cigarettes whilst on a "break" :)I was putting things in the wrong places and got myself confused, based on your code you suggested, Pat, I've been able to do it. :) Dim OneCharacter As Char Dim FirstName As String Dim i As Integer Dim TextLength As Integer Dim NumberCount As Integer FirstName = Trim(txtChars.Text) TextLength = FirstName.Length For i = 0 To TextLength - 1 OneCharacter = FirstName.Chars(i) If IsNumeric(OneCharacter) Then NumberCount = NumberCount + 1 End If Next i If NumberCount > 0 Then MsgBox(NumberCount & " numbers were found") End IfThanks :) I can move on now. :D Quote Link to comment Share on other sites More sharing options...
ɹəuəllıʍ ʇɐb Posted December 5, 2008 Report Share Posted December 5, 2008 We could probably shorten that For loop a little bit; try if this works For i = 0 To TextLength - 1 NumberCount = NumberCount + IsNumeric(FirstName.Chars(i)) Next iIf it works, then you can eliminate OneCharacter altogether. Quote Link to comment Share on other sites More sharing options...
Hb_Kai Posted December 5, 2008 Author Report Share Posted December 5, 2008 Quote Link to comment Share on other sites More sharing options...
ɹəuəllıʍ ʇɐb Posted December 5, 2008 Report Share Posted December 5, 2008 Some comments on the two version of the code (For loop).While my short version certainly looks elegant, it is not necessarily the best code.The earlier code, with several lines, is indeed better, because it is easier to read & understand. It is one of the important things when writing productive code, that others can read and understand it.Anyway, waiting for your next challenge... Quote Link to comment Share on other sites More sharing options...
Hb_Kai Posted December 5, 2008 Author Report Share Posted December 5, 2008 :lol: Maybe you can learn a bit of VB now. ;)Well my next one should be using text files along side the VB forms. Opening/saving/over writing, etc... and soon after it will be on to database programming. :D :lol:Thanks for the help. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.