[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:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'FrontDataSet.Copy_of_Features' table. You can move, or remove it, as needed.
Dim a As Integer
With ListView1
.View = View.Details
End With
Call lvwInitialize(ListView1)
Call DoTheChart()
Call ResizeForm()
End Sub
Private Sub lvwInitialize(ByVal lvw As ListView)
Dim lvh As ColumnHeader
Dim a As Integer
With lvw
.Clear()
.FullRowSelect = True
With .Columns
For a = 1 To 3
lvh = .Add("Column " & a)
lvh.Width = lvw.Width / 3
Next a
End With
End With
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Call LoadData(ListView1)
End Sub
Private Sub LoadData(ByVal lvw As ListView)
Dim lvi As ListViewItem
Dim i As Integer
Dim a As Integer
With lvw
.FullRowSelect = True
.View = View.Details
.Items.Clear()
For i = 0 To 10
lvi = New ListViewItem()
lvi.Text = String.Format("Col1-Row{0}", i)
For a = 1 To .Columns.Count
lvi.SubItems.Add("") '<=== Why do I need this line?
lvi.SubItems(a).Text = String.Format("Col" & a & "-Row{0}", i)
Next a
.Items.Add(lvi)
Next i
End With
End Sub
Private Sub ResizeForm()
Dim lvw As ListView
Dim lvh As ColumnHeader
Dim a As Integer
lvw = ListView1
If Me.WindowState = FormWindowState.Minimized Then Exit Sub
With lvw
.Top = 0
.Left = 0
.Width = ClientSize.Width - 10
.Height = (ClientSize.Height) - 30
With .Columns
For a = 0 To .Count - 1
lvh = .Item(a)
lvh.Width = lvw.Width / 3
Next a
End With
End With
With Button1
.Top = ClientSize.Height - .Height - 5
End With
End Sub
Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
Call ResizeForm()
End Sub
End Class
Thanks! :wave:
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
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
Re: VB.Net Newbie - Listview Questions
VB Code:
Private Sub LoadData(ByVal lvw As ListView)
Dim lvi As ListViewItem
Dim i As Integer
Dim a As Integer
With lvw
.FullRowSelect = True
.View = View.Details
.Items.Clear()
For i = 0 To 10
lvi = New ListViewItem()
lvi.Text = String.Format("Col1-Row{0}", i)
'Do this instead
For a = 1 To .Columns.Count
lvi.SubItems.Add(String.Format("Col" & a & "-Row{0}", i))
' lvi.SubItems(a).Text = String.Format("Col" & a & "-Row{0}", i)
Next
.Items.Add(lvi)
Next i
End With
End Sub
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
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?
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
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.
1 Attachment(s)
Re: VB.Net Newbie - Listview Questions
Quote:
Originally Posted by vbdotnetboy
VB Code:
. . .
'Do this instead
For a = 1 To .Columns.Count
lvi.SubItems.Add(String.Format("Col" & a & "-Row{0}", i))
' lvi.SubItems(a).Text = String.Format("Col" & a & "-Row{0}", i)
Next
. . .
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:
Private Sub LoadData(ByVal lvw As ListView)
Dim lvi As ListViewItem
Dim i As Integer
Dim a As Integer
With lvw
.FullRowSelect = True
.View = View.Details
.SmallImageList = Me.ImageList1
.Items.Clear()
For i = 0 To 10
lvi = New ListViewItem()
lvi.Text = String.Format("Col1-Row{0}", i)
For a = 1 To .Columns.Count
lvi.SubItems.Add(String.Format("Col" & a & "-Row{0}", i))
lvi.ImageIndex = a + 5
Next a
.Items.Add(lvi)
Next i
End With
End Sub
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?
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.
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
Re: VB.Net Newbie - Listview Questions
i taking a guess but did you want the image next to each subitem?
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 :blush: :blush: , Thanks!
Re: [RESOLVED] VB.Net Newbie - Listview Questions
Re: [RESOLVED] VB.Net Newbie - Listview Questions
Here is the updated code, just in case anyone was interested:
VB Code:
Private Sub LoadData(ByVal lvw As ListView)
Dim lvi As ListViewItem
Dim i As Integer
Dim a As Integer
With lvw
.FullRowSelect = True
.View = View.Details
.SmallImageList = Me.ImageList1
.Items.Clear()
For i = 0 To 10
lvi = New ListViewItem()
lvi.Text = String.Format("Col1-Row{0}", i)
lvi.ImageIndex = I
For a = 1 To .Columns.Count
lvi.SubItems.Add(String.Format("Col" & a & "-Row{0}", i))
Next a
.Items.Add(lvi)
Next i
End With
End Sub