Search code examples
vb6

Visual Basic 6 - when label changes, add/remove item in list


I am currently working on a Visual Basic 6 project called Starboy Notepad, which you can check on Github. I ran into a problem.

I have a RichTextBox1, Label1, and List1 controls. I've set that, every time I add a line to my RichTextBox1, the text in Label1 changes, displaying the number of lines in the RichTextBox1.

What I would want to do is that, in the List1, there would be added a time every time a line in RichTextBox1 gets added, and every time a line in RichTextBox1 gets removed, the last item in List1 gets removed too.

Basically, I want to do the line count you see on the left side in programs such as Notepad++ or Notepads.

Any help would be greatly appreciated :D (and credited)

Code about the line counter:

Public Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lparam As Any) As Long
Public Const EM_GETLINECOUNT As Long = &HBA
Public Sub linecount()
Dim linecount As Integer
  linecount = SendMessage(Form1.RichTextBox1.hwnd, _
                  EM_GETLINECOUNT, 0, 0)
Form1.Label1.Caption = linecount
Form1.List1.AddItem (linecount)
End Sub


Public Sub linecount2()
Dim linecount As Integer
  linecount = SendMessage(Form1.RichTextBox1.hwnd, _
                  EM_GETLINECOUNT, 0, 0)
Form1.Label1.Caption = linecount
End Sub

In the RichTextBox:

Private Sub RichTextBox1_Change()

On Error Resume Next
Dim KeyCode As Integer
Form1.KeyPreview = True
If KeyCode = vbKeyReturn Then linecount
linecount2

Like this it does work, aka every time I do press Enter, it works. But every time I do remove one line, it doesn't remove the last item from the List1 item.


Solution

  • If I'm correct. You have a side panel to identify each line in the file.

    This panel always needs to be updated. Imagine the situation of a middle line being removed. In your case you can simply remove all numbers and add them again.

    Private Sub UpdateLines(linecount As Integer)
        Dim i As Integer
    
        List1.Clear
        For i = 1 To linecount
            List1.AddItem (i)
        Next
    End Sub