Results 1 to 16 of 16

Thread: [RESOLVED] VB.Net Newbie - Listview Questions

  1. #1

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Resolved [RESOLVED] VB.Net Newbie - Listview Questions

    I finally am going to make a concerted effort to learn VB.Net. So I installed VB.Net 2005 Express on my machine and away I go. I think the best way for me to learn how to program in VB.Net is to do it in small bits. I decided to start with the Listview. I want to learn how to use a listview and to do it properly meaning using it the way it is suppose to be used. Please take a look at my code and let me know how I can improve it.

    VB Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         'TODO: This line of code loads data into the 'FrontDataSet.Copy_of_Features' table. You can move, or remove it, as needed.
    5.         Dim a As Integer
    6.         With ListView1
    7.             .View = View.Details
    8.         End With
    9.  
    10.         Call lvwInitialize(ListView1)
    11.         Call DoTheChart()
    12.         Call ResizeForm()
    13.     End Sub
    14.  
    15.  
    16.     Private Sub lvwInitialize(ByVal lvw As ListView)
    17.         Dim lvh As ColumnHeader
    18.         Dim a As Integer
    19.  
    20.         With lvw
    21.             .Clear()
    22.             .FullRowSelect = True
    23.  
    24.             With .Columns
    25.                 For a = 1 To 3
    26.                     lvh = .Add("Column " & a)
    27.                     lvh.Width = lvw.Width / 3
    28.                 Next a
    29.             End With
    30.         End With
    31.  
    32.     End Sub
    33.  
    34.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    35.         Call LoadData(ListView1)
    36.     End Sub
    37.  
    38.     Private Sub LoadData(ByVal lvw As ListView)
    39.         Dim lvi As ListViewItem
    40.         Dim i As Integer
    41.         Dim a As Integer
    42.  
    43.         With lvw
    44.             .FullRowSelect = True
    45.             .View = View.Details
    46.             .Items.Clear()
    47.  
    48.             For i = 0 To 10
    49.                 lvi = New ListViewItem()
    50.                 lvi.Text = String.Format("Col1-Row{0}", i)
    51.  
    52.                 For a = 1 To .Columns.Count
    53.                     lvi.SubItems.Add("") '<=== Why do I need this line?
    54.                     lvi.SubItems(a).Text = String.Format("Col" & a & "-Row{0}", i)
    55.                 Next a
    56.  
    57.                 .Items.Add(lvi)
    58.             Next i
    59.         End With
    60.  
    61.     End Sub
    62.  
    63.     Private Sub ResizeForm()
    64.         Dim lvw As ListView
    65.         Dim lvh As ColumnHeader
    66.         Dim a As Integer
    67.  
    68.         lvw = ListView1
    69.  
    70.         If Me.WindowState = FormWindowState.Minimized Then Exit Sub
    71.  
    72.         With lvw
    73.             .Top = 0
    74.             .Left = 0
    75.             .Width = ClientSize.Width - 10
    76.             .Height = (ClientSize.Height) - 30
    77.  
    78.             With .Columns
    79.                 For a = 0 To .Count - 1
    80.                     lvh = .Item(a)
    81.                     lvh.Width = lvw.Width / 3
    82.                 Next a
    83.             End With
    84.         End With
    85.  
    86.         With Button1
    87.             .Top = ClientSize.Height - .Height - 5
    88.         End With
    89.     End Sub
    90.  
    91.     Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
    92.         Call ResizeForm()
    93.     End Sub
    94.  
    95. End Class

    Thanks!
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  2. #2
    Frenzied Member vbdotnetboy's Avatar
    Join Date
    Jun 2004
    Location
    Lewisburg, PA "Next year Raiders in the Super Bowl"
    Posts
    1,310

    Re: VB.Net Newbie - Listview Questions

    well for one with the resize function all you have to do is set the listviews anchor or dock properties then adjust the column widths

    same with the button

    Derek - Using VS 2008 99% of the time and VS 2003 1% of the time

    Please Help Us To Save Ana

    ● Helpful Links: DNR TV | Awesome site for tips | Using ADO.NET to work with Excel | Xml Namespace 2.0 Framework Changes|Ultra High Security Password Generator | Mendhak's ADO.NET Tutorial
    ● Code Bank: Random Password Generator | Generic DbProviderFactory Access
    ● Site Work: Bottle Run Xtreme | Spaids Racing.com

    Company I work for - CSSI

    WHEN POSTING PLEASE INDICATE VERSION

    Please use vbcode tags or code tags when posting code
    [highlight=vb]ALL your code goes here[/highlight] or [code]ALL your code goes here[/code]

    If my post helped you in anyway... please be kind and give me some ratings

  3. #3

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: VB.Net Newbie - Listview Questions

    Quote Originally Posted by vbdotnetboy
    well for one with the resize function all you have to do is set the listviews anchor or dock properties then adjust the column widths

    same with the button

    I can't believe how easy that was! I used the Dock Property but I was unable to size the listview by using the Anchor Property, could you show me how?

    Thanks
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  4. #4
    Frenzied Member vbdotnetboy's Avatar
    Join Date
    Jun 2004
    Location
    Lewisburg, PA "Next year Raiders in the Super Bowl"
    Posts
    1,310

    Re: VB.Net Newbie - Listview Questions

    VB Code:
    1. Private Sub LoadData(ByVal lvw As ListView)
    2.         Dim lvi As ListViewItem
    3.         Dim i As Integer
    4.         Dim a As Integer
    5.  
    6.         With lvw
    7.             .FullRowSelect = True
    8.             .View = View.Details
    9.             .Items.Clear()
    10.  
    11.             For i = 0 To 10
    12.                 lvi = New ListViewItem()
    13.                 lvi.Text = String.Format("Col1-Row{0}", i)
    14.  
    15.                 'Do this instead
    16.                 For a = 1 To .Columns.Count
    17.                     lvi.SubItems.Add(String.Format("Col" & a & "-Row{0}", i))
    18.                 '    lvi.SubItems(a).Text = String.Format("Col" & a & "-Row{0}", i)
    19.                 Next
    20.  
    21.                 .Items.Add(lvi)
    22.             Next i
    23.         End With
    24.  
    25.     End Sub

    Derek - Using VS 2008 99% of the time and VS 2003 1% of the time

    Please Help Us To Save Ana

    ● Helpful Links: DNR TV | Awesome site for tips | Using ADO.NET to work with Excel | Xml Namespace 2.0 Framework Changes|Ultra High Security Password Generator | Mendhak's ADO.NET Tutorial
    ● Code Bank: Random Password Generator | Generic DbProviderFactory Access
    ● Site Work: Bottle Run Xtreme | Spaids Racing.com

    Company I work for - CSSI

    WHEN POSTING PLEASE INDICATE VERSION

    Please use vbcode tags or code tags when posting code
    [highlight=vb]ALL your code goes here[/highlight] or [code]ALL your code goes here[/code]

    If my post helped you in anyway... please be kind and give me some ratings

  5. #5
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    33,923

    Re: VB.Net Newbie - Listview Questions

    You set which sides you want it anchored from (default is left, top) then as the form is resized, the LV edges will stay that same distance from the form's edges.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: VB.Net Newbie - Listview Questions

    Quote Originally Posted by techgnome
    You set which sides you want it anchored from (default is left, top) then as the form is resized, the LV edges will stay that same distance from the form's edges.

    -tg
    Thanks for the post! So when I set the Anchor property which should take care of the Top and Left properties I still have to size the width and height?
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  7. #7
    Frenzied Member vbdotnetboy's Avatar
    Join Date
    Jun 2004
    Location
    Lewisburg, PA "Next year Raiders in the Super Bowl"
    Posts
    1,310

    Re: VB.Net Newbie - Listview Questions

    docking the control to either top, right, left, bottom, or fill will place it in that area. meaning for instance you dock to the top it would be placed in the top of a form or container control and would be the width of the listview owner control you could still set the height of the listview. this is the same with docking it to the bottom.
    if you dock to the right or left then the control is expanded on the y-axis instead of the x, meaning you can play with the width of the control.
    if you us fill the control does just what the property says... it fills the parent control on both the x and y-axis.

    anchor on the other hand, is a nice featrue for resizing controls.

    lets say you have a form that is 800x600
    a listview that is 300x200
    at an x/y of 100/50

    you want it to stay at the 100/50 x/y but when you maximize the form you want the control to grow. so you would set the anchor properties to left, top, right, and bottom... that is if you want it to expand both on the x and y. if you only would want the x it would be left, top, and right. us the IntelliSense to get the exact enumerator

    Derek - Using VS 2008 99% of the time and VS 2003 1% of the time

    Please Help Us To Save Ana

    ● Helpful Links: DNR TV | Awesome site for tips | Using ADO.NET to work with Excel | Xml Namespace 2.0 Framework Changes|Ultra High Security Password Generator | Mendhak's ADO.NET Tutorial
    ● Code Bank: Random Password Generator | Generic DbProviderFactory Access
    ● Site Work: Bottle Run Xtreme | Spaids Racing.com

    Company I work for - CSSI

    WHEN POSTING PLEASE INDICATE VERSION

    Please use vbcode tags or code tags when posting code
    [highlight=vb]ALL your code goes here[/highlight] or [code]ALL your code goes here[/code]

    If my post helped you in anyway... please be kind and give me some ratings

  8. #8
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    33,923

    Re: VB.Net Newbie - Listview Questions

    Step by step:
    1) Toss in a new form (blank)
    2) Plop a LV on it
    - run it and resize the form.... no matter what you do the upper left of the LV stays put
    -stop the app
    3) Find the Anchor property for the LV. Change it FROM Top, Left to Bottom, Right
    - run the app again and resize the form... now the LV will "Stick" to the bottom right corner
    -stop the app
    4) Change the Anchor to Top, Left, Right
    -Run the app again and resize the form
    -Repeat ad nauseum to see how the different combinations of top, left, bottom & right work.

    -tg

    edit: I should mention that this example is with the Docking off... but it can also be retried w/ docking on to see how it works with it and effects things as well.
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: VB.Net Newbie - Listview Questions

    Quote Originally Posted by vbdotnetboy
    VB Code:
    1. . . .
    2.                 'Do this instead
    3.                 For a = 1 To .Columns.Count
    4.                     lvi.SubItems.Add(String.Format("Col" & a & "-Row{0}", i))
    5.                 '    lvi.SubItems(a).Text = String.Format("Col" & a & "-Row{0}", i)
    6.                 Next
    7. . . .
    Thanks for the help, I have implemented your suggestions. I put an Image list on the form and I loaded it with images but when I run the program the icons don't change. Here is my code:

    VB Code:
    1. Private Sub LoadData(ByVal lvw As ListView)
    2.         Dim lvi As ListViewItem
    3.         Dim i As Integer
    4.         Dim a As Integer
    5.  
    6.         With lvw
    7.             .FullRowSelect = True
    8.             .View = View.Details
    9.             .SmallImageList = Me.ImageList1
    10.             .Items.Clear()
    11.  
    12.             For i = 0 To 10
    13.                 lvi = New ListViewItem()
    14.                 lvi.Text = String.Format("Col1-Row{0}", i)
    15.  
    16.                 For a = 1 To .Columns.Count
    17.                     lvi.SubItems.Add(String.Format("Col" & a & "-Row{0}", i))
    18.                     lvi.ImageIndex = a + 5
    19.                 Next a
    20.  
    21.  
    22.                 .Items.Add(lvi)
    23.             Next i
    24.         End With
    25.  
    26.     End Sub
    Attached Images Attached Images  
    Last edited by Mark Gambo; Dec 30th, 2005 at 03:09 PM.
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  10. #10
    Frenzied Member vbdotnetboy's Avatar
    Join Date
    Jun 2004
    Location
    Lewisburg, PA "Next year Raiders in the Super Bowl"
    Posts
    1,310

    Re: VB.Net Newbie - Listview Questions

    well before i answer and possiblly make myself out to be a fool where do you want the images to appear?

    Derek - Using VS 2008 99% of the time and VS 2003 1% of the time

    Please Help Us To Save Ana

    ● Helpful Links: DNR TV | Awesome site for tips | Using ADO.NET to work with Excel | Xml Namespace 2.0 Framework Changes|Ultra High Security Password Generator | Mendhak's ADO.NET Tutorial
    ● Code Bank: Random Password Generator | Generic DbProviderFactory Access
    ● Site Work: Bottle Run Xtreme | Spaids Racing.com

    Company I work for - CSSI

    WHEN POSTING PLEASE INDICATE VERSION

    Please use vbcode tags or code tags when posting code
    [highlight=vb]ALL your code goes here[/highlight] or [code]ALL your code goes here[/code]

    If my post helped you in anyway... please be kind and give me some ratings

  11. #11

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: VB.Net Newbie - Listview Questions

    Quote Originally Posted by vbdotnetboy
    well before i answer and possiblly make myself out to be a fool where do you want the images to appear?

    Adjcent to each row.
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  12. #12
    Frenzied Member vbdotnetboy's Avatar
    Join Date
    Jun 2004
    Location
    Lewisburg, PA "Next year Raiders in the Super Bowl"
    Posts
    1,310

    Re: VB.Net Newbie - Listview Questions

    ok i see the problem... think about it each time the loop end it's the same value therefore your imagelistindex is always going to whatever (x) would be x = a + 5... you would want it in the first loop

    Derek - Using VS 2008 99% of the time and VS 2003 1% of the time

    Please Help Us To Save Ana

    ● Helpful Links: DNR TV | Awesome site for tips | Using ADO.NET to work with Excel | Xml Namespace 2.0 Framework Changes|Ultra High Security Password Generator | Mendhak's ADO.NET Tutorial
    ● Code Bank: Random Password Generator | Generic DbProviderFactory Access
    ● Site Work: Bottle Run Xtreme | Spaids Racing.com

    Company I work for - CSSI

    WHEN POSTING PLEASE INDICATE VERSION

    Please use vbcode tags or code tags when posting code
    [highlight=vb]ALL your code goes here[/highlight] or [code]ALL your code goes here[/code]

    If my post helped you in anyway... please be kind and give me some ratings

  13. #13
    Frenzied Member vbdotnetboy's Avatar
    Join Date
    Jun 2004
    Location
    Lewisburg, PA "Next year Raiders in the Super Bowl"
    Posts
    1,310

    Re: VB.Net Newbie - Listview Questions

    i taking a guess but did you want the image next to each subitem?

    Derek - Using VS 2008 99% of the time and VS 2003 1% of the time

    Please Help Us To Save Ana

    ● Helpful Links: DNR TV | Awesome site for tips | Using ADO.NET to work with Excel | Xml Namespace 2.0 Framework Changes|Ultra High Security Password Generator | Mendhak's ADO.NET Tutorial
    ● Code Bank: Random Password Generator | Generic DbProviderFactory Access
    ● Site Work: Bottle Run Xtreme | Spaids Racing.com

    Company I work for - CSSI

    WHEN POSTING PLEASE INDICATE VERSION

    Please use vbcode tags or code tags when posting code
    [highlight=vb]ALL your code goes here[/highlight] or [code]ALL your code goes here[/code]

    If my post helped you in anyway... please be kind and give me some ratings

  14. #14

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: VB.Net Newbie - Listview Questions

    Quote Originally Posted by vbdotnetboy
    ok i see the problem... think about it each time the loop end it's the same value therefore your imagelistindex is always going to whatever (x) would be x = a + 5... you would want it in the first loop

    D'uh, I can't believe I did that , Thanks!
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  15. #15
    Frenzied Member vbdotnetboy's Avatar
    Join Date
    Jun 2004
    Location
    Lewisburg, PA "Next year Raiders in the Super Bowl"
    Posts
    1,310

    Re: [RESOLVED] VB.Net Newbie - Listview Questions

    no problem

    Derek - Using VS 2008 99% of the time and VS 2003 1% of the time

    Please Help Us To Save Ana

    ● Helpful Links: DNR TV | Awesome site for tips | Using ADO.NET to work with Excel | Xml Namespace 2.0 Framework Changes|Ultra High Security Password Generator | Mendhak's ADO.NET Tutorial
    ● Code Bank: Random Password Generator | Generic DbProviderFactory Access
    ● Site Work: Bottle Run Xtreme | Spaids Racing.com

    Company I work for - CSSI

    WHEN POSTING PLEASE INDICATE VERSION

    Please use vbcode tags or code tags when posting code
    [highlight=vb]ALL your code goes here[/highlight] or [code]ALL your code goes here[/code]

    If my post helped you in anyway... please be kind and give me some ratings

  16. #16

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: [RESOLVED] VB.Net Newbie - Listview Questions

    Here is the updated code, just in case anyone was interested:

    VB Code:
    1. Private Sub LoadData(ByVal lvw As ListView)
    2.         Dim lvi As ListViewItem
    3.         Dim i As Integer
    4.         Dim a As Integer
    5.  
    6.         With lvw
    7.             .FullRowSelect = True
    8.             .View = View.Details
    9.             .SmallImageList = Me.ImageList1
    10.             .Items.Clear()
    11.  
    12.             For i = 0 To 10
    13.                 lvi = New ListViewItem()
    14.                 lvi.Text = String.Format("Col1-Row{0}", i)
    15.                 lvi.ImageIndex = I
    16.  
    17.                 For a = 1 To .Columns.Count
    18.                     lvi.SubItems.Add(String.Format("Col" & a & "-Row{0}", i))
    19.                 Next a
    20.  
    21.                 .Items.Add(lvi)
    22.             Next i
    23.         End With
    24.  
    25.     End Sub
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width