• 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

    • https://lunduke.locals.com/post/4387539/firefox-money-investigating-the-bizarre-finances-of-mozilla
    • The Start menu in Windows 11 is the Start menu I dislike the most.
    • Apple's AI models still trail behind OpenAI's GPT-4o despite latest update by Pradeep Viswanathan At WWDC 2025, Apple announced several updates related to Apple Intelligence for both developers and consumers. With the new Foundation Models framework, developers can now bring AI experiences to their apps that work offline in a privacy-preserving way and are available free of charge. The Foundation Models framework is built on Apple’s own in-house-developed AI models. Apple also unveiled a new generation of language foundation models. According to Apple, these updated models are faster, more efficient, and offer improved tool use, better reasoning capabilities, multimodal support for image and text inputs, and support for 15 languages. Apple Intelligence includes two foundation models: A 3-billion-parameter model that runs on-device using Apple Silicon. A server-based mixture-of-experts model optimized for Private Cloud Compute. Apple noted that the on-device 3B language model is not designed to be a general-purpose chatbot. Instead, it is intended to perform text-related tasks such as summarization, entity extraction, text understanding, refinement, short dialogues, and creative content generation, among others. The big question is how well Apple’s models perform compared to other leading models on the market. Rather than using standard AI benchmarks, Apple shared results from its own internal evaluations of fundamental language and reasoning capabilities. According to Apple’s text-based evaluations, its on-device 3B model performs favorably against the slightly larger Qwen-2.5-3B and competitively against the larger Qwen-3-4B and Gemma-3-4B in English. Its server-based model performs slightly better than Llama-4-Scout but falls short compared to Qwen-3-235B and OpenAI’s proprietary GPT-4o. In evaluations involving image input, Apple’s on-device model outperforms InternVL and Qwen, and performs competitively against Gemma. While Apple’s server model beats Qwen-2.5-VL, it underperforms when compared to Llama-4-Scout and GPT-4o. These results highlight how far Apple still has to go in foundational AI capabilities. It seems Apple compared its models to GPT-4o to make its performance appear relatively decent. If Apple were to compare its results against OpenAI’s latest O-series models or Google’s Gemini 2.5 Pro, the gap would likely appear much wider. It will be interesting to see how Apple navigates the AI era with its in-house capabilities in the years ahead.
  • Recent Achievements

    • Week One Done
      IAMFLUXX earned a badge
      Week One Done
    • One Month Later
      Æhund earned a badge
      One Month Later
    • One Month Later
      CoolRaoul earned a badge
      One Month Later
    • First Post
      Kurotama earned a badge
      First Post
    • Collaborator
      Carltonbar earned a badge
      Collaborator
  • Popular Contributors

    1. 1
      +primortal
      497
    2. 2
      ATLien_0
      268
    3. 3
      +FloatingFatMan
      229
    4. 4
      +Edouard
      199
    5. 5
      snowy owl
      151
  • Tell a friend

    Love Neowin? Tell a friend!