English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I am using VB 98


Write a program which will ask the user to enter a sentence. At the click of a “remove first word” button the program will print the first word of the sentence in a picture box and replace the sentence in the input text box with the sentence without the first word. (In this way if the user enters a 5 word sentence and hits the “remove first word” button 5 times, the sentence will be reduced to nothing and 5 words will be printed in the picture box)

Private Sub cmdRemove_Click()
Dim strSentence As String
Dim intSpace As Integer
Dim strFirstPart As String
Dim strSecondPart As String
Dim strExtra As String


strSentence = Trim(txtSentence.Text)

intSpace = InStr(strSentence, " ")
strFirstPart = Left(strSentence, intSpace - 1)
strSecondPart = Mid(strSentence, intSpace)
strExtra = Mid(strSecondPart, Len(intSpace))


txtSentence.Text = strExtra
picOutput.Print (strFirstPart)
End Sub

My code works until the last word...then it crashes

Thanks

2007-10-28 12:43:16 · 3 answers · asked by Supreme_edge 2 in Computers & Internet Programming & Design

3 answers

Since this is Visual Basic and NOT C you don't have to worry about calling from a MAIN (That's C)

Your program is crashing on the last word because NO SPACES are in the remaining text. The Trim function is removing any before or after the word. I believe that the inStr function will return a -1 when it doesn't find an occurance of the search.

Add an if statement to check for intSpace = -1 if so skip the string manipulation which removes a word (as there is only one word left) from the sentence. Just move the remaining word from the text box to the picture box.

An error is being generated because the -1 in intSpace is being reduced further by -1 (-1-1 =-2) and this becomes an invalid index position in the Left statement.
strFirstPart = Left(strSentence, intSpace - 1)

Another way to handle this and prevent error generation is to perform the calculation of index as a seperate line outside of the left function and test index value is equal to or greater than zero
index = intSpace -1
if index >=0 then
strFirstPart = Left(strSentence, index)
etc...

Else


end if

2007-10-28 15:10:47 · answer #1 · answered by MarkG 7 · 0 0

Try this code here: Private Sub FillList() List1.Clear Dim i As Long Dim num As Long, bAdd As Boolean For i = 1 To 52 StartNum: num = Int(Rnd * 52) + 1 If NumberExists(num) Then GoTo StartNum List1.AddItem num Next i End Sub Private Function NumberExists(Number As Long) As Boolean Dim bExist As Boolean Dim j As Long For j = 0 To (List1.ListCount - 1) If (Val(List1.List(j)) = Number) Then bExist = True Next j If bExist Then NumberExists = True End Function Hope its what u need.

2016-05-25 23:01:14 · answer #2 · answered by brook 3 · 0 0

Since you structured this as a subroutine you need to have a MAIN program that calls it and then returns from the subroutine to that MAIN address to end or to loop.

If you just jump into this subroutine as though it was in line code you will crash when you hit the End Sub since you will not have stored a return address to go to. Crash is guaranteed at that point.

2007-10-28 13:12:39 · answer #3 · answered by Rich Z 7 · 0 1

fedest.com, questions and answers