• 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

    • Do you have a 365 account? I should have been more clear, I mean free accounts.
    • What?! "May 31 2024 knowledge cutoff"?
    • Amazon Alexa+ now has more than a million users by Aditya Tiwari Amazon's muscled-up voice assistant, Alexa+, has reached a new milestone. A company spokesperson told The Verge that Alexa+ has now crossed one million users. The e-commerce giant introduced Alexa+ earlier this year as its generative AI offering. Why? It's a new trend, and everyone is doing it. According to the company, Alexa Plus offers more natural and free-flowing conversations than its predecessor. You can speak half-formed thoughts using colloquial expressions, and the AI assistant should be able to understand you and provide an answer. Announcing its capabilities, Amazon previously said that you will be able to start a conversation on your Echo device and continue it on your phone, car, or computer. One million may not be a significant number when comparing it with the number of Alexa-enabled devices out there. Amazon revealed earlier this year that there are over 600 million Alexa devices globally. However, the number of Alexa+ users has increased from 'hundreds of thousands' in the previous month. The user base is not as big as that of other names like Gemini and ChatGPT because Amazon is still offering the generative AI assistant through an Early Access program, available to Prime and non-Prime members who own a compatible Echo device. We can find social media posts from different users who have been invited to try Alexa+. While there have been positive reviews from some, the road isn't buttery smooth for others. One user claimed that the early access Alexa+ has problems accessing some temperature sensors the previous version of Alexa would. "I also really dislike how it confidently will tell me something that is incorrect now instead of just saying it doesn't know like it used to tell me," the user added. The upgraded AI voice assistant will cost $19.99 per month, but is being offered for free to Prime subscribers. Alexa+ started rolling out in the US as part of its early access program. One reason why Amazon is giving Alexa+ a slow rollout is that the new devices and services chief, Panos Panay, wants to eliminate all the problems related to the generative AI assistant. Amazon's spokesperson told the publication that the early access program doesn't include features like brainstorming gift ideas, scheduling your next spa visit, ordering groceries hands-free, and jumping to your favorite scene on Fire TV. The program also doesn't offer the "new browser-based experience at Alexa.com," which would put Amazon's AI assistant in line with ChatGPT and Gemini. These missing features will be added in the coming weeks and months, as per the spokesperson, adding that almost 90% of the features are now a part of early access.
    • MSI's 32-inch 4K QD-OLED gaming monitor gets a big price cut for UK gamers and professionals by Paul Hill If you’re a gamer in the UK and looking for a monitor to upgrade to then check out the MSI MPG 321URX QD-OLED 31.5 Inch 4K UHD Gaming Monitor which you can now pick up for just 75% of its recommended retail price. The RRP of this monitor is £1,199, but thanks to this deal, you can get it for just £898.99 for a limited time (purchase link down below). With its 4K display, 240Hz refresh rate, and 0.03ms GTG, you’ll have the edge over other gamers by avoiding lag. At 31.5-inches, it’s the ideal monitor size if you’re sitting up close to it at a desk, you don’t want it too big at such a short range, but you also want to be able to see all the image details so 31.5-inches is a good balance. What makes QD-OLED stand out? There are loads of terms used to describe displays such as AMOLED, OLED, LED, and it can all get a bit confusing. This monitor adds yet another acronym called QD-OLED, which stands for Quantum Dot OLED. For you as a buyer, this means your new monitor has self-emitting pixels that deliver great black levels. It also features an enhanced sub-pixel arrangement for extra sharpness. The 31.5-inch 4K UHD monitor has a 3,840 x 2,160 pixel resolution making it ideal for playing games, but also watching movies in the best quality. Other important features worth mentioning are the 1.07 billion colors (10-bit) that the monitor can produce, its 99% DCI-P3 support, and DisplayHDR True Black 400 certification. All of these things make the monitor produce more accurate colours, potentially making it a good choice for professionals editing videos and photos too. Obviously, games will look good too. MSI has also packed in a fanless graphene heatsink which should help to increase the durability of the monitor long-term. This could extend the time until you need to buy a new monitor, further justifying its almost £900 price tag. Gaming and productivity features It’s not just the hardware that makes this monitor excel for gaming, it also comes with great software enhancements and connectivity options. On the software side, you get the following features: Smart Crosshair: Projects a customizable crosshair onto the screen to improve hip-fire accuracy and iron sights in first-person shooter games. Optix Scope: Gives you a built-in aim magnifier with multi-stage zooming and shortcut keys to quickly switch magnification levels. AI Vision: This automatically enhances brightness and colour saturation, particularly in dark areas of the screen, making it easier to see enemies hiding in shadows or dark corners. If you have two separate systems you want to connect to the monitor at once, you can do so with this monitor thanks to KVM support. You can view both sources with Picture-in-Picture and Picture-by-Picture modes. The MSI MPG 321URX QD-OLED 31.5 Inch 4K UHD Gaming Monitor also supports next-gen consoles with features like HDMI CEC Profile Sync, HDMI Variable Refresh Rate (VRR), and 4K:4K downscaling. In terms of connectivity and ergonomics, you get DisplayPort 1.4a, 2x HDMI 2.1 (CEC), USB Type-C with 90W power delivery, and a USB hub. The monitor uses a tilt-, swivel- & height-adjustable stand that is VESA compatible. Should you buy this monitor? The MSI MPG 321URX QD-OLED 31.5 Inch 4K UHD Gaming Monitor is definitely a product for serious gamers looking for top-tier visual fidelity and performance or content creators who need accurate colours and high resolution. Even with the significant discount, it’s still at a premium price and definitely not for everyone. If you are in one of the groups mentioned, then you should give serious consideration to buying the MSI MPG 321URX QD-OLED 31.5 Inch 4K UHD Gaming Monitor as it's the lowest price the monitor has been at on Amazon to date. MSI MPG 321URX QD-OLED 31.5 Inch 4K Gaming Monitor: £898.99 (Amazon UK) / RRP £1,199 This Amazon deal is U.K. 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 UK deals page here. Get Prime, Prime Video, Music Unlimited, Audible or Kindle Unlimited, free for the first 30 days As an Amazon Associate we earn from qualifying purchases.
  • Recent Achievements

    • Enthusiast
      computerdave91111 went up a rank
      Enthusiast
    • Week One Done
      Falisha Manpower earned a badge
      Week One Done
    • One Month Later
      elsa777 earned a badge
      One Month Later
    • Week One Done
      elsa777 earned a badge
      Week One Done
    • First Post
      K Dorman earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      533
    2. 2
      ATLien_0
      273
    3. 3
      +FloatingFatMan
      201
    4. 4
      +Edouard
      200
    5. 5
      snowy owl
      138
  • Tell a friend

    Love Neowin? Tell a friend!