dcsimg
Results 1 to 26 of 26

Thread: Problem with lines (position) in textbox

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2019
    Posts
    19

    Problem with lines (position) in textbox

    Hi to everyone!

    I'm searching for place were, can introduce myself... not seen !!!

    Anyway, I'm a not professional programmer (I only like to do this!)


    My present problem are:

    I have a Textbox with a specify numbers of line visible ( : can show for exemple 10 line in is Highness).

    I have a way to know which is the number of line, were cursor is... But...
    When I scroll Text, and (for exemple) the last line (in that case: n10), and this line go to the top,
    there is a way to know which is the Number of the line at specific position??


    ...

  2. #2
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,171

    Re: Problem with lines (position) in textbox

    I do not BELIEVE there is a way (even with API's) which would allow you to find a particular line using the mouse (I could be VERY wrong, as someone MAY post a solution).

    MY solution would be to use a listbox instead. That is easy to determine which line the mouse is on. You could hide the textbox if desired (but just using a listbox by itself would be best).

    Here is a short example of using a listbox (filling it with a pre-existing MultiLine Textbox)...when you click on a line in the listbox (which would correspond with the one in the text box), you can see the the value of that line.

    Code:
    Option Explicit
    Dim sArray() As String
    
    
    Private Sub Form_Load()
        Text1.Text = "A" & vbCrLf & "B" & vbCrLf & "C"
        sArray = Split(Text1.Text, vbCrLf)
        Dim x As Integer
        For x = 0 To UBound(sArray)
            List1.AddItem sArray(x)
        Next x
       
    End Sub
    
    
    Private Sub List1_Click()
        Debug.Print List1.Text
    End Sub

  3. #3
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,171

    Re: Problem with lines (position) in textbox

    I take that back...researching I found something that may give you the solution you want....check out this code. When you click on your ML textbox, the debug window will show the LINE you clicked on, as well as the TEXT of that line...you should be able to take it from here:

    Code:
    Option Explicit
    
    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_LINEFROMCHAR = &HC9
    Private Const EM_LINEINDEX = &HBB
    
    
    Dim sArray() As String
    Private Sub Form_Load()
        Text1.Text = "4" & vbCrLf & "6" & vbCrLf & "10"
    End Sub
    
    
    Private Sub text1_Click()
        sArray = Split(Text1.Text, vbCrLf)
        Dim sMsg As String
        Dim nCol As Long
        Dim nRow As Long
    
    
        nCol = SendMessage(Text1.hwnd, EM_LINEINDEX, -1, 0)
        nCol = Text1.SelStart - nCol + 1
        nRow = SendMessage(Text1.hwnd, EM_LINEFROMCHAR, -1, 0) + 1
        sMsg = "Line: " & nRow
        Debug.Print sMsg & " " & sArray(nRow - 1)
    End Sub
    EDIT: But DO note that I populated the ML textbox with values AND the VB Carriage Return/Line Feed (vbCrLf)

    EDIT 2: You can disregard the nCol variable...this would be for multi-COLUMNs in a ML textbox.
    Last edited by SamOscarBrown; Sep 6th, 2019 at 08:41 AM.

  4. #4
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,171

    Re: Problem with lines (position) in textbox

    Oh yeah....WELCOME TO THE FORUM!!!!!

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Sep 2019
    Posts
    19

    Re: Problem with lines (position) in textbox

    HI again... Thanks for the WELCOME !

    -------------------------------------------

    I want to explain better what I need:

    Name:  for vbforum A.jpg
Views: 169
Size:  44.3 KBName:  for vbforum B.jpg
Views: 165
Size:  64.6 KB




    That's it!
    Attached Images Attached Images  
    Last edited by Daduq; Sep 6th, 2019 at 05:06 PM.

  6. #6
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,171

    Re: Problem with lines (position) in textbox

    With something like this...WHY are you using an ML Textbox...use a ListBox instead....!!!

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Sep 2019
    Posts
    19

    Re: Problem with lines (position) in textbox

    Because, I cannot use listbox; I need to do with Textbox. This is my principal control (in my program)!

    I trying to do... But, till this moment, I not find a way yet!!

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Sep 2019
    Posts
    19

    Re: Problem with lines (position) in textbox

    Take this opportunity for another question:

    I want to select a text in the same textbox (or in another, for exemple), but, I want to
    select it, from a certain position to the END.

    I'm doing tests!!

    I know, that I must use .Selstart to find starting point (is very simple), and I can know
    lenght with .Sellenght...
    But I can't do SelText with this two parameter ?

    Something like Text1.SelStart = Len(Text1.Text) + 1 ???



    In this moment I can't reason !!!
    Last edited by Daduq; Sep 7th, 2019 at 05:56 AM.

  9. #9
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,171

    Re: Problem with lines (position) in textbox

    Here you go (partially)---please do SOME work yourself...the code below will work with the Down Arrow, YOU look it over closely and figure out how to code the Up Arrow:

    Code:
    Option Explicit
    
    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_LINEFROMCHAR = &HC9
    Private Const EM_LINEINDEX = &HBB
    
    
    Dim sArray() As String
    Private Sub Form_Load()
        Form1.KeyPreview = True
        Dim x As Integer
        For x = 1 To 50  'Fill MY textbox
            Text1.Text = Text1.Text & CStr(x) & ". This is the LINE number ." & CStr(x) & vbCrLf
        Next x
        'NOTE: "THE TEXTBOX I USED HAS FIFTEEN (15) ITEMS SHOWN AT A TIME (LIKE YOURS)
    End Sub
    
    
    
    
    Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
        Select Case KeyCode
            Case 40  'down arrow
                sArray = Split(Text1.Text, vbCrLf)
                Dim sMsg As String
                Dim nRow As Long
                nRow = SendMessage(Text1.hwnd, EM_LINEFROMCHAR, -1, 0) + 1
                If nRow < 16 Then  'REMEMBER--MY textbox has 15 items shown at a time
                     Debug.Print "The line at First Position is Line n." & sArray(0)
                Else
                    Debug.Print "The line at First Position is Line n." & sArray(nRow - 15)
                End If
            Case 38  'up arrow
                Debug.Print "For YOUR homework, YOU figure out how to determine sArray(nRow) using the Up Arrow!!!"
        End Select
    End Sub

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Sep 2019
    Posts
    19

    Re: Problem with lines (position) in textbox

    Hi! Again!


    I tryed to do something, with this, but... Not work!
    This is not, really, what the code must do! Is only a way to SEE the result.

    There is a possibility to convert the Debug.Print in a variable ? (I think probably not!)


    Anyway: I don't put the code, only because, the part I need (and I have explaned, is the
    same thing, of a new project... Only this part, have no reason to show all the rest...
    Not linked!!


    I try in many way (for my possibilities), but... Nothing!

    I want to ask this too:

    How can I select all line (only 1 line) of a text, in textbox, by clicking!?

    Thanks again!

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Sep 2019
    Posts
    19

    Re: Problem with lines (position) in textbox

    Hi! Again!


    I tryed to do something, with this, but... Not work!
    This is not, really, what the code must do! Is only a way to SEE the result.

    There is a possibility to convert the Debug.Print in a variable ? (I think probably not!)


    Anyway: I don't put the code, only because, the part I need (and I have explaned, is the
    same thing, of a new project... Only this part, have no reason to show all the rest...
    Not linked!!


    I try in many way (for my possibilities), but... Nothing!

    I want to ask this too:

    How can I select all line (only 1 line) of a text, in textbox, by clicking!?

    Thanks again!

  12. #12
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,171

    Re: Problem with lines (position) in textbox

    Post # 3 does exactly that....selects ONE line by clicking.

  13. #13
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,171

    Re: Problem with lines (position) in textbox

    ​Download and unzip this project....uses a variable (msg) instead of Debug.print and puts variable into the second textbox.


    MLTextbox_SelectLine.zip

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Sep 2019
    Posts
    19

    Re: Problem with lines (position) in textbox

    Hi SamOscarBrown!

    First of all, thanks for your code!

    Yes! I'v seen that the post #3, is a right code...
    I noticed it now!


    I was really stupid not to do it right away, it was enough to put a MsgBox, to see the result. But the problem is on the other code (at post # 9), since I didn't find a possible way to pass the result to a variable.


    PS!!!!

    There must have been some problem when I tested it ... Because it wasn't going!
    Now it seems to work! (?)


    Now I'll post some of my whole idea OK !? (Today I tray to do other, and later i make new post)

  15. #15
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    1,970

    Re: Problem with lines (position) in textbox

    You do know that a Textbox has an "artificial" upper Limit of 64K Characters?
    One System to rule them all, One IDE to find them,
    One Code to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    For health reasons i try to avoid reading unformatted Code

  16. #16
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,171

    Re: Problem with lines (position) in textbox

    Beware...my post #9 works with down arrow as long as up arrow is not selected along the way....I just did a simple program to show how to scroll using the down arrow. You will have to modify it considerably to change for the up arrow function (especially when used in conjunction with the down arrow).

    @Z...."I" know...and that is why I wondered simply why OP is not using a listbox...SO much easier for what he seems to want to do! I don't see why changing out from the ML textbox to a listbox should be a problem (UNLESS THIS IS A HOMEWORK ASSIGNMENT!!!)

  17. #17
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    1,970

    Re: Problem with lines (position) in textbox

    What he's basically trying to "emulate" is a Memo-Textbox we know from different programs (like MySQL-Workbench etc.)
    Instead of a ListBox i would have recommended a RichTextBox
    One System to rule them all, One IDE to find them,
    One Code to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    For health reasons i try to avoid reading unformatted Code

  18. #18

    Thread Starter
    Junior Member
    Join Date
    Sep 2019
    Posts
    19

    Re: Problem with lines (position) in textbox

    Name:  exemple.PNG
Views: 109
Size:  16.9 KB

    This would be my intent!!



    And... Is not an HOMEWORK !!

    Simply:

    I'm looking for ways to do some things ...
    And I'm finding a lot of hitches !!


    I need to use this control (Textbox) and I can't use others (like listbox or richtextbox),
    because, for next things I must do, I have to do it with this!

    ---
    As you can understand: matching the buttons (for the first time) to the corresponding lines is very simple, but if the text scrolls, the button is no longer found in correspondence.
    I'm thinking of a check to make (in addition to your suggestions!).
    I found a way to count, how many lines are present (possibly present) in a Textbox, by
    his dimension:

    Private Function NLINESinTXTBX() As Long
    Dim frzHeight As Long

    frzHeight = Me.TextHeight("ABC")

    NLINESinTXTBX = (Text1.Height - 4 * Screen.TwipsPerPixelY) \ frzHeight
    End Function

    Private Sub Command1_Click()
    'Create Textbox1 and Textbox2
    Text2.Text = NLINESinTXTBX()
    End Sub


    ---

    My problems (in list) are:

    - the 1st I've propose now!
    - I need to select text by a specified point to end of text!
    - As you can see (on the up-right corner of image), in text (that
    I put in second moment like copy-paste) there are some 'null'
    characters, that I'm not able to remove in any ways!!
    I tryed with all possible trim process... Nothing to do.

    (I pass this text, from a file, in textbox, or richtextbox...
    Secon moment: I put part of this text in other control, or in a
    Clipbord...
    When a recall this text, this type of characters " 𝓼𝓽𝓮 " -or similar-)

  19. #19
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,171

    Re: Problem with lines (position) in textbox

    Alright....I'm out of here...I do NOT understand a couple of things...1: What you really want, and 2: Why you are so adamant about using a textbox. After relooking at GUESSES at what you might be doing, I might recommend you GET RID OF THAT TEXTBOX and use a MSHFLexGrid.

  20. #20

    Thread Starter
    Junior Member
    Join Date
    Sep 2019
    Posts
    19

    Re: Problem with lines (position) in textbox

    Good!
    I keep going!

    I believe I have almost completed.

    There are things, that sometimes, one really doesn't think about, and instead: they are of an absurd ease!

    Anyway, I'll come back to it when I complete it!

  21. #21
    New Member
    Join Date
    Oct 2013
    Posts
    10

    Re: Problem with lines (position) in textbox

    was this what you were after?

    Code:
    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 Sub Command1_Click(Index As Integer)
        Dim line_idx As Long
        Dim char_idx As Long
        Dim char_cnt As Long
        Dim line_str As String
        
        Const EM_GETLINE = &HC4
        Const EM_LINEINDEX = &HBB
        Const EM_GETFIRSTVISIBLELINE = &HCE
        
        line_idx = SendMessage(Text1.hWnd, EM_GETFIRSTVISIBLELINE, -1&, 0&)
     
        line_idx = line_idx + Index    ' Index is 0 based line number
       
        char_idx = SendMessage(Text1.hWnd, EM_LINEINDEX, ByVal line_idx, 0&)
        
        If char_idx >= 0 Then
            line_str = Chr$(0) & Chr$(4) & Space(1024)
            char_cnt = SendMessage(Text1.hWnd, EM_GETLINE, ByVal line_idx, ByVal line_str)
            line_str = Left(line_str, char_cnt)
            
            Text1.SelStart = char_idx
            Text1.SelLength = Len(line_str)
            Text1.SetFocus
        End If
    End Sub

  22. #22
    New Member
    Join Date
    Oct 2013
    Posts
    10

    Re: Problem with lines (position) in textbox

    oops, should be
    Code:
    line_idx = SendMessage(Text1.hWnd, EM_GETFIRSTVISIBLELINE, 0&, 0&)

  23. #23

    Thread Starter
    Junior Member
    Join Date
    Sep 2019
    Posts
    19

    Re: Problem with lines (position) in textbox

    Here we are!
    This is what I needed.

    I just need to add a function to it when the text goes up, to complete everything.
    (Here I deactivated the Textbox).

    Ajsha, now I'm seeing your code, but thanks anyway. In fact I used something similar.

    I have attached a zip, with the small program filled in and a text file that is loaded, to show it.




    rename Sel Line by Button.txt to .exe


    (ps: I modified the file, because I could not attach it)
    (not a virus, rest assured!)
    Last edited by Shaggy Hiker; Sep 27th, 2019 at 10:16 PM.

  24. #24
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,203

    Re: Problem with lines (position) in textbox

    Anybody who downloads and runs an exe from this forum needs their head examined. The AUP specifically prohibits attaching compiled code. Feel free to attach the source code, but please remove any compiled code. We learned from experience.
    My usual boring signature: Nothing

  25. #25

    Thread Starter
    Junior Member
    Join Date
    Sep 2019
    Posts
    19

    Re: Problem with lines (position) in textbox

    Ah! Ok!
    I'm sorry, I thought, it wasn't so prohibitive, since it's easy to verify that it's not a virus.

    However, if someone can be interested (let me know), I will enter the code (after removing all unnecessary parts).

    (Ps: to the Moderator:
    you can see for yourself, that I did it in good faith ...
    If you start it in a sandbox, you will realize that it is nothing more than what was said previously!)

  26. #26
    Lively Member
    Join Date
    May 2017
    Posts
    69

    Re: Problem with lines (position) in textbox

    Quote Originally Posted by Daduq View Post
    Ah! Ok!
    I'm sorry, I thought, it wasn't so prohibitive, since it's easy to verify that it's not a virus.

    However, if someone can be interested (let me know), I will enter the code (after removing all unnecessary parts).

    (Ps: to the Moderator:
    you can see for yourself, that I did it in good faith ...
    If you start it in a sandbox, you will realize that it is nothing more than what was said previously!)
    Unfortunately it's NOT easy to verify it's not a virus or malware. Most virus checkers rely primarily on signatures of already known code for detection. A 'new' virus/malware could pass this check. Other levels of checking like heuristics is not 100% sucessfull. It may well detect a virus's replication attempts, but not simple malware program that does one-off damage.
    Try it your self, you can easily write a VB6 prog that deletes a few files in the documents folder for example, that won't trigger a virus warning.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width