Results 1 to 9 of 9

Thread: Allow User To Add To ListBox

  1. #1

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    I am developing an application where the user has control over the contents a certain ListBox. The ListBox is not sorted. While it's easy to get the user's input from a TextBox and then to add its contents to the ListBox, I'd like an easy way for the user to be able to indicate where the new value should be inserted. I envision something like a fly-over arrow that would become visible when the mouse was over the listbox, and when the user decides that it is at the corrent position a right-mouse click would insert the value from the TextBox. It would also require that the listbox automatically scroll (if necessary) when the fly-over arrow was either at the top or bottom of the ListBox.

    Does anyone have code samples and/or other/better ways of doing it?

  2. #2
    Guest
    This code adds Text1's text where-ever you double click
    Code:
    Private Sub Form_Load()
        List1.AddItem "1"
        List1.AddItem "2"
        List1.AddItem "3"
        List1.AddItem "4"
        List1.AddItem "5"
        List1.AddItem "6"
    End Sub
    
    Private Sub List1_DblClick()
        List1.AddItem Text1.Text, List1.ListIndex
    End Sub
    I hope this was what you meant..
    sorry, I couldnt figure out how to add it with right-click.

  3. #3
    Guest
    I think I may have found something...

    Code:
    Dim StartDrag As Boolean
    
    Private Sub Form_Load()
    For i = 0 To 5
        List1.AddItem i
    Next i
    End Sub
    
    Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
      StartDrag = True
        strTemp = List1.Text
    End Sub
    
    
    Private Sub List1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
        If StartDrag = True Then
            List1.AddItem List1.Text
            List1.RemoveItem List1.ListIndex
        End If
        StartDrag = False
    End Sub
    Not sure if it's the best, but it works .

  4. #4
    Guest
    that is just for drag and drop, I think he wants it so you can choose were to put a new entry.

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Ok, what do you think about this?
    Code:
    Private Type POINTAPI
       X As Long
       Y As Long
    End Type
    Private Declare Function LBItemFromPt Lib "comctl32.dll" (ByVal hwnd As Long, ByVal ptx As Long, ByVal pty As Long, ByVal bAutoScroll As Long) As Long
    Private Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
    
    Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
        Dim a As POINTAPI, b As Long
        a.X = X \ Screen.TwipsPerPixelX
        a.Y = Y \ Screen.TwipsPerPixelY
        ClientToScreen List1.hwnd, a
        b = LBItemFromPt(List1.hwnd, a.X, a.Y, 1)
        If b = -1 Then
            List1.AddItem Text1
            List1.ListIndex = 0
        Else
            List1.AddItem Text1, b
            List1.ListIndex = b
        End If
    End Sub
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  6. #6
    Guest
    that is great!

    another undocumented API to add to the list!

  7. #7

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Thanks. kedaman's solution is essentially what I want, but I would also really like to be able to give the user a visual clue as to where the new value will be inserted, so for example with kedaman's code, the user would know that the new value would be added before the clicked listbox entry and not after it. What I'd like is an arrow that points to the dividing line between two listbox entrys.

  8. #8
    Guest
    I am not sure if you have it yet, but you could add a blank listbox entry ( " " ), then when the user clicks, you could remove it, and replace it with the text.

  9. #9
    Guest
    Try this. I have modified Kedaman's code slightly (renamed the variables) and I added some of my own as well. Basically, this will draw a line under the current item.

    Code:
    Private Declare Function MoveToEx Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long, lpPoint As POINTAPI) As Long
    Private Declare Function LineTo Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long) As Long
    Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function Ellipse Lib "gdi32" (ByVal hdc As Long, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
    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 Declare Function Rectangle Lib "gdi32" (ByVal hdc As Long, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
    Private Declare Function LBItemFromPt Lib "comctl32.dll" (ByVal hwnd As Long, ByVal ptx As Long, ByVal pty As Long, ByVal bAutoScroll As Long) As Long
    Private Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
    
    Private Type RECT
            Left As Long
            Top As Long
            Right As Long
            Bottom As Long
    End Type
    
    Private Type POINTAPI
            X As Long
            Y As Long
    End Type
    
    Const LB_GETITEMRECT = &H198
    Const WM_PAINT = &HF
    
    
    Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
        List1.Refresh
        Dim PT As POINTAPI
        Dim LB_ITEM As Long
        PT.X = X \ Screen.TwipsPerPixelX
        PT.Y = Y \ Screen.TwipsPerPixelY
        ClientToScreen List1.hwnd, PT
        LB_ITEM = LBItemFromPt(List1.hwnd, PT.X, PT.Y, 1)
        If LB_ITEM = -1 Then
            List1.AddItem Text1
            List1.ListIndex = 0
        Else
            List1.AddItem Text1, LB_ITEM
            List1.ListIndex = LB_ITEM
        End If
    End Sub
    
    Private Sub List1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
        If List1.ListCount = 0 Then Exit Sub
        Dim PT As POINTAPI
        Dim LB_ITEM As Long
        Dim LB_HDC As Long
        Dim LB_RECT As RECT
        
        LB_HDC = GetWindowDC(List1.hwnd)
        
        List1.Refresh
        PT.X = X \ Screen.TwipsPerPixelX
        PT.Y = Y \ Screen.TwipsPerPixelY
        ClientToScreen List1.hwnd, PT
        LB_ITEM = LBItemFromPt(List1.hwnd, PT.X, PT.Y, 1)
        
        SendMessage List1.hwnd, LB_GETITEMRECT, LB_ITEM, LB_RECT
        MoveToEx LB_HDC, LB_RECT.Right, LB_RECT.Bottom, PT
        LineTo LB_HDC, LB_RECT.Left, LB_RECT.Bottom
        MoveToEx LB_HDC, LB_RECT.Right, LB_RECT.Bottom - 1, PT
        LineTo LB_HDC, LB_RECT.Left, LB_RECT.Bottom - 1
    End Sub

Posting Permissions

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



Click Here to Expand Forum to Full Width