• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 577
  • Last Modified:

Insert TextBox Value Into ListView

Can someone please tell me how to perform the following:

I have 3 textboxes.
   txtFirstName
   txtLastName
   txtAddress

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

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.

Thanks,
CR

0
chrisryhal
Asked:
chrisryhal
1 Solution
 
planoczCommented:
Sample to play with....

'FORM 1

Public Class frmAddListview
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call
        InitializeListView()

    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
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    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.SuspendLayout()
        '
        'ListView1
        '
        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
        '
        'ImageList1
        '
        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
        '
        'TextBox1
        '
        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"
        '
        'TextBox2
        '
        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"
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(160, 208)
        Me.Button1.Name = "Button1"
        Me.Button1.TabIndex = 3
        Me.Button1.Text = "Button1"
        '
        'Label1
        '
        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:"
        '
        'Label2
        '
        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:"
        '
        'Form2
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.TextBox2)
        Me.Controls.Add(Me.TextBox1)
        Me.Controls.Add(Me.ListView1)
        Me.Name = "frmAddListview"
        Me.Text = "frmAddListview"
        Me.ResumeLayout(False)

    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)
            .Items(i).SubItems.Add(TextBox1.Text)
            .Items(i).SubItems.Add(TextBox2.Text)
            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
            PopulateList()
        Next
        Dim last As Short
        last = ListView1.Items.Count - 1
        ListView1.Items(last).Selected = True
        ListView1.HideSelection = False
        ListView1.EnsureVisible(last)
        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
0
 
S-TwilleyCommented:
an alternative...
'Create a listviewitem in the list and then add the subitems
Dim itemX As ListViewItem = ListView1.Items.Add(txtFirstName.Text)
itemX.SubItems.Add(txtLastName.Text)
itemX.SubItems.Add(txtAddress.Text)

=========================================

Or
        'create a listviewitem, add the subitems, then add it to the listview
        Dim itemX As New ListViewItem(txtFirstName.Text)
        itemX.SubItems.Add(txtLastName.Text)
        itemX.SubItems.Add(txtAddress.Text)
        ListView1.Items.Add(itemX)

----------------------

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)
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now