[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?
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…
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.
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
Re: Replace a Multicolumn Listbox with a DataGridView?
Attachment 183620
sorry I'm not sure how to get a picture up...
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
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
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
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
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..