none
More issues with listview RRS feed

  • Question

  • @Leshay, The code you gave me on my last posting worked fine but I am having problem getting it to work with data coming from textbox please if you could offer a little more help.  I only need the address on the second listview (lstStudcopy). I am going to put as little code as possible that way I do not clog the forum but if have a question please let me. For a little history, I am looking to have the name and address information of the student in addition to appearing in the textboxes to also appear on the second listview on  button Click. The moment it appears in the text boxes but not on listview. 2 I will prefer that it appear at the top of the list on its column. I have tried to upload the image if the form without success. I kept getting "Body must be 4 to 6000 character long" 

    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click 'Search table Students cmd = New OleDbCommand("Select * From Students WHERE Student_ID = @StuID", conn) cmd.Parameters.AddWithValue("Student_ID", txtStudentID.Text) dr = cmd.ExecuteReader If dr.Read Then txtSurname.Text = dr.Item("LastName").ToString txtfirstName.Text = dr.Item("FirstName").ToString txtMiddleName.Text = dr.Item("MiddleName").ToString txtAddress1.Text = dr.Item("Address1").ToString txtAddress2.Text = dr.Item("Address2").ToString txtCity.Text = dr.Item("City").ToString txtState.Text = dr.Item("State").ToString ' Here I am trying to set up the name address information details ' these details are comming form a table into txtbox ' then into the the listview. Dim strFirstName As String = txtfirstName.Text Dim strSurname As String = txtSurname.Text Dim strMidName As String = txtMiddleName.Text Dim strAddress1 As String = txtAddress1.Text Dim strAddress2 As String = txtAddress2.Text Dim strcity As String = txtCity.Text Dim strState As String = txtState.Text Dim strFullName As String = strFirstName & " " & strSurname Dim fullAddress As String = strAddress1 & " " & strAddress2 Dim City_State As String = strcity & " " & strState Address.Add("strFullName") Address.Add("Address1") Address.Add("strAddress2") Address.Add("StrFullCity_State") End If dr.Close() dr.Dispose() conn.Close() End Sub


    Monday, February 12, 2018 10:23 AM

Answers

  • Hi

    Here is the code altered to maybe show a little more detail in using it in conjunction with the text boxes you mention.

    This is different from the first code example I posted as it now has the lstStudcopy LV, Button1 and textboxes (named ass per your post) added via the designer.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim Address As New List(Of String)
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    	' set updummy address details
    	' these details could be from TextBoxes
    	' or any other source
    	Address.Add(txtfirstName.Text & " " & txtSurname.Text)
    	Address.Add(txtAddress1.Text)
    	Address.Add(txtAddress2.Text)
    	Address.Add(txtCity.Text & " " & txtState.Text)
    	' ------------------------
    
    	' set up initial ListView with some
    	' dunny test data
    	With lstStudcopy
    	  .View = View.Details
    
    	  .Columns.Add("Item Column", 100, HorizontalAlignment.Left)
    	  .Columns.Add("Col 2", 50, HorizontalAlignment.Left)
    	  .Columns.Add("Col 3", 50, HorizontalAlignment.Left)
    	  .Columns.Add("Col 4", 50, HorizontalAlignment.Center)
    	  .Columns.Add("Col 5", 50, HorizontalAlignment.Center)
    	  .Columns.Add("Col 6", 50, HorizontalAlignment.Center)
    
    	  ' add some test data
    	  For i As Integer = 1 To 20
    		Dim item As New ListViewItem("item" & i.ToString, 0)
    		item.Checked = True
    		item.SubItems.Add(i.ToString)
    		item.SubItems.Add((i + 1).ToString)
    		item.SubItems.Add((i + 2).ToString)
    		item.SubItems.Add((i + 3).ToString)
    		item.SubItems.Add((i + 4).ToString)
    		lstStudcopy.Items.Add(item)
    	  Next
    	End With
      End Sub
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    	' Button Click inserts Address data
    	' NOTE: no checks for already used
    	' so each Click inserts a set of
    	' Address details.
    	For i As Integer = Address.Count - 1 To 0 Step -1
    	  lstStudcopy.Items.Insert(0, Address(i))
    	Next
      End Sub
    
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by alobi Monday, February 12, 2018 4:16 PM
    Monday, February 12, 2018 12:18 PM

All replies

  • Hi

    Here is the code altered to maybe show a little more detail in using it in conjunction with the text boxes you mention.

    This is different from the first code example I posted as it now has the lstStudcopy LV, Button1 and textboxes (named ass per your post) added via the designer.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim Address As New List(Of String)
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    	' set updummy address details
    	' these details could be from TextBoxes
    	' or any other source
    	Address.Add(txtfirstName.Text & " " & txtSurname.Text)
    	Address.Add(txtAddress1.Text)
    	Address.Add(txtAddress2.Text)
    	Address.Add(txtCity.Text & " " & txtState.Text)
    	' ------------------------
    
    	' set up initial ListView with some
    	' dunny test data
    	With lstStudcopy
    	  .View = View.Details
    
    	  .Columns.Add("Item Column", 100, HorizontalAlignment.Left)
    	  .Columns.Add("Col 2", 50, HorizontalAlignment.Left)
    	  .Columns.Add("Col 3", 50, HorizontalAlignment.Left)
    	  .Columns.Add("Col 4", 50, HorizontalAlignment.Center)
    	  .Columns.Add("Col 5", 50, HorizontalAlignment.Center)
    	  .Columns.Add("Col 6", 50, HorizontalAlignment.Center)
    
    	  ' add some test data
    	  For i As Integer = 1 To 20
    		Dim item As New ListViewItem("item" & i.ToString, 0)
    		item.Checked = True
    		item.SubItems.Add(i.ToString)
    		item.SubItems.Add((i + 1).ToString)
    		item.SubItems.Add((i + 2).ToString)
    		item.SubItems.Add((i + 3).ToString)
    		item.SubItems.Add((i + 4).ToString)
    		lstStudcopy.Items.Add(item)
    	  Next
    	End With
      End Sub
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    	' Button Click inserts Address data
    	' NOTE: no checks for already used
    	' so each Click inserts a set of
    	' Address details.
    	For i As Integer = Address.Count - 1 To 0 Step -1
    	  lstStudcopy.Items.Insert(0, Address(i))
    	Next
      End Sub
    
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by alobi Monday, February 12, 2018 4:16 PM
    Monday, February 12, 2018 12:18 PM
  • Thanks Les, I will go integrate it with my code and get back here.
    Monday, February 12, 2018 1:02 PM
  • Hi

    One thing. The way things have been done are purely for this demo. In reality, you will have to consider:

    1) Is there already a set of Address Data added to the LV

    2) If so, then instead of adding a new set, you would need to either remove the top 4 LV rows before adding new - or - code for an edit of existing Address Data.


    Regards Les, Livingston, Scotland

    Monday, February 12, 2018 1:06 PM