Wednesday, April 25, 2018

How to edit or delete selected Listview Items in vb6.0 tips

How to edit or delete selected Listview Items in vb6.0 tips

First you have to add Listview control in form1.

Listview is basically a table on which can easily add, edit or delete selected record. You can easily show records in controls like: TextBox, Label, ListBox, ComboBox etc. In this post, we describe the process of documentation with image as well as regarding description.

We will follow step by step process to fully understand.

(1). Edit or update Process
(2). Delete items Process

First of all, there is main point that this control must contain at least one record. So, first to understand this process, Just add some records in this control.
So, let’s start.

(1). Edit or update Process:
Suppose, Listview contain 4 column like: ‘Customer_Name’, ‘Cust_Age’, ‘Cust_Address’, ‘Cust_MoNo’.

Suppose, Form1 contain column’s related Textbox like: ‘CustName_Txt’, ‘CustAge_Txt’, ‘CustAddress_Txt’, ‘CustMoNo_Txt’. These Textbox shows Listview’s items data for editing or updating.

Note: You have to drag a label and define its name ‘Index_Lbl’. ‘Index_Lbl’ will present the item’s index number. sanjeev

The following image will describe the related above description’s confusion.
Record and controls for edit or delete selected items
Selected Record and Controls Description
The description is easily understandable that how to define controls in Form and How to show data to Textbox and selected row index in label?
After selecting item from Listview, its related index id and data will show in Textbox and Label.
Suppose, we edit record from row no 2. Then select row 2 and it will show data in Textbox.
Suppose, we drag a command button with caption ‘Edit or Update’, assign this command button ‘Name’ as “Edit_Cmd”.

And Change Listview1 name as 'LVData1'

Now, double click on ‘Edit or Update’ command button and write code on its ‘Click’ event as shown below.

Code:
Private Sub Edit_Cmd_Click()

With LVData1 

'Textbox data will edit Listview's selected item data 
 .ListItems(Val(Index_Lbl.Caption)).Text = CustName_Txt.Text
 .ListItems(Val(Index_Lbl.Caption)).SubItems(1) = CustAge_Txt.Text
 .ListItems(Val(Index_Lbl.Caption)).SubItems(2) = CustAddress_Txt.Text
 .ListItems(Val(Index_Lbl.Caption)).SubItems(3) = CustMoNo_Txt.Text

End With
 MsgBox "Record Edited successfully.", vbInformation 

'After completion of edtitng process, clear all Textbox and Label

 CustName_Txt.Text = ""
 CustAge_Txt.Text = ""
 CustAddress_Txt.Text = ""
 CustMoNo_Txt.Text = ""
 Index_Lbl.Caption = ""

End Sub


Code for Edit or delete selected Listview items
Click Event code
Or type code through another way

Code:
Private Sub Edit_Cmd_Click()

'Index_Lbl: is item index number
Dim indx As Integer
indx = Val(Index_Lbl.Caption)

 With LVData1

'Textbox data will edit Listview's selected item data

 .ListItems(indx).Text = CustName_Txt.Text
 .ListItems(indx).SubItems(1) = CustAge_Txt.Text
 .ListItems(indx).SubItems(2) = Cust Address_Txt.Text
 .ListItems(indx).SubItems(3) = Cust MoNo_Txt.Text

 End With

MsgBox "Record Edited successfully.", vbInformation

End Sub
Edit Output for selected Listview Items
Edit Record Description and Output
After writing this code, run application. After running application, select any item for edit from LVData1. Then click on ‘Edit or Update’ button. After that a message box will show for completion of editing or updating record.


(2). Delete items Process :
If you want to remove selected items, there are 2 processes for it.

(a). Delete items through Command Button
(b). Delete items through Press ‘Delete’ key

Now follow it with details.

(a). Delete items through Command Button:
Drag command button control to form and assign its Caption as ‘Delete’ and Name as ‘Delete_Txt’.

And type following code in click event of this button.

Code:
Private Sub Command2_Click()

 LVData1.ListItems.Remove (Val(Index_Lbl.Caption))
 Index_Lbl.Caption = ""

End Sub

(b). Delete item through Press ‘Delete’ key:
For remove selected item through ‘Delete’ key press from keyboard, just type code in LVData1’s ‘KeyDown’ event. The following code will explain easily.

Code:
Private Sub LVData1_KeyDown(KeyCode As Integer, Shift As Integer)

If KeyCode = 46 Then

 LVData1.ListItems.Remove (Val(Index_Lbl.Caption))
 Index_Lbl.Caption = ""

End If

End Sub

Here, Keycode=46 is process on ‘Delete’ Key. When you press this key after selecting item, then this will remove that particular row items.

Now the Tips to edit or delete selected Listview item in visual basic 6.0.

Add ADO Data Control (ADODC) component in vb6.0

2 comments: