1 / 11

Listview Control(VB.Net)

Listview Control(VB.Net). Ronak K Panchal (Asst. Professor). Introduction ListView. multiple columns to a ListView control. Some times its hard for beginners to find even small clues like ListView multi column won't work until you set its View property as Details. Create Project.

gmarie
Download Presentation

Listview Control(VB.Net)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Listview Control(VB.Net) Ronak K Panchal (Asst. Professor)

  2. Introduction ListView • multiple columns to a ListView control. Some times its hard for beginners to find even small clues like ListView multi column won't work until you set its View property as Details.

  3. Create Project • First create a Windows • Application project in • Visual Studio .NET and • drop a ListView control from toolbox to the form. After that first thing • you need to do is to set View property of list view control to Details. See figure

  4. The View property can have any of following members of View enum. • DetailsEach item appears on a separate line with further information about each item arranged in columns. The left most column contains a small icon and label, and subsequent columns contain sub items as specified by the application. A column displays a header which can display a caption for the column. The user can resize each column at runtime • . • LargeIconEach item appears as a full-sized icon with a label below it • . • ListEach item appears as a small icon with a label to its right. Items are arranged in columns with no column headers • . • SmallIconEach item appears as a small icon with a label to its right. You can also set this property programmatically by using the following code:ListView1.View = System.Windows.Forms.View.Details

  5. Adding Multiple Columns  • Now I add five columns to the control. You can add column headers to the control using its Columns.Add method. The Columns.Add method takes column header name, its width, and alignment. In our sample, I write code at Form_Load method to do so. See Listing 1.  • Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load' Adding ListView Columns • ListView1.Columns.Add("Emp Name", 100, HorizontalAlignment.Left)ListView1.Columns.Add("Emp Address", 150, HorizontalAlignment.Left)ListView1.Columns.Add("Title", 60, HorizontalAlignment.Left)ListView1.Columns.Add("Salary", 50, HorizontalAlignment.Left)ListView1.Columns.Add("Department", 60, HorizontalAlignment.Left) • End Sub • Listing 1. Adding Column Headers of a ListView ControlAfter adding this code, the output of program looks like Figure 2. 

  6. Adding Records • ListView.Item.Add method adds records to a list view control. The Add method takes ListViewItem type value. For multi column list view control, we create a ListViewItem with an array of 5 string items. The following code adds a record to the list view control. Dim str(5) As StringDim itm As ListViewItemstr(0) = "Rob Machy"str(1) = "100 North Ave"str(2) = "Business Manager"str(3) = "89,000"str(4) = "Development" itm = New ListViewItem(str)ListView1.Items.Add(itm)After setting more properties of the list view control and adding more records, our list program looks like Figure 3. 

  7. Code to fill the listview control in vb.net dynamically means use with databaseDim rdGetData As SqlClient.SqlDataReaderTryIf ListView1.Items.Count > 0 ThenListView1.Items.Clear()End IfSQLCmd.CommandType = CommandType.TextSQLCmd.CommandText = "SELECT * from table"rdGetData = SQLCmd.ExecuteReaderDim intCount As Decimal = 0While rdGetData.ReadListView1.Items.Add(Trim("FieldName")) 'col no. 1ListView1.Items(CInt(intCount)).SubItems.Add(Trim(rdGetContactsInfo("FieldName"))) 'col no. 2ListView1.Items(CInt(intCount)).SubItems.Add(Trim(rdGetContactsInfo("FieldName"))) 'col no. 3intCount = intCount + 1End WhilerdGetData.Close()rdGetData = NothingCatch Exp As ExceptionintNumError = Err.Number()MsgBox("[ " & CStr(intNumError) + " ] " + Err.Description, MsgBoxStyle.Critical, " (Program Error)")End TryCode to get selected item in the textbox control into the button's click event or according to your requirment.TextBox1.Text = ListView1.SelectedItems(0).Text TextBox2.Text = ListView1.SelectedItems(0).SubItems(2).TextThis will copy the selected item's first value and 2nd value into the textboxes

  8. Error Provider • The ErrorProvider component provides an easy way to set validation errors. It allows us to set an error message for any control on the form when the input is not valid. When an error message is set, an icon indicating the error will appear next to the control and the error message is displayed as Tool Tip when the mouse is over the control. • Notable property of ErrorProvider in the Appearance section is the Icon property which allows us to set an icon that should be displayed. Notable property in Behavior section is theBlinkRate property which allows to set the rate in milliseconds at which the icon blinks.

  9. Displaying an Error

  10. Let's work with an example. Assume we have a TextBox and a Button on a form. If the TextBox on the form is left blank and if the Button is clicked, an icon will be displayed next to the TextBox and the specified text will appear in the Tool Tip box when the mouse is over the control. • The code for that looks like this: • Private Sub Button1_Click(ByVal sender As System.Object, ByVal e_As System.EventArgs) Handles Button1.Click If TextBox1.Text = "" Then ErrorProvider1.SetError(TextBox1, "Cannot leave textbox blank") Else ErrorProvider1.SetError(TextBox1, "") End IfEnd Sub The image below displays output from above code.

More Related