-
Hi there,
The text in my label can be dragged and dropped into another label. Does anybody know how I can have it so that, if the character "-" appears in the label before it is dragged and dropped it will become "+", and vice versa?
For example, +12 is in label1. When it is dragged and dropped into label2 it becomes -12?
I was going to use 'Do Until InStr etc', but I'm not too sure how to use this.
Any help would be much appreciated!
-
If you have VB6 then you can use Replace function.
Code:
Dim strText As String
strText = "+12"
strText = Replace(strText, "+", "-")
If you have VB5 and earlier then you can use this function I wrote a while back:
Code:
Public Function ReplaceText(pText As String, pFindString As String, pReplaceString As String) As String
Do Until InStr(pText, pFindString) = 0
pText = Left(pText, InStr(pText, pFindString) - 1) & _
Mid(pText, InStr(pText, pFindString) + Len(pFindString))
Loop
End Function
Then you can use it similar to the VB6's one:
Code:
Dim strText As String
strText = "+12"
strText = ReplaceText(strText, "+", "-")
-
Serge,
Thanks for the response. However, I'm not too sure about the code you gave me - I'm sure it makes sense, just I'm making a mess of it.
At the top of my code I have:
Public Function ReplaceText(pText As String, pFindString As String, pReplaceString As String) As String
Do Until InStr(pText, pFindString) = 0
pText = Left(pText, InStr(pText, pFindString) - 1) & _
Mid(pText, InStr(pText, pFindString) + Len(pFindString))
Loop
End Function
And then:
Private Sub Label2_DragDrop(Source As Control, X As Single, Y As Single)
Dim strText As String
strText = "+12"
strText = ReplaceText(strText, "+", "-")
+12 is in label1. And I want label2 to be -12.
P.S: Instead of saying "+12" couldn't I have:
strText = Label1.Caption (So I could use other numbers than +12)?
Thanks anyway!
-
No, strText was just part of the example. In your case you would have to use the function like this:
Code:
Private Sub Label2_DragDrop(Source As Control, X As Single, Y As Single)
If TypeOf Source Is Label Then
Source.Caption = ReplaceText(Source.Caption, "+", "-")
End If
End Sub