Visual Basic List control additions and deletions to the ListView, modal dialog boxes, window resizing

Source: Internet
Author: User

List control ListView is one of the core components of a form, and is widely used in various forms programs. In the "MFC" Student information management, the implementation of the list control node additions and deletions (click Open link) is extremely difficult to implement the list control ListView additions and deletions, in VB can be easily implemented, the following to give a Lezilai to explain the problem.


As a ListView control, click the "Add" button, in the popup modal window, you can add the corresponding item for the ListView control.

The Delete button allows you to delete the selected item. With the Modify button, the value of the selected item is passed to the modal window by modifying the value of the modal window. Ability to modify the contents of the corresponding column.

Production process:

First, control layout and property settings

1, a new VB project, the inside has brought a Form1 form, is inside add a Form2 form


2. As shown in the layout, drag 3 buttons into the Form1 with a ListView control. Click the Form1 form to disable the Form1 maximize button. Also note the size of the Form1 at this time, and a script will be written.


3, for the ListView1 property of Form1, change its modifiers from the default friends to public, so that the Form2 script can manipulate it. Otherwise, this control can only be manipulated in Form1 scripts.


4, in Form2, as shown in the layout, drag into three label text label, three textbox textbox, a Button1 button. Change the modifiers of the three text box textbox from the default friends to public so that the Form1 script can manipulate the three text boxes. Also, for Form2 this form, set the same as Form1, disable the Maximize button, and record the size of the Form2


Second, script programming

1, double-click the Form1 form, three buttons, write the following script.

Its core idea is that

(1) Prohibit Form1 is adjusted size, once the size of the FORM1 is adjusted, then immediately reply to our set size

(2) When the Form1 form is loaded, the basic properties of the ListView and three buttons are set.

(3) "Add" button Button1 click, the FORM2 global variable selectitemindex is set to "add" state. The selectitemindex=-1 here think there are no selected items. Unlike the "Modify" button Button3 is clicked, the index value of the selected item is sent to FORM2 by modifying the global variable.

(4) for the "delete" button, it is to find the selected item and delete it.

Public Class Form1 ' disable resizing of form script ' here is unable to remove the width of the current form through me.width and me.height, this value is fixed to the system's form default value until the form is not finished loading, Win7 is 300 ' Therefore, set the current form manually. The width and height is the Size property in the Form1 of the form editing area Dim form_width As Integer = 275 Dim form_height As Integer = 226 Private Sub form1_res            Ize (ByVal sender as Object, ByVal e as System.EventArgs) Handles me.resize If me.width <> Form_width Then Me.Width = Form_width End If if me.height <> form_height then me.height = Form_heig HT End If End Sub ' program starts script Private Sub Form1_Load (ByVal sender as System.Object, ByVal e as System.eventar GS) Handles MyBase.Load me.text = "ListView control Additions and deletions" Listview1.view = View.Details ' Set ListView1 style Lis Tview1.fullrowselect = True ' When selected, the entire line is blacked out listview1.multiselect = False ' Prohibit select multiple ' to set column name and width for each column because there are 3 items, so the width is the current The width of the ListView is One-third. Each minus 3 pixels is placed in the presence of a horizontal scroll bar ListView1.Columns.Add ("Column 1", CInt (LISTVIEW1.WIDTH/3)-3) LISTVIEW1.COLUMNS.ADD ("Column 2", CI NT (ListVIEW1.WIDTH/3)-3) LISTVIEW1.COLUMNS.ADD ("Column 3", CInt (LISTVIEW1.WIDTH/3)-3) ' button settings Button1.Text = "Add" button2.text = "delete" Button3.text = "Modify" End Sub ' "Add" button script Private Sub button1_click (ByVal se        NDEr as System.Object, ByVal e as System.EventArgs) Handles Button1.Click form2.selectitemindex =-1 ' flag current behavior: ' Add ' The Form2.showdialog () ' form Form2 the End sub ' delete button script as a modal dialog box to the Private Sub button2_click (ByVal sender as System.            Object, ByVal e as System.EventArgs) Handles button2.click If ListView1.SelectedItems.Count > 0 Then ' Find the currently selected item and delete Dim Selectitemindex as Integer = ListView1.SelectedItems.Item (listview1.selecteditems.count-1). Index ListView1.Items.Item (Selectitemindex). Remove () Else MsgBox ("Please select one of them!"). ") End If End Sub ' modify ' button script Private Sub button3_click (ByVal sender as System.Object, ByVal e as System.eve Ntargs) Handles Button3.click If LiStView1.SelectedItems.Count > 0 Then ' Find the currently selected item Dim Selectitemindex as Integer = Listview1.selecte Ditems.item (listview1.selecteditems.count-1). Index Form2.selectitemindex = Selectitemindex ' assigns its value to Form2 's various dialog boxes Form2.TextBox1.Text = Lis TView1.Items.Item (Selectitemindex). SubItems (0). Text Form2.TextBox2.Text = ListView1.Items.Item (Selectitemindex). SubItems (1). Text Form2.TextBox3.Text = ListView1.Items.Item (Selectitemindex). SubItems (2). The Text form2.showdialog () ' form Form2 a modal dialog to open the Else MsgBox ("Please select one of them!"). ") End If End SubEnd Class

