VB6 - Transfer Text2 specific multi row into Text1

Hi

I have 2 Text box. Text1 and Text2

Text2 is a multiLine Textbox.

I would like to transfer the Text2 selected line text into Text1 when i click

Ex:
If i have in my Text 2:
111-11-1111
222-22-2222


If i click on the second line in the Text 2, the text 222-22-2222 would be transferred into Text1.

How can i do that?

So far i have the below code but it just transfer the 3 first numbers:
Clipboard.clear
Clipboard.SetText Text2.SelText

Text1.Text = Text2.SelText()
Text1.SetFocus
Text1.SelStart = Len(Text1.Text)

Open in new window


Thanks for your help

Transfer picture
LVL 11
Wilder1626Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin LissOlder than dirtCommented:
It seems strange to do this from a textbox. How does the text get into Text2? In any case...

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, _
                                     ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const EM_LINEINDEX = &HBB
Private Const EM_LINEFROMCHAR = &HC9
Private Const EM_LINELENGTH = &HC1
Private Const EM_GETLINE = &HC4

Private Sub Command1_Click()

Dim lngSelectedLine As Long
Dim lngLength As Long
Dim lngFirstCharPos As Long
Dim strBuffer As String

' Determine selected line number (Starts at 0)
lngSelectedLine = SendMessage(Text1.hwnd, EM_LINEFROMCHAR, -1, 0)
lngFirstCharPos = SendMessage(Text1.hwnd, EM_LINEINDEX, lngSelectedLine, 0&)
' length of line
lngLength = SendMessage(Text1.hwnd, EM_LINELENGTH, lngFirstCharPos, 0&)

strBuffer = Space(lngLength)
' get line text
Call SendMessage(Text1.hwnd, EM_GETLINE, lngSelectedLine, ByVal strBuffer)
Text1.text = strBuffer

Open in new window

0
Ryan ChongCommented:
try:

Private Sub Text2_Click()
    Dim tmp As String, tmpF As String
    Dim tmpArr() As String, tmpArrF() As String
    
    tmpF = Text2.Text
    tmp = Mid(Text2.Text, 1, Text2.SelStart + 1)
    
    tmpArrF = Split(tmpF, vbCrLf, , vbTextCompare)
    tmpArr = Split(tmp, vbCrLf, , vbTextCompare)
    If UBound(tmpArr) = 0 Then
        If UBound(tmpArrF) = 0 Then
            Text1.Text = Split(Text2.Text, " ", , vbTextCompare)(0)
        Else
            Text1.Text = Split(tmpArrF(0), " ", , vbTextCompare)(0)
        End If
    Else
        Text1.Text = Split(tmpArrF(UBound(tmpArr)), " ", , vbTextCompare)(0)
    End If
End Sub

Open in new window

0
Martin LissOlder than dirtCommented:
My first code transfers the whole line. This just transfers the first part.

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, _
                                     ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const EM_LINEINDEX = &HBB
Private Const EM_LINEFROMCHAR = &HC9
Private Const EM_LINELENGTH = &HC1
Private Const EM_GETLINE = &HC4

Private Sub Command1_Click()

Dim lngSelectedLine As Long
Dim lngLength As Long
Dim lngFirstCharPos As Long
Dim strBuffer As String
Dim strParts() As String

' Determine selected line number (Starts at 0)
lngSelectedLine = SendMessage(Text1.hwnd, EM_LINEFROMCHAR, -1, 0)
lngFirstCharPos = SendMessage(Text1.hwnd, EM_LINEINDEX, lngSelectedLine, 0&)
' length of line
lngLength = SendMessage(Text1.hwnd, EM_LINELENGTH, lngFirstCharPos, 0&)

strBuffer = Space(lngLength)
' get line text
Call SendMessage(Text1.hwnd, EM_GETLINE, lngSelectedLine, ByVal strBuffer)
strParts = Split(strBuffer, "   ")
Text1.text = strParts(0)

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

GrahamSkanRetiredCommented:
This code avoids sing the clipboard:
Dim LineEnd As Integer
Dim i As Integer
Dim strLines() As String

strLines = Split(Text2.Text, vbCrLf)

For i = 0 To UBound(strLines)
    LineEnd = LineEnd + Len(strLines(i)) + 2
    If LineEnd > Text2.SelStart Then
        Text1.Text = strLines(i)
        Exit Sub
    End If
Next i

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Martin LissOlder than dirtCommented:
@Wilder: I just want to point out that both Ryan's and Graham's code assume that there is carriage return at the end of each line in Text2. Depending on how the text get's there (like if it's pasted), that might not be the case.
0
Wilder1626Author Commented:
Hi all

Thanks for all your help. I will try all the solutions shortly.

MartinLiss, to answer your question, the Text2 is a pre-populated textbox with fix data. It never changes, it a fix list.
0
Martin LissOlder than dirtCommented:
Then why use Text2 at all? Instead you could use a grid or listview.
0
Wilder1626Author Commented:
Good question.

I would have to update to many other thing to the existing form features that was already built by another person. But yes, you or right, it could be cleaner. Let me see what i can do.

Thanks
0
Wilder1626Author Commented:
Hi all

I took the time to test them all, and all work. But due to the simplicity of the code, i will go with GrahamSkan. But i will still keep the other code and do some other test also.

Thanks again for all your help
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.