CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Sep 2001
    Posts
    254

    listbox two flds

    i need to use listbox, but i need it to display 2 fields extracted from table.
    when i click the listbox it should only take the 1 field to store in a textbox.

    any suggestion

    tks
    cyrus

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: listbox two flds

    To get the selected Item from the Listbox into the Textbox, all you need is :
    Code:
    Private Sub Command1_Click()
    Text1.Text = List1.List(List1.ListIndex)
    End Sub

  3. #3
    Join Date
    Sep 2001
    Location
    South Africa
    Posts
    186

    Re: listbox two flds

    What type of data will be in the two fields? Fixed or variable length?

    Do you want the first or the second field returned into textbox?

  4. #4
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    1,901

    Re: listbox two flds

    Ideally the data you want to add to the List Box is reasonably predictable, otherwise you will end up with a ListBox not quite lined up

    To separate 2 fields you should use a special character like a TAB

    So If Field1 is A1234 and Field2 is Widgets, by doing

    ListBox1.AddItem Field1 & Chr(9) & Field2 you will get an entry in the listbox looking approximately like

    A1234 Widgets

    By doing as Hannes has suggested, you can extract the value into either a Textbox or a string, then strip out the left and right values (which have been split by the tab)

    This is done using the SPLIT Command

    Example
    Code:
    Public LineData As String
    Public LineVar() As String
     
    Public Sub SplitStringOnTab
        LineData = ListBox1.Text                 'the Item Clicked in the ListBox which has 2 fields separated by the TAB
        LineVar = Split(LineData, CHR(9))    'this splits the line which is separated by the TAB (CHR(9)
        Field1 = LineVar(0)
        Field2 = LineVar(1) 
    End Sub

  5. #5
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: listbox two flds

    Better idea. Use an API:
    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 LB_SETTABSTOPS = &H192
    
    Private Sub Form_Load()
       ReDim TabStop(0 To 2) As Long
        'assign some values to the tabs for the second third and fourth
      'column (the first is flush against the listbox edge)
       TabStop(0) = 90
       TabStop(1) = 130
       TabStop(2) = 185
      
      'clear then set the tabs
       Call SendMessage(List1.hwnd, LB_SETTABSTOPS, 0&, ByVal 0&)
       Call SendMessage(List1.hwnd, LB_SETTABSTOPS, 3, TabStop(0))
        List1.AddItem "Big" & Chr(9) & "Brown" & Chr(9) & "Dog"
        List1.AddItem "Brown" & Chr(9) & "Big" & Chr(9) & "Dog"
        List1.AddItem "Dog" & Chr(9) & "Brown" & Chr(9) & "Big"
        List1.Refresh
    End Sub
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  6. #6
    Join Date
    Sep 2001
    Posts
    254

    Re: listbox two flds

    Quote Originally Posted by Bezzie
    What type of data will be in the two fields? Fixed or variable length?

    Do you want the first or the second field returned into textbox?

    both are string and fix, first element to return to textbox

  7. #7
    Join Date
    Sep 2001
    Location
    South Africa
    Posts
    186

    Re: listbox two flds

    Select your recordset (RS)
    Populate your Listbox:
    Code:
     Do While Not RS.EOF
    
    List1.AddItem RS("Field1") & Chr(9) & RS("Field2") 'the fields in your recordset separated with TAB character RS.MoveNext
    Loop
    BTW: Can someone tell me how to resize this Code blocks to show more code!

    In List1.Click event extract the part you want
    Code:
    Text1.Text = Left(List1.List(List1.ListIndex),10) 'extract the number of chars of field1
    Last edited by Bezzie; September 4th, 2008 at 08:50 AM.

  8. #8
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: listbox two flds

    Quote Originally Posted by Bezzie
    Select your recordset (RS)
    Populate your Listbox:
    Code:
     Do While Not RS.EOF
    
    List1.AddItem RS("Field1") & Chr(9) & RS("Field2") 'the fields in your recordset separated with TAB character RS.MoveNext
    Loop
    BTW: Can someone tell me how to resize this Code blocks to show more code!

    In List1.Click event extract the part you want
    Code:
    Text1.Text = Left(List1.List(List1.ListIndex),10) 'extract the number of chars of field1

    Did you try my code? It sets the tab stops!
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  9. #9
    Join Date
    Sep 2001
    Posts
    254

    Re: listbox two flds [resolve]

    thanks all gurus, i've use the split(.......) and works fine for me

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

Featured