|
|
|
|
|
|
|
Page Views :
|
16701
|
|
Downloads :
|
206
|
|
Rating :
|
Rate it
|
|
Level :
|
Intermediate
|
|
|
|
|
Download
Files:
|
|
|
|
|
|
|
|
|
|
|
Introduction
Have you wondered how you could show text in ListView control if it's empty, just like Microsoft does it in Outlook Express and in some other applications?
Background
OnPaint() won't work
If you're thinking about OnPaint() event, forget it. ListView control is just a wrapper around the control in ComCtl and it doesn't fire this event.
UserControl: 100% pure VB solution but complicated
Another clean solution would be to make your own UserControl with one ListView and one label on the top which could be visible only if ListView would be empty, but there is one little problem with column resizing which draws a dark line on the control which goes under the label control which doesn't look very professional. We could possibly cover whole listview background, but then we would find problems with not showing scrollbars... well, fixing one problem creates another one... and so on...
Using the code
Probably the best solution is to override WndProc method with something like this. If m.MSG == 20 listview calls function to redraw background, so we just draw some string on top of it if listView doesn't contain any items.
Protected Overrides Sub WndProc(ByRef m As Message) MyBase.WndProc(m) If m.Msg = 20 Then If Me.Items.Count = 0 Then _b = True Dim g As Graphics = Me.CreateGraphics() Dim w As Integer = (Me.Width - g.MeasureString(_msg, Me.Font).ToSize().Width)/2 g.DrawString(_msg, Me.Font, SystemBrushes.ControlText, w, 30) End If End If End Sub
Well now it does what we need but sooner or later we will find out that this solution is not perfect. If you will try to resize the whole control, or only one of the columns, or if you add a new item to the listView and part or whole of your text string will be outside of the column's area, listView will became pretty messy. Improved overrided WndProc method, which is fixing all those problems, is here.
Protected Overrides Sub WndProc(ByRef m As Message) MyBase.WndProc(m) If m.Msg = 20 Then If Me.Items.Count = 0 Then _b = True Dim g As Graphics = Me.CreateGraphics() Dim w As Integer = (Me.Width - g.MeasureString(_msg, Me.Font).ToSize().Width)/2 g.DrawString(_msg, Me.Font, SystemBrushes.ControlText, w, 30) Else If _b Then Me.Invalidate() _b = False End If End If End If If m.Msg = 4127 Then Me.Invalidate() End If End Sub
Yes, and we shouldn't forget to redraw the whole control on Resize event if listView is empty and showing our text string
Private Sub ListView2_Resize(ByVal sender As Object, ByVal e As EventArgs) If _b Then Invalidate() End If End Sub
That's it.
NOTE: THIS ARTICLE IS CONVERTED FROM C# TO VB.NET USING A CONVERSION TOOL. ORIGINAL ARTICLE CAN BE FOUND ON C# CORNER (WWW.C-SHARPCORNER.COM).
|
|
Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post
Here.
|
|
|
|
|
Login
to add your contents and source code to this article
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional
consulting company, our consultants are well-known experts in .NET and many of them
are MVPs, authors, and trainers. We specialize in Microsoft .NET development and
utilize Agile Development and Extreme Programming practices to provide fast pace
quick turnaround results. Our software development model is a mix of Agile Development,
traditional SDLC, and Waterfall models.
|
|
Click here to learn more about C# Consulting. |
|
|
|
|
|
|
|
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon.
Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees.
As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
|
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
|
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
|
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
|
|
|
|
|
|
|
|
|
|
|
|
|