• 0

Formatting a listview item


Question

Hey people!

I have pulled information from an access table into a listview item within Visual Basic 2010. It displays all the information, field by field how I want it...however one of the fields has a lot of text. It stops after the specified lenght with ... suggesting there is more text that doesn't fit. I have used "-2" to make it longer, but then it's too long. Also scrolling wouldn't be very nice.

I'd like it to display the information from the field on two lines, but I can't figure it out. Here's my existing text (without the dims as I've made it flat at the moment)


objConnection = CreateObject("ADODB.Connection")
objRecordset = CreateObject("ADODB.Recordset")
dbasename = "D:\technicianmanagement.MDB"
tblname = "FaultCalls"
objConnection.Open("ProviderMiMicrosoft.Jet.OLEDB.4.0; Data Source = " & dbasename)
objRecordset.Open("SELECTOM " & tblname & " ORDER BY DATEREP DESC, TIMEREP DESC", objConnection, adOpenStatic, _
adLockOptimistic)

lvref.Clear()
lvref.View = View.Details
lvref.Columns.Add("Ref, HorizontalAlignment.Left)
lvref.Columns.Add("User, HorizontalAlignment.Left)
lvref.Columns.Add("Asset0, HorizontalAlignment.Left)
lvref.Columns.Add("Fault, HorizontalAlignment.Left)
lvref.Columns.Add("Assigned, HorizontalAlignment.Left)
lvref.Columns.Add("Date, HorizontalAlignment.Left)
lvref.Columns.Add("Time, HorizontalAlignment.Left)
lvref.Columns.Add("Email, HorizontalAlignment.Left)

Do While Not objRecordset.EOF

MyListText(0) = sID
MyListText(1) = sUser
MyListText(2) = sAsset
MyListText(3) = sFault
MyListText(4) = sAssigned
MyListText(5) = sDate
MyListText(6) = sTime
If sEmail = True Then MyListText(7) = "Yes" Else MyListText(7) = "No"
MyListItem = New ListViewItem(MyListText)
lvref.Items.Add(MyListItem)
objRecordset.MoveNext()
Loop
[/CODE]

Link to comment
https://www.neowin.net/forum/topic/1059766-formatting-a-listview-item/
Share on other sites

9 answers to this question

Recommended Posts

  • 0

The problem is that the included listview control does not support multiple lines. (sorry) The general view is that the user will expand the column so that they can see the entire text.

I did a quick search and there are comments about 3rd party controls that allow multi line, but this is an unstandard pattern so be aware.

Sorry I can't give you the answer you wanted. I would really rethink the amount of data that you are showing.

  • 0
  On 23/02/2012 at 14:24, Daedroth said:

Hey people!

I have pulled information from an access table into a listview item within Visual Basic 2010. It displays all the information, field by field how I want it...however one of the fields has a lot of text. It stops after the specified lenght with ... suggesting there is more text that doesn't fit. I have used "-2" to make it longer, but then it's too long. Also scrolling wouldn't be very nice.

I'd like it to display the information from the field on two lines, but I can't figure it out. Here's my existing text (without the dims as I've made it flat at the moment)


objConnection = CreateObject("ADODB.Connection")
objRecordset = CreateObject("ADODB.Recordset")
dbasename = "D:\technicianmanagement.MDB"
tblname = "FaultCalls"
objConnection.Open("ProviderMiMicrosoft.Jet.OLEDB.4.0; Data Source = " & dbasename)
objRecordset.Open("SELECTOM " & tblname & " ORDER BY DATEREP DESC, TIMEREP DESC", objConnection, adOpenStatic, _
adLockOptimistic)

lvref.Clear()
lvref.View = View.Details
lvref.Columns.Add("Ref, HorizontalAlignment.Left)
lvref.Columns.Add("User, HorizontalAlignment.Left)
lvref.Columns.Add("Asset0, HorizontalAlignment.Left)
lvref.Columns.Add("Fault, HorizontalAlignment.Left)
lvref.Columns.Add("Assigned, HorizontalAlignment.Left)
lvref.Columns.Add("Date, HorizontalAlignment.Left)
lvref.Columns.Add("Time, HorizontalAlignment.Left)
lvref.Columns.Add("Email, HorizontalAlignment.Left)

Do While Not objRecordset.EOF

MyListText(0) = sID
MyListText(1) = sUser
MyListText(2) = sAsset
MyListText(3) = sFault
MyListText(4) = sAssigned
MyListText(5) = sDate
MyListText(6) = sTime
If sEmail = True Then MyListText(7) = "Yes" Else MyListText(7) = "No"
MyListItem = New ListViewItem(MyListText)
lvref.Items.Add(MyListItem)
objRecordset.MoveNext()
Loop
[/CODE]

Not sure why you dont use DataGridView.

you can do DataGridView.Columns.Add([font=monospace]"Ref") [/font]DataGridView.Columns.Add([font=monospace]"User") [/font]DataGridView.Columns.Add([font=monospace]"Asset")[/font] etc.

[font=monospace]and then for the items you can do DataGridView.Rows.Add([/font]sID, sUser, sAsset, ...)

p.s. my VB.NET skills are rusty as I've been doing C# for a while now so ignore semicolons where not required ;p

  • 0


objConnection = CreateObject("ADODB.Connection")
objRecordset = CreateObject("ADODB.Recordset")
dbasename = "D:\technicianmanagement.MDB"
tblname = "FaultCalls"
objConnection.Open("ProviderMiMicrosoft.Jet.OLEDB.4.0; Data Source = " & dbasename)
objRecordset.Open("SELECTOM " & tblname & " ORDER BY DATEREP DESC, TIMEREP DESC", objConnection, adOpenStatic, adLockOptimistic)

DataGridView1.Columns.Clear()
DataGridView1.Columns.Add("Refef")
DataGridView1.Columns.Add("Userser")
DataGridView1.Columns.Add("Assetsset")
DataGridView1.Columns.Add("Faultault")
DataGridView1.Columns.Add("Assignedssigned")
DataGridView1.Columns.Add("Dateate")
DataGridView1.Columns.Add("Timeime")
DataGridView1.Columns.Add("Emailmail")

' Makes the form stretch to show all the columns that are visible.
Me.Width = DataGridView1.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) + 60

' Clear all current rows of data.
DataGridView1.Rows.Clear()

Do While Not objRecordset.EOF
' You would probably want to do something like this... I put an in-line if statement to shrink the amount of code needed.
' The first parameter if If() is the expression, second is what to do if it is true and the third is what to do if it is false.
DataGridView1.Rows.Add(sID, sUser, sAsset, sFault, sAssigned, sDate, sTime, If(sEmail, "Yes", "No"))
Loop
[/CODE]

That should help you along.

Or... for more column size control etc.

[CODE]
objConnection = CreateObject("ADODB.Connection")
objRecordset = CreateObject("ADODB.Recordset")
dbasename = "D:\technicianmanagement.MDB"
tblname = "FaultCalls"
objConnection.Open("ProviderMiMicrosoft.Jet.OLEDB.4.0; Data Source = " & dbasename)
objRecordset.Open("SELECTOM " & tblname & " ORDER BY DATEREP DESC, TIMEREP DESC", objConnection, adOpenStatic, adLockOptimistic)

DataGridView1.Columns.Clear()

Dim DGVC(7) As DataGridViewTextBoxColumn
DGVC(0) = New DataGridViewTextBoxColumn
DGVC(0).HeaderText = "Ref"
DGVC(0).Name = "Ref"
DGVC(0).Width = 40
DGVC(1) = New DataGridViewTextBoxColumn
DGVC(1).HeaderText = "User"
DGVC(1).Name = "User"
DGVC(1).Width = 75
DGVC(2) = New DataGridViewTextBoxColumn
DGVC(2).HeaderText = "Asset"
DGVC(2).Name = "Asset"
DGVC(2).Width = 100
DGVC(3) = New DataGridViewTextBoxColumn
DGVC(3).HeaderText = "Fault"
DGVC(3).Name = "Fault"
DGVC(3).Width = 140
DGVC(4) = New DataGridViewTextBoxColumn
DGVC(4).HeaderText = "Assigned"
DGVC(4).Name = "Assigned"
DGVC(4).Width = 75
DGVC(5) = New DataGridViewTextBoxColumn
DGVC(5).HeaderText = "Date"
DGVC(5).Name = "Date"
DGVC(5).Width = 50
DGVC(6) = New DataGridViewTextBoxColumn
DGVC(6).HeaderText = "Time"
DGVC(6).Name = "Time"
DGVC(6).Width = 75
DGVC(7) = New DataGridViewTextBoxColumn
DGVC(7).HeaderText = "Email"
DGVC(7).Name = "Email"
DGVC(7).Width = 40
DataGridView1.Columns.AddRange(DGVC)

' Makes the form stretch to show all the columns that are visible.
Me.Width = DataGridView1.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) + 60

' Clear all current rows of data.
DataGridView1.Rows.Clear()

Do While Not objRecordset.EOF
' You would probably want to do something like this... I put an in-line if statement to shrink the amount of code needed.
' The first parameter if If() is the expression, second is what to do if it is true and the third is what to do if it is false.
DataGridView1.Rows.Add(sID, sUser, sAsset, sFault, sAssigned, sDate, sTime, If(sEmail, "Yes", "No"))
Loop
[/CODE]

post-20891-0-97936800-1330062215.png

Edited by FuhrerDarqueSyde
  • 0

I decided against a DataGraidView for asthetics.

I found this command:

lvref.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent)[/CODE]

This resizes the field how I want, but again its too long. :(

Also, thanks for the help. I have added a datagridview for testing and comparison. Is there either way to make the datagridview or listview grow/shrink but keep ratios and autosize if the user resizes the form?

So if I resize the form, the text will go onto new lines, rather than the user having to use the scroll bar to view obscured text.

  • 0

Auto-Resizing: Me.DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells

Wrapping: Me.DataGridView1.RowsDefaultCellStyle.WrapMode = DataGridViewTriState.True

Useful?

(Also, you say you've gone against the DGV for Aesthetics... you can style them to look identical to a list view if you so wished... but imo you can make them look an awful lot better too!)

  • 0

That's worked on some, but not all cells.

Is there any way to get it working for the rows too?

I have tried:

Me.dgvfaults.AutoResizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells[/CODE]

But that didn't work, as it says "'AutoResizeRowsMode' is not a member of 'System.Windows.Forms.DataGridView'"

Attached is what it looks like at the moment, but I'd like it so that it will resize with the form. Meaning it'll add extra lines if needed and shove the teext down.

post-395128-0-02155200-1330090307_thumb.

  • 0

After the loop can't you just do.


lvref.Columns(3).Width = -1
[/CODE]

That should resize it to the largest data item in the column. You may be able to set it to -1 before the loop and then it will do it as the data is added. My VB is very rusty so I can't remember exactly how it works (so rusty I could be way off as well)

  • 0

I've just programmed my first VB.NET in 2 years... here's a simple implementation of a ListBox with custom drawn mutli line items. Use it as you like...

(something went wrong, the attachment is attached twice)

OmgPonies.zipFetching info...

OmgPonies.zipFetching info...

This topic is now closed to further replies.
  • Posts

    • I’d be wanting to offload it fast too, wasted desk real estate.
    • AMD Ryzen 9600X 6-core AM5 CPU is just $185 and you get a free 512GB NVMe SSD too by Sayan Sen If you are on AMD's AM4 socket or older Intel and are looking to upgrade your processor, AMD has the Ryzen 9600X for just $185 (purchase link down below), plus you get a free NVMe SSD as well. The deal comes hot on the heels of Intel also offering the Core i5-14600K for as low as just $200, which includes a 240 mm AIO liquid cooler. Check that deal out in this article if you want to go Team Blue. The AMD Ryzen 9600X is based on the latest Zen 5 design and is the company's best chip to date. This desktop CPU has six cores and 12 threads; it competes with Intel's 12th Gen i7 for productivity performance, and is almost as good as the 14th Gen i7 for gaming. The SKU does not include a cooler and so you will need to buy one separately. The technical specifications of the Ryzen 5 9600X are given below: Architecture: Zen 5 Process Technology: TSMC 4nm FinFET manufacturing process Core Count: 6 cores Thread Count: 12 threads Base Clock Frequency: 3.9 GHz Max Boost Clock Frequency: 5.4 GHz Total Cache: 6 MB + 32 MB (L2 + L3) Thermal Design Power (TDP): 65W PCI Express Version: PCIe 5.0 28 lanes (usable: 24) Overclocking: Unlocked for overclocking TjMax: 95 C Platform Socket: AM5 Memory capacity support: max 192 GB DDR5 Memory Speed: 2x1R DDR5-5600, 2x2R DDR5-5600, 4x1R DDR5-3600, 4x2R DDR5-3600 Get it at the links below: AMD Ryzen 5 9600X (includes Radeon 2CU Integrated Graphics) - 100-100001405WOF: $184.99 (Shipped and Sold by Amazon US) | $189.99 (Shipped and Sold by Newegg US + free 512 GB NVMe SSD) This Amazon deal is US-specific and not available in other regions unless specified. If you don't like it or want to look at more options, check out the Amazon US deals page here. Get Prime (SNAP), Prime Video, Audible Plus or Kindle / Music Unlimited. Free for 30 days. As an Amazon Associate, we earn from qualifying purchases.
    • Hello, The general things are to make sure you have all of the latest Windows Updates installed as well as various device drivers for the computer's hardware components (video card, storage devices, etc.), as these can make a difference.  It might be small differences, but they can add up.  Case in point:  I was reading a while back about how in Windows 11 when you right-click on an object in File Explorer to bring up the context menu, it first enables to 3-D acceleration… to display a menu with text and maybe some 2-D icons.  Such behavior slows down the display of the context menu slightly.  If (and/or when) this gets fixed in Windows 11, context menus in File Explorer will appear much snappier. A few things you can try, in no particular order: Try toggling the full screen view of File Explorer on and off by pressing the F11 key twice.  This fixes another (different) bug like the one I mentioned above with Windows.  I don't recall if it has been fixed yet. Open the File Explorer, and look at the Panes section.  Click on the Navigation Pane button, and uncheck all of the options you do not need, like Navigation Pane, Show all folders, Show This PC, Show Network and Show Libraries.  You can also deselect (uncheck) the Preview pane and Details pane. In the Layout section, change the viewing option to either List or Details. if you use the Quick Access section in the left pane of the File Explorer, unpin (remove) any mappings to network drives, plus anything else you do not need or regularly access.  If you do not use it at all, consider unpinning everything there. In the File Explorer, select File → Options to bring up the Folder Options dialog and make the following changes: On the General tab, set Open File Explorer to This PC and below it in the Privacy section, uncheck the various Show… options and click on the Clear button. On the View tab, check the Always show icons, never thumbnails and the Launch folder windows in a separate process options.  Also on the View tab, uncheck the Display file size information in folder tips, Display the full path in the title bar, Restore previous folder windows at logon, Show preview handlers in preview pane and all of the options in the Navigation pane section. On the Search tab, scroll down the the When searching non-indexed locations section, and uncheck all of the items. Go to Settings → Network & internet → Advanced network settings → Advanced sharing settings and select the Private networks section.  In there, set Network discovery to On, enable (check) Set up network connected devices automatically, and set File and printer sharing to On.  Do not make any changes to the Public networks or All networks sections. Go to Settings → Privacy & security → Searching Windows and in the Find my files section, select (check) the Classic option.   Under the Classic option, select the Customize search locations option.  The Indexing Options window will appear.  Click on the Modify button to show the Indexed Locations window.  Click on the Show all locations button.  In the Change selection locations section at the top, remove any entries that say ""This location is currently unavailable" in them. Go back to the Indexing Options window, and click on the Advanced button.  The Advanced Options window will appear.  On the Index Settings tab, click the Rebuild tab. Other things to consider:  If you upgraded from Windows 10 to Windows 11 and/or cloned your boot drive (from a HDD to a SSD or a SSD to another SSD), wiping it and performing a clean installation of Windows 11 may improve performance, as legacy settings from the older hardware will no longer be in place.  If you are still using a hard disk drive, upgrading to a solid state drive will provide a noticeable performance improvement to all operations that involve accessing the drive. You can also try using third-party replacements for the Start Menu (Start 11, StartAllBack, etc.) and/or Windows File Explorer (Directory Opus, Double Commander, Explorer++, Files, FAR, Free Commander, Midnight Commander, Q-Dir, Total Commander, XYplorer ,etc.) to see if those perform better for you than Windows stock versions.  I don't think changing the antivirus software would have any effect, but it is something you could try as well, just to see if it does make any difference. Regards, Aryeh Goretsky  
    • I think it's bloody great, old chap!
  • Recent Achievements

    • Week One Done
      elsafaacompany earned a badge
      Week One Done
    • Week One Done
      Yianis earned a badge
      Week One Done
    • Veteran
      Travesty went up a rank
      Veteran
    • One Month Later
      somar86 earned a badge
      One Month Later
    • Week One Done
      somar86 earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      506
    2. 2
      ATLien_0
      260
    3. 3
      +Edouard
      186
    4. 4
      +FloatingFatMan
      177
    5. 5
      snowy owl
      132
  • Tell a friend

    Love Neowin? Tell a friend!