2. Double-click the Button1 in the Form2 form and Form1 to write the following script.

The basic idea is:

(1) Same as FORM1, prevents users from resizing this form Form2

(2) When Form2 is opened, the value of the SELECTITEMINDEX global variable is defined according to the FORM1 user's clicked button, setting Button1 whether to show "add" or "modify"

(3) When the Form2 Button1 is clicked, the "Add" and "modify" actions are implemented according to the value of Selectitemindex.

Public Class Form2 ' disable resizing of form script ' here is unable to remove the width of the current form through me.width and me.height, this value is fixed to the system's form default value until the form is not finished loading, Win7 is 300 ' Therefore, set the current form manually. The width and height is the Size property in the Form2 of the form editing area Dim form_width As Integer = 195 Dim form_height As Integer = 176 Private Sub form1_res            Ize (ByVal sender as Object, ByVal e as System.EventArgs) Handles me.resize If me.width <> Form_width Then Me.Width = Form_width End If if me.height <> form_height then me.height = Form_heig HT End If End Sub public selectitemindex as Integer for global variables controlled by FORM1 to determine whether to modify or add an item ' This modal dialog box loads the action Private        Sub Form2_load (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles mybase.load label1.text = "Column 1:"        Label2.Text = "Column 2:" Label3.text = "column 3:" If selectitemindex =-1 Then button1.text = "Add" Else Button1.Text = "Modify" End If End Sub ' Button1 is clicked when action Private Sub button1_click (ByVal Sender as System.Object, ByVal e as System.EventArgs) Handles Button1.Click If Selectitemindex =-1 Then ' add If TextBox1.Text = "Or TextBox2.Text =" "or TextBox3.Text =" then MsgBox ("Column 1, column 2, column 3 any of the items cannot be empty! ") Else Dim item As ListViewItem = Form1.ListView1.Items.Add (TextBox1.Text) Dim t                Strings (2) as String tStrings (0) = TextBox2.Text tStrings (1) = TextBox3.Text Item. Subitems.addrange (tStrings) me.close () End if Else ' modify if TextBox1.Text = "" O R TextBox2.Text = "" Or textbox3.text = "" Then MsgBox ("Column 1, column 2, column 3 any of the items cannot be empty! ") Else Form1.ListView1.Items.Item (Me.selectitemindex). SubItems (0). Text = TextBox1.Text Form1.ListView1.Items.Item (me.selectitemindex). SubItems (1). Text = TextBox2.Text Form1.ListView1.Items.Item (me.selectitemindex). SubItems (2). Text = TextBox3.Text Me.close() End If End If End SubEnd Class 


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Visual Basic List control additions and deletions to the ListView, modal dialog boxes, window resizing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.