Stay in touch
Subscribe to our RSS!
Oh c'mon
Bookmark us!
Have a question?
Get an answer!

Working with Advance Controls




Working with Advance Controls


1.1. ListView Control

The ListView control can be used to display a list of icons in your program, similar to the explorer file view, or a list of data in the form of a table, with icons at the side.

Ex1: Filling the ListView control with 3 different items, in view1: LargeIcon

• Add a ListView control to the form you are working on
• Call it lvwTest

This was achieved with the following code in the Form_Load event.

Private Sub frmLVWTest_Load(ByVal sender As System.Object, ByVal e As 
System.EventArgs) Handles MyBase.Load
 lvwTest.View = View.LargeIcon
 lvwTest.Items.Add("Item 1")
    lvwTest.Items.Add("Item 22")
    lvwTest.Items.Add("Item 333")
End Sub

Ex2:  Adding 32x32 icons to the ListView in view1

• Add a ListView control to the form you are working on
• Call it lvwTest
• Add an ImageList to the form
• Call it 'imgIcons'
• Set the 'Width' and 'Height' properties to 32
• Add any 3 icons to the ImageList by clicking on it and hitting the '...' button on the 'Images' property of the control. Now press the 'Add' button to find an icon

We can spruce the previous ListView example up by attaching icons to the items in the ListView, by changing the code in the Form_Load event.

Private Sub frmLVWTest_Load(ByVal sender As System.Object, ByVal e As 
                                                                                System.EventArgs) Handles MyBase.Load
  lvwTest.View = View.LargeIcon
lvwTest.LargeImageList = imgIcons     
lvwTest.Items.Add("Item 1", 0)
      lvwTest.Items.Add("Item 22", 1)
      lvwTest.Items.Add("Item 333", 2)
End Sub

Here, we are instructing each ListItem to pull an icon from the ImageList by its index.  We have 3 icons in the ImageList, so they are numbered 0, 1 and 2.  Note that I've added the extra line 2nd from the top to tell the ListView which ImageList to look in for its icons.

Another thing to try is using 16x16 icons.  For this, make sure you set the ImageList, imgIcons 'Height' and 'Width' properties to 16. You will have to delete and re-add the icons to the ImageList when changing the size. Now you have to tell the ListView where to get the small icons, and to switch to small icon view.

Simply use this code in place of the first two lines of the previous code:

lvwTest.View = View.List 'Sets the display type to List them in a straight line downwards
lvwTest.SmallImageList = imgIcons

OR

lvwTest.View = View.SmallIcons 'Tells the ListView to list items across itself
lvwTest.SmallImageList = imgIcons

Ex3: The ListView in style2: Details

• Add a ListView control to the form you are working on
• Call it lvwTest
• Add an ImageList to the form
• Call it 'imgIcons'
• Set the 'Width' and 'Height' properties to 16
• Add any 3 icons to the ImageList by clicking on it and hitting the '...' button on the 'Images' property of the control. Now press the 'Add' button to find an icon.

In my opinion, this is the most useful mode of the ListView.  It is similar to a spreadsheet layout with its multiple columns and lines.

The code here is slightly more complex, mainly because there is more of it.  If you examine it closely, it is the same thing repeated 3 times, for each of the columns or items.

Private Sub frmLVWTest_Load(ByVal sender As System.Object, ByVal e As 
System.EventArgs) Handles MyBase.Load
lvwTest.View = View.Details 'Sets the ListView to display items with columns, as seen in the picture above
lvwTest.SmallImageList = imgIcons 'Sets the ImageList that will be used to get the small icons on the first column
  
 'The following three lines of code create the columns in the picture (A, B and C). 
 'You can also change the width of the columns by changing the '100' to a different value. 
 'The alignment can also be changed, to be right or centre aligned.
 lvwTest.Columns.Add("Column A", 100, HorizontalAlignment.Left)
 lvwTest.Columns.Add("Column B", 100, HorizontalAlignment.Left)
 lvwTest.Columns.Add("Column C", 100, HorizontalAlignment.Left)
  
 Dim lSingleItem As ListViewItem 'The variable will hold the ListItem information so that you can add values to the column you want to change
  
 lSingleItem = lvwTest.Items.Add("Item 1", 0) 'Create a new line, and assign the ListItem into the variable so we can add sub items
 lSingleItem.SubItems.Add("Subitem XXY") 'The first sub item for the first line
 lSingleItem.SubItems.Add("Subitem ZZY") 'The second sub item for the first line
  
 lSingleItem = lvwTest.Items.Add("Item 22", 1) 'Create another line. Same as previous new line code, but with different icon number and caption
lSingleItem.SubItems.Add("Subitem CCA") 'The first sub item for the second line
 lSingleItem.SubItems.Add("Subitem DDA")
  
 lSingleItem = lvwTest.Items.Add("Item 333", 2)
 lSingleItem.SubItems.Add("Subitem TTG")
 lSingleItem.SubItems.Add("Subitem HHG") 'The second sub item for the third line
End Sub

1.2. Create MDI Form and Menu Bar

This section shows you how to create MDI Form with Menu Bar in Microsoft Visual Basic 2008. From this article, you will know:

• How to create MDI Form?
• How to create Menu Bar?
• How to click on each menu item to open child form in MDI form?




1. Create MDI Form
Create New Form (Project Menu -> Add New Item -> Windows Form) 
Change Properties of Form as below
• WindowState: Maximized
• isMdiContainer: True
2. Create Menu Bar
Drag MenuStrip Control to MDI Form 
Add Menu Item and Sub Menu Item
3. Write Code
Click on Menu Item to show Form in MDI Form (see example below)
frmStudent.MdiParent = Me
frmStudent.Show()

1.3. CheckedListBox

Add Object to CheckedListBox

Dim pItem As CarItem

pItem = New CarItem() 
pItem.CarId = 1
pItem.CarName="CarName"

' Add this object to CheckListBox
' And CheckListBox show CarName because I override toString function
Me.CheckListBox1.Items.Add(pItem, True) 

' Class CarItem

Private Class CarItem

Public CarId As Integer 
Public CarName As String

Public Sub New()

End Sub 

' Override tostring
Public Overrides Function ToString() As String
Return CarName
End Function

End Class
Get status items in CheckedListBox

For i As Integer = 0 To Me.CheckedListBox.Items.Count - 1
If Me.CheckedListBox.GetItemChecked(i) Then
'TODO Something 
End If 
Next i

Get only Checked Items

For i As Integer = 0 To Me.CheckedListBox.CheckedItems.Count - 1
'TODO Something
Next

Set all item checked

For i As Integer = 0 To Me.CheckedListBox.Items.Count-1
Me.CheckedListBox.SetItemChecked(i, true) 
Next i

1.4. Other Advance Controls

• LinkLabel
• DateTimePicker
• MaskTextBox
• MonthCalendar
• NotifyIcon
• NumericUpdown
• ProgressBar
• RichTextBox
• ToolTip
• TreeView
• WebBrowser
• ToolStrip
• Process
• ColorDialog
• Font Dialog
• FolderBrowserDialog