Results 1 to 10 of 10

Thread: [RESOLVED] Replace a Multicolumn Listbox with a DataGridView?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2022
    Posts
    5

    Resolved [RESOLVED] Replace a Multicolumn Listbox with a DataGridView?

    Hi everyone, I hope someone can help me with this question... I'm using vb.net 2019 and I have a password program which uses a bound multicolumn Listbox to show all records of the "SiteName" field of an Access DB. Everything is working fine except the listbox layout looks rubbish with no padding around each value. Would it be possible to use a DataGridView instead to display all "SiteName" values? eg a 6 x 4 DatagridView populated with only "SiteName" values?

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Replace a Multicolumn Listbox with a DataGridView?

    Yes it’s possible to use a DGV, but what is a multi-column listbox? We’re going to need to see your code, and a screenshot if possible…

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Replace a Multicolumn Listbox with a DataGridView?

    It might be easier for us to show you how to improve your ‘ListBox’ layout and appearance.

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2022
    Posts
    5

    Re: Replace a Multicolumn Listbox with a DataGridView?

    Hi Paul, there is not much code to show as the Listbox is bound to the DB and automatically populates it. Here is the listbox... all I really want to do is move the leftmost values away from the edge a bit. If you could show me how to do that, that would be great....Attachment 183619

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2022
    Posts
    5

    Re: Replace a Multicolumn Listbox with a DataGridView?

    Attachment 183620
    sorry I'm not sure how to get a picture up...

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Replace a Multicolumn Listbox with a DataGridView?

    Multi-column listboxes are not a standard part of vb.net. This is a winforms app, right? Have a look in your listbox’s design time properties. There should be a padding property

  7. #7

    Thread Starter
    New Member
    Join Date
    Jan 2022
    Posts
    5

    Re: Replace a Multicolumn Listbox with a DataGridView?

    Hi Paul, there is no padding property for my Listbox... i've no idea about winforms, the vb i'm using is part of Visual Studio 2019

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Replace a Multicolumn Listbox with a DataGridView?

    If you want to display your data in tabular form, try this...

    Code:
    Imports System.Runtime.InteropServices
    
    Public Class Form20
    
        <DllImport("user32.dll")> _
      Private Shared Function SendMessage( _
        ByVal hWnd As IntPtr, _
        ByVal wMsg As Integer, _
        ByVal wParam As IntPtr, _
        ByVal lParam As IntPtr) As Integer
        End Function
    
        Private Sub SetTabStops(ByVal listBox As ListBox, ByVal tabStops() As Integer)
            Const LB_SETTABSTOPS As Integer = &H192
            Dim pinnedValues As GCHandle
            pinnedValues = GCHandle.Alloc(tabStops, GCHandleType.Pinned)
            Dim ptr As IntPtr = pinnedValues.AddrOfPinnedObject()
            SendMessage(listBox.Handle, LB_SETTABSTOPS, New IntPtr(tabStops.Length), ptr)
            pinnedValues.Free()
            listBox.Refresh()
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ListBox1.Items.AddRange(New String() {"one", "two", "three", "four", "five"})
            For x As Integer = 0 To ListBox1.Items.Count - 1
                ListBox1.Items(x) = vbTab & ListBox1.Items(x).ToString & vbTab & ListBox1.Items(x).ToString
            Next
            SetTabStops(ListBox1, New Integer() {3, 35}) 'left offset 3 pixels, column1 width 35 pixels
        End Sub
    
    End Class
    Attachment 183623

    The item text is split into columns on vbTab, so it's important to format your item text to at least one tabstop for the left offset

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Replace a Multicolumn Listbox with a DataGridView?

    If you'd still prefer a DGV, instead of: ListBox1.DataSource = yourDataTable, replace it with a DGV: DataGridView1..DataSource = yourDataTable

  10. #10

    Thread Starter
    New Member
    Join Date
    Jan 2022
    Posts
    5

    Re: Replace a Multicolumn Listbox with a DataGridView?

    Hi Paul, thanks very much for helping me out with this, the Listbox example is too hard for me to understand just yet.. (maybe in another 20 years or so) the DataGridView1..DataSource = yourDataTable looks easy enough so i'm going to work with that. Thanks again..

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