Click here to Skip to main content
15,894,740 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was using a program called personal ancestry file and liked the setup they had for adding or updating information. They had one column (1st) with the name the columns of a database (full name, data of birth, date of death, ....etc) then the second column was where you entered the information, for instance,
column 1              column 2
Full name:            Smith, Steve
Date of birth:        12/12/2012
Date of death:        12/12/2017

the design is interesting and the flow is great. it only showed a textbox or combobox for which ever column I was in. I could tab over or use my mouse to go to whichever column I wanted to. once I left one column and went to the next then the textbox or combobox would disappear and be displayed in the new column.

I can add a textbox to the specific location of a listview, however, it permanently adds it to the listview instead of being displayed only when on that column or cell. that code is:

VB
Private Sub ListView1_MouseDoubleClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseClick

For i = 0 To ListView1.Items.Count - 1

            With txb
                .Name = ListView1.Items(i).Text.ToString.Replace(":", "")

            End With

        Next

        txb.Location = New Point(ListView1.SelectedItems.Item(0).SubItems.Item(1).Bounds.X, ListView1.SelectedItems.Item(0).SubItems.Item(1).Bounds.Y)
        ListView1.Controls.Add(txb)

        ListView1.SelectedItems.Item(0).SubItems.Item(1).Text = txb.Text

        If Not String.IsNullOrEmpty(ListView1.Items(0).SubItems(1).Text) Then
            txb.Hide()
        Else
            txb.Clear()
            txb.Show

        End If

 End Sub


What I have tried:

I have tried to use a listview.controls.remove(txb), but that did not work the way I wanted it to.

I have now tried the hide/show method. It semiworks. Problem is when I first move to another subitem the text gets added to it instead of the one I was trying to add text to. Another issue I am having is with the txb.clear, I was hoping that I could reuse the text in the subitem cell and clear text data in the textbox before going to the subitem.

Any suggestions?
Posted
Updated 20-Apr-18 19:22pm
v5

1 solution

One thing is that if you have code that should remove the text box, you didn't show it. Have a try with something like

  • When a listview row is clicked (not double clicked), first check if there is a text box in the window level variable
  • If there is, hide it and get rid of the instance
  • After that create a new text box and store it into a window level variable

If not really needed, I wouldn't try to reuse the objects since it just introduces unnecessary complexity into the logic.

ADDITION

Consider the following example. It contains a lot of assumptions so modify it based on your requirements
VB
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim a As ListViewItem

    a = Me.ListView1.Items.Add("One")
    a.SubItems.Add("1")

    a = Me.ListView1.Items.Add("Two")
    a.SubItems.Add("2")

    a = Me.ListView1.Items.Add("Three")
    a.SubItems.Add("3")
End Sub

Private Sub ListView1_Click(sender As Object, e As EventArgs) Handles ListView1.Click

    If Not txb Is Nothing Then
        RemoveHandler txb.Leave, AddressOf txb_Leave
        ListView1.Controls.Remove(txb)
        txb.Hide()
        txb = Nothing
    End If

    txb = New TextBox()
    txb.Text = Me.ListView1.SelectedItems(0).SubItems(1).Text
    txb.Location = New Point(ListView1.SelectedItems.Item(0).SubItems.Item(1).Bounds.X, ListView1.SelectedItems.Item(0).SubItems.Item(1).Bounds.Y)
    txb.Tag = Me.ListView1.SelectedItems(0)
    AddHandler txb.Leave, AddressOf txb_Leave
    ListView1.Controls.Add(txb)

End Sub

Private Sub txb_Leave(sender As Object, e As EventArgs) Handles TextBox1.Leave
    Dim item As ListViewItem
    item = CType(txb.Tag, ListViewItem)

    item.SubItems(1).Text = txb.Text
End Sub
 
Share this answer
 
v3
Comments
Member 11856456 21-Apr-18 2:16am    
Could you give an example. Also, sorry about the code, its not a double click, its only a single click. The first portion says double because thats what I started with, but have changed it since then.
Wendelius 21-Apr-18 2:40am    
See the updated answer.
Member 11856456 21-Apr-18 9:14am    
I had to change one thing in the coding for it to work
changed: Private Sub txb_Leave(sender As Object, e As EventArgs) Handles TextBox1.Leave
to: Private Sub txb_Leave(sender As Object, e As EventArgs) Handles Txb.Leave

The code works great, I really appreciate the help. There was not much difference in the code so it is pretty easy to understand.

Thanks again for the help
Wendelius 21-Apr-18 11:06am    
You're most welcome, glad it helped!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900