• 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("Provider = Microsoft.Jet.OLEDB.4.0; Data Source = " & dbasename)
objRecordset.Open("SELECT * FROM " & tblname & " ORDER BY DATEREP DESC, TIMEREP DESC", objConnection, adOpenStatic, _
adLockOptimistic)

lvref.Clear()
lvref.View = View.Details
lvref.Columns.Add("Ref", 40, HorizontalAlignment.Left)
lvref.Columns.Add("User", 75, HorizontalAlignment.Left)
lvref.Columns.Add("Asset", 100, HorizontalAlignment.Left)
lvref.Columns.Add("Fault", -2, HorizontalAlignment.Left)
lvref.Columns.Add("Assigned", 75, HorizontalAlignment.Left)
lvref.Columns.Add("Date", 50, HorizontalAlignment.Left)
lvref.Columns.Add("Time", 75, HorizontalAlignment.Left)
lvref.Columns.Add("Email", 40, 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

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("Provider = Microsoft.Jet.OLEDB.4.0; Data Source = " & dbasename)
objRecordset.Open("SELECT * FROM " & tblname & " ORDER BY DATEREP DESC, TIMEREP DESC", objConnection, adOpenStatic, _
adLockOptimistic)

lvref.Clear()
lvref.View = View.Details
lvref.Columns.Add("Ref", 40, HorizontalAlignment.Left)
lvref.Columns.Add("User", 75, HorizontalAlignment.Left)
lvref.Columns.Add("Asset", 100, HorizontalAlignment.Left)
lvref.Columns.Add("Fault", -2, HorizontalAlignment.Left)
lvref.Columns.Add("Assigned", 75, HorizontalAlignment.Left)
lvref.Columns.Add("Date", 50, HorizontalAlignment.Left)
lvref.Columns.Add("Time", 75, HorizontalAlignment.Left)
lvref.Columns.Add("Email", 40, 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("Provider = Microsoft.Jet.OLEDB.4.0; Data Source = " & dbasename)
objRecordset.Open("SELECT * FROM " & tblname & " ORDER BY DATEREP DESC, TIMEREP DESC", objConnection, adOpenStatic, adLockOptimistic)

DataGridView1.Columns.Clear()
DataGridView1.Columns.Add("Ref", "Ref")
DataGridView1.Columns.Add("User", "User")
DataGridView1.Columns.Add("Asset", "Asset")
DataGridView1.Columns.Add("Fault", "Fault")
DataGridView1.Columns.Add("Assigned", "Assigned")
DataGridView1.Columns.Add("Date", "Date")
DataGridView1.Columns.Add("Time", "Time")
DataGridView1.Columns.Add("Email", "Email")

' 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("Provider = Microsoft.Jet.OLEDB.4.0; Data Source = " & dbasename)
objRecordset.Open("SELECT * FROM " & 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.zip

OmgPonies.zip

This topic is now closed to further replies.
  • Posts

    • Are they marketed as an entry into astronomy or astrophotography? I do astrophotography. With big rigs, lots of computers, cables and headaches. I love it. And by learning this ridiculously complex hobby, I’ve learned about the objects I’m shooting. Astronomy followed from photography.
    • Microsoft confirms Recycle Bin bug across all versions of Windows by Usama Jawad A couple of days ago, we reported that the latest Patch Tuesday update has seemingly resulted in a lot of issues for many users, including OneDrive and Dropbox access problems, BitLocker recovery lockouts, and BSODs. Although Microsoft is yet to acknowledge these bugs, it has confirmed another, relatively smaller issue across all supported versions of Windows. In an update on its Windows Release Health Dashboard, Microsoft has confirmed that after installing June's Patch Tuesday update (KB5094126), you'll experience unexpected behavior when leveraging Recycle Bin. Basically, when you attempt to delete an item from the Recycle Bin, the confirm dialog will show you the internal file name of that content rather than the actual name. For example, the file may be named abc.png, but the confirm dialog will ask if you're sure that you want to permanently delete $Rxxxxx.png from the Recycle Bin. This is pretty much it for the scope of the bug itself; it just displays the wrong name in the confirm dialog. The correct name will be shown in the list view of the Recycle Bin and if you restore the file, it will return with the correct name as well. This issue affects pretty much all supported versions of Windows client and server, including: Client: Windows 11, version 26H1; Windows 11, version 25H2; Windows 11, version 24H2; Windows 11, version 23H2; Windows 10, version 22H2; Windows 10 Enterprise LTSC 2021; Windows 10 Enterprise LTSC 2019; Windows 10 Enterprise LTSB 2016 Server: Windows Server 2025; Windows Server 2022; Windows Server 2019; Windows Server 2016; Windows Server 2012 R2; Windows Server 2012 As things currently stand, Microsoft is working on a concrete solution that will be released in a "future" Windows update. It remains to be seen if the firm will wait till the next Patch Tuesday or roll out an out-of-band (OOB) fix. The good news is that commercial customers can deploy a workaround right now, but they will have to reach out to Microsoft Support for Business for additional details.
    • They said by this time everyone will have flying cars. WELL...
    • A study by physicist Henry Tye of Cornell University suggests that the universe may not expand forever. Instead, it could eventually stop expanding, begin contracting and end in a "Big Crunch" roughly 20 billion years from now. Maybe not as we now know that time can flow backwards.
    • Of course. Simply reverse the polarity.
  • Recent Achievements

    • Week One Done
      Jordan Smith earned a badge
      Week One Done
    • Reacting Well
      BizSAR earned a badge
      Reacting Well
    • First Post
      AndreaB earned a badge
      First Post
    • Week One Done
      Huge Trailer earned a badge
      Week One Done
    • Week One Done
      Classifyskilleducation earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      578
    2. 2
      +Edouard
      184
    3. 3
      PsYcHoKiLLa
      75
    4. 4
      Michael Scrip
      72
    5. 5
      neufuse
      64
  • Tell a friend

    Love Neowin? Tell a friend!