not having programmed in any of the BASIC languages, I am wondering if Visual Basic’s string variable is in anyway similar to what a string variable is in C (i.e. is it defined as an array of characters in visual basic?)The reason I am asking this is that – I’d like to take the content of a text box (a file name) and then change the extension on the same file name to something else and put it in another text box – and I am not quite sure how to go about this in visual basic (the help file on the string variable was a bit brief…)
VB has a special datatype for String. You don’t have to define it as an array of characters…just do “Dim temp as String” and it’s a String.
The following code assumes:
To test this code, make a form with two textboxes (leave the default names of Text1 and Text2). Paste the following code into the double-click event of Text2. Run the project and try it with various things (or nothing!) in Text1.
I’ve commented the code heavily, hope everything’s clear.
Dim flag As Integer ‘flag is used for the condition in the Do/LoopDim x As IntegerDim temp As String’x is being used as a “counter” variable’we set its length herex = Len(Text1)’next line errortraps to make sure that it’s not an’empty textboxIf x > 0 ThenDo While Not flag ‘as long as flag is False…’next line picks out one character from the string.’the first pass through the loop will get the last’character in the string, the next pass will get’the next-to-the-last, etc. because we decrement’x by one in the next “If/End If” statementtemp = Mid$(Text1, x, 1)If temp = “.” Then flag = True x = x – 1Else x = x – 1End If’the next line gracefully exits the loop if’there’s no period in Text1 at allIf x = 0 Then flag = TrueLoop’the next line first tests to make sure that’there was a period found in Text1… if so’it adds everything to the left of the period’and “.ext” and puts the resultant string into’Text2. You can remove the “If x > 0 Then” if’you think there might be an “extensionless”‘filename in Text1.If x > 0 Then Text2 = Left$(Text1, x) & “.ext”End If