i was trying to figure out this code by myself today and took me a bit before getting to this point
vb Code:
Private Function GetWord(TextBox_Name As TextBox) As String Dim WordFound As String Dim SplitWords() As String Dim WordFoundSplit() As String Dim DefaultStartPOS As Long Dim StartPOS As Long Dim WordPOS As Long Dim EndPOS As Long Dim i As Integer On Error Resume Next WordFound = "" StartPOS = 0 EndPOS = 0 WordPOS = 0 DefaultStartPOS = TextBox_Name.SelStart SplitWords = Split(TextBox_Name, Chr(32)) If DefaultStartPOS <= Len(SplitWords(0)) Then TextBox_Name.SelStart = 0 TextBox_Name.SelLength = Len(SplitWords(0)) WordFound = TextBox_Name.SelText TextBox_Name.SelLength = 0 TextBox_Name.SelStart = DefaultStartPOS Me.Caption = WordFound Else WordPOS = Len(SplitWords(0)) For i = 1 To UBound(SplitWords) StartPOS = WordPOS WordPOS = WordPOS + Len(SplitWords(i)) + 1 EndPOS = WordPOS If DefaultStartPOS <= EndPOS And DefaultStartPOS > StartPOS Then TextBox_Name.SelStart = WordPOS - Len(SplitWords(i)) TextBox_Name.SelLength = Len(SplitWords(i)) WordFound = TextBox_Name.SelText TextBox_Name.SelLength = 0 TextBox_Name.SelStart = DefaultStartPOS WordFoundSplit = Split(WordFound, vbNewLine) WordFound = WordFoundSplit(0) Exit For End If Next End If GetWord = WordFound End Function Private Sub txtScript_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) lblWord.Caption = GetWord(txtScript) End Sub
is there a easier formula or easier way to find the word you click on in a textbox(multiline)?




Reply With Quote