Insert TextBox Value Into ListView

Posted on 2005-04-18
Last Modified: 2008-02-26
Can someone please tell me how to perform the following:

I have 3 textboxes.

I have a ListView that I set properties for (3) columns on.  
    First Name
    Last Name

I want to have a button, that when pressed, puts the value of the textboxes, into the list, in the accordence of course.  So, value of txtFirstName, goes into column "First Name"

Unless, a ListView is not the best method to perform this function.   I am sure its pretty simple, maybe even an IF Statement, but I have not played with ListView's before, and am a little confused on their usage or purpose.  I think it should work.


Question by:chrisryhal
    LVL 27

    Accepted Solution

    Sample to play with....

    'FORM 1

    Public Class frmAddListview
        Inherits System.Windows.Forms.Form

    #Region " Windows Form Designer generated code "

        Public Sub New()

            'This call is required by the Windows Form Designer.

            'Add any initialization after the InitializeComponent() call

        End Sub

        'Form overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                End If
            End If
        End Sub

        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer

        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        Friend WithEvents ListView1 As System.Windows.Forms.ListView
        Friend WithEvents ImageList1 As System.Windows.Forms.ImageList
        Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
        Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
        Friend WithEvents Button1 As System.Windows.Forms.Button
        Friend WithEvents Label1 As System.Windows.Forms.Label
        Friend WithEvents Label2 As System.Windows.Forms.Label
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Me.components = New System.ComponentModel.Container
            Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(frmAddListview))
            Me.ListView1 = New System.Windows.Forms.ListView
            Me.ImageList1 = New System.Windows.Forms.ImageList(Me.components)
            Me.TextBox1 = New System.Windows.Forms.TextBox
            Me.TextBox2 = New System.Windows.Forms.TextBox
            Me.Button1 = New System.Windows.Forms.Button
            Me.Label1 = New System.Windows.Forms.Label
            Me.Label2 = New System.Windows.Forms.Label
            Me.ListView1.Location = New System.Drawing.Point(48, 12)
            Me.ListView1.Name = "ListView1"
            Me.ListView1.Size = New System.Drawing.Size(212, 97)
            Me.ListView1.TabIndex = 0
            Me.ImageList1.ImageSize = New System.Drawing.Size(16, 16)
            Me.ImageList1.ImageStream = CType(resources.GetObject("ImageList1.ImageStream"), System.Windows.Forms.ImageListStreamer)
            Me.ImageList1.TransparentColor = System.Drawing.Color.Transparent
            Me.TextBox1.Location = New System.Drawing.Point(108, 124)
            Me.TextBox1.Name = "TextBox1"
            Me.TextBox1.Size = New System.Drawing.Size(128, 20)
            Me.TextBox1.TabIndex = 1
            Me.TextBox1.Text = "TextBox1"
            Me.TextBox2.Location = New System.Drawing.Point(108, 156)
            Me.TextBox2.Name = "TextBox2"
            Me.TextBox2.Size = New System.Drawing.Size(128, 20)
            Me.TextBox2.TabIndex = 2
            Me.TextBox2.Text = "TextBox2"
            Me.Button1.Location = New System.Drawing.Point(160, 208)
            Me.Button1.Name = "Button1"
            Me.Button1.TabIndex = 3
            Me.Button1.Text = "Button1"
            Me.Label1.Location = New System.Drawing.Point(24, 124)
            Me.Label1.Name = "Label1"
            Me.Label1.Size = New System.Drawing.Size(76, 16)
            Me.Label1.TabIndex = 4
            Me.Label1.Text = "Sports Name:"
            Me.Label2.Location = New System.Drawing.Point(24, 160)
            Me.Label2.Name = "Label2"
            Me.Label2.Size = New System.Drawing.Size(72, 16)
            Me.Label2.TabIndex = 5
            Me.Label2.Text = "Sub Names:"
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(292, 273)
            Me.Name = "frmAddListview"
            Me.Text = "frmAddListview"

        End Sub

    #End Region

        Private Sub InitializeListView()
            With ListView1
                .Columns.Add("ID", 40, HorizontalAlignment.Right)
                .Columns.Add("Name", 80, HorizontalAlignment.Right)
                .Columns.Add("Sub Name", 80, HorizontalAlignment.Left)
            End With
        End Sub
        Private Sub PopulateList()
            Dim s As String
            Static i As Integer
            Static iDir As Short

            With ListView1
                .SmallImageList = ImageList1
                .Items.Add(iDir, i)
                i += 1
                iDir += 1
                .Sorting = SortOrder.Ascending
                .View = View.Details
            End With
        End Sub

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim i As Short
            For i = 0 To 6
            Dim last As Short
            last = ListView1.Items.Count - 1
            ListView1.Items(last).Selected = True
            ListView1.HideSelection = False
            TextBox1.Text = ListView1.Items(last).Text
        End Sub
        'Dim objCount As Integer
        'Dim ListItem1 As ListViewItem

        '    objCount = lstObjects.Items.Count
        '    ListItem1 = lstObjects.Items.Add(txtObjName.Text)
        '    lstObjects.Items(objCount).ImageIndex = lstObjTypes.SelectedItems(0).ImageIndex
        '    lstObjects.Items(objCount).SubItems.Add(lstObjTypes.SelectedItems(0).Text)

    End Class
    LVL 12

    Expert Comment

    an alternative...
    'Create a listviewitem in the list and then add the subitems
    Dim itemX As ListViewItem = ListView1.Items.Add(txtFirstName.Text)


            'create a listviewitem, add the subitems, then add it to the listview
            Dim itemX As New ListViewItem(txtFirstName.Text)


    Just remember that this is an alternative to give the same result as planocz... so don't award points unfairly (i.e. split them, or just award them to planocz since my answer came in later)

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Article by: jpaulino
    XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String ( Literal, only instead of starting and ending with w…
    Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
    This video discusses moving either the default database or any database to a new volume.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now