How To Highlight The 1St Row in A Listview VB.net

SOLVED Discussion in 'Web Design & Coding' started by Comptech, Dec 12, 2011.

  1. Comptech

    Comptech ICT Technican

    Joined:
    Dec 11, 2011
    Messages:
    16
    Likes Received:
    14
    Location:
    UK
    Code under Load Event

    Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ListView1.View = View.Details
    ListView1.HideSelection = False
    ListView1.FullRowSelect = True
    Dim lCount As Integer
    ListView1.Columns.Add("No", 100)
    For lCount = 1 To 5
    ListView1.Items.Add(lCount.ToString)
    Next
    If ListView1.Items.Count > 0 Then
    ListView1.Items(0).Selected = True
    End If
    ListView1.View = View.Details
    ListView1.HideSelection = False
    ListView1.FullRowSelect = True

    ListView1.Columns.Add("No", 100)
    For lCount = 1 To 5
    ListView1.Items.Add(lCount.ToString)
    Next

    If ListView1.Items.Count > 0 Then
    ListView1.Items(0).Selected = True
    End If
    End Sub
    End Class

    Hope this helps someone

    Kind Regards

    Bradley
     
    Comptech, Dec 12, 2011
    #1
    Zeus likes this.
    1. Advertisements

  2. Comptech

    AceInfinity Somewhere...

    Joined:
    Aug 13, 2012
    Messages:
    7
    Likes Received:
    0
    Location:
    Canada
    You have duplicate code here, just to point it out:
    Code:
    ListView1.View = View.Details
    ListView1.HideSelection = False
    ListView1.FullRowSelect = True
    
    ListView1.Columns.Add("No", 100)
    For lCount = 1 To 5
    	ListView1.Items.Add(lCount.ToString)
    Next
    
    If ListView1.Items.Count > 0 Then
    	ListView1.Items(0).Selected = True
    End If
    Also if you do this:
    Code:
    For lCount As Integer = 1 To 5
    Instead of declaring it's type outside of that For loop, it's much better because the scope of the variable only extends to as far as you need it in this case; only for the loop, and not the full Sub.

    Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    	With ListView1
    		.View = View.Details
    		.HideSelection = False
    		.FullRowSelect = True
    	End With
    
    	ListView1.Columns.Add("No", 100)
    	For lCount As Integer = 1 To 5
    		ListView1.Items.Add(lCount.ToString)
    	Next
    
    	If ListView1.Items.Count > 0 Then
    		ListView1.Items(0).Selected = True
    	End If
    End Sub
    Or more technical work with LINQ (Assuming .NET 3.5 or higher):
    Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    	With ListView1
    		.View = View.Details
    		.HideSelection = False
    		.FullRowSelect = True
    	End With
    
    	ListView1.Columns.Add("No", 100)
    	Array.ForEach(Enumerable.Range(1, 5).ToArray, Sub(i) ListView1.Items.Add(CStr(i)))
    
    	If ListView1.Items.Count > 0 Then _
    		ListView1.Items(0).Selected = True
    End Sub
     
    AceInfinity, Aug 13, 2012
    #2
    1. Advertisements

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Similar Threads
  1. spike228

    can't highlight text with firefox

    spike228, Aug 19, 2005, in forum: Networking and Internet
    Replies:
    2
    Views:
    2,752
    spike228
    Aug 20, 2005
  2. S Walch

    Highlight table cells with a background Image

    S Walch, Apr 27, 2006, in forum: Web Design & Coding
    Replies:
    8
    Views:
    1,405
    S Walch
    Apr 27, 2006
  3. Your Name
    Replies:
    0
    Views:
    412
    Your Name
    Mar 21, 2009
  4. Nick O'Tyme
    Replies:
    0
    Views:
    1,526
    Nick O'Tyme
    Feb 19, 2008
  5. Dr. Sir John Howard, AC, WSCMoF

    Row over web blacklist. Just another KRudd and labour f*k-up.

    Dr. Sir John Howard, AC, WSCMoF, Feb 24, 2009, in forum: Computing
    Replies:
    0
    Views:
    532
    Dr. Sir John Howard, AC, WSCMoF
    Feb 24, 2009
  6. Samson

    4 Bad Motherboards in a row?

    Samson, Sep 6, 2006, in forum: PC Hardware
    Replies:
    21
    Views:
    642
    Jan Alter
    Sep 8, 2006
  7. Comptech

    SOLVED How To Do A Array For A Listview Table

    Comptech, Dec 13, 2011, in forum: Web Design & Coding
    Replies:
    1
    Views:
    774
    AceInfinity
    Aug 13, 2012
  8. JF Mezei

    Highlight colour on iPhone

    JF Mezei, Sep 23, 2014, in forum: iPhone
    Replies:
    1
    Views:
    221
    Your Name
    Sep 25, 2014
Loading...