Solved

ListView - Press Enter to move to another subitem's textbox

Posted on 2010-11-29
12
1,165 Views
Last Modified: 2012-05-10
I have created a listview with textboxes in the subitems as in the example shown at http://www.codeguru.com/vb/gen/vb_forms/listviewcontrols/article.php/c16251.

Now I want that when user press enter key, it will automatically move the cursor to next subitem textbox.

For example the listview as below:
Name     Age          Height         Bust       Waist
Alice      20             157               40            28
Ally        25              160              38            24
......
There are 30 rows in the listview for user to enter the age, height, bust and waist. the name is not editable. Let say I enter the age for Alice, and when I press enter, it will move the cursor to the textbox height for Alice, and press enter it will move cursor to textbox for the bust Alice to be keyed in the record.

Does anyone know how to do this? I really need help for this.
0
Comment
Question by:alicealiciasarawak
  • 6
  • 5
12 Comments
 
LVL 17

Expert Comment

by:Zhaolai
Comment Utility
Have you tried to use DataGridView control to achieve this, instead of ListView with Textbox?
DataGridView is more powerful and flexible. Here is a tutorial:
http://vb.net-informations.com/datagridview/vb.net_datagridview_tutorial.htm
0
 
LVL 11

Expert Comment

by:Deepak Lakkad
Comment Utility
Hi

below code will help you to select item by pressing "Enter" Key
    Private Sub ListView42_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ListView42.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
            Dim selected As Integer
            selected = ListView42.SelectedIndices(ListView42.SelectedIndices.Count - 1)

            If selected = (ListView42.Items.Count - 1) Then
                selected = 0
            Else
                selected = selected + 1
            End If

            If selected > 0 Then
                ListView42.Items(selected - 1).Selected = False
                ListView42.Items(selected).Selected = True
            End If
        End If
    End Sub

Open in new window


- Deepak Lakkad
0
 

Author Comment

by:alicealiciasarawak
Comment Utility
deepaklakkad,

I have put the code in, but still cannot. I used the same code from http://www.codeguru.com/vb/gen/vb_forms/listviewcontrols/article.php/c16251. And I put in your code, but when I press enter after I keyed in something to the textbox of age for Alice, it didn't move the cursor to the textbox of Height for Alice. Do I miss out something.

Can I have a full example for this?
0
 
LVL 11

Expert Comment

by:Deepak Lakkad
Comment Utility
Hi

I have written KeyPress on ListView not on TextBox.

Your Event for Textbox is not changed so as per the example, TextBox's KeyPress event will call TextBox's LostFocus event - which will write changes into selected ListItem and hides the Textbox.

BTW, Here is the changed code with your requirement.

Public Class frmListView

    'declare a global variable to store the column index
    Private selCol As Integer

    ' Change by Deepak: Move to Next Item
    Private vMoveNext As Boolean = False

    'create ListView structure during FormLoad Event
    Private Sub frmListView_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
        ListView42.Clear()

        ListView42.Columns.Add("Category")
        ListView42.Columns.Add("Monday")
        ListView42.Columns.Add("Tuseday")
        ListView42.Columns.Add("Wednesday")
        ListView42.Columns.Add("Thursday")
        ListView42.Columns.Add("Friday")
        ListView42.Columns.Add("Satturday")
        ListView42.Columns.Add("Sunday")
        ListView42.Columns.Add("Average")

        'load criteria column values into list view
        ListView42.Items.Add("quiet")
        ListView42.Items.Add("light")
        ListView42.Items.Add("medium")
        ListView42.Items.Add("intensive")
        ListView42.Items.Add("heavy")

        'create subitems
        'set the Tags for later identification of clicked subitem(index)
        For a = 0 To ListView42.Items.Count - 1
            For c = 1 To ListView42.Columns.Count - 1
                ListView42.Items(a).SubItems.Add(a & c).Tag = a & c
            Next
        Next

        ListView42.Visible = True
    End Sub

    Private Sub MoveNext()
        Dim selected As Integer
        selected = ListView42.SelectedIndices(ListView42.SelectedIndices.Count - 1)

        If selected = (ListView42.Items.Count - 1) Then
            selected = 0
        Else
            selected = selected + 1
        End If

        If selected > 0 Then
            ListView42.Items(selected - 1).Selected = False
            ListView42.Items(selected).Selected = True
        End If
    End Sub

    Private Sub ListView42_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ListView42.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
            MoveNext()
        End If
    End Sub

    Private Sub ListView42_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView42.MouseDoubleClick

        'detect clicked subitem / column
        Dim hit As ListViewHitTestInfo = ListView42.HitTest(e.X, e.Y)


        For Each lItem As ListViewItem In ListView42.Items
            If lItem.Selected = True Then
                For a = 0 To lItem.SubItems.Count
                    If hit.SubItem.Tag = lItem.SubItems(a).Tag Then
                        selCol = a
                        'prevent edit on first column
                        If selCol = 0 Then Exit Sub
                        'dim size and location of the TextBox
                        'TextBox.FontSize maybe 1 bigger than that of
                        'Listview Items to get the users eye catched on it.
                        TextBox42.Left = ListView42.Left + hit.SubItem.Bounds.Left + 3
                        TextBox42.Top = ListView42.Top + hit.SubItem.Bounds.Top
                        TextBox42.Width = hit.SubItem.Bounds.Width
                        TextBox42.Text = hit.SubItem.Text
                        'set TextBox to visible for user input
                        TextBox42.Visible = True
                        TextBox42.Focus()
                        TextBox42.SelectAll()
                        Exit For
                    End If
                Next
            End If
        Next
    End Sub

    Private Sub TextBox42_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox42.KeyPress
        'call LostFocus Sub in the event user pressed RETURN
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
            ' Change by Deepak: Move to Next Item
            vMoveNext = True

            TextBox42_LostFocus(sender, Nothing)
        End If
    End Sub

    Private Sub TextBox42_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox42.LostFocus

        Dim selDetail As String
        'hide textbox
        TextBox42.Visible = False
        'load old value to detect if there is a change
        selDetail = ListView42.SelectedItems(0).SubItems(selCol).Text

        'change the subitem.text only if the new value is different to the existing one
        'to prevent display of the save butten without changing values
        If selDetail <> TextBox42.Text Then
            ListView42.Items(ListView42.SelectedItems(0).Index).SubItems(selCol).Text = TextBox42.Text
            'set change flag for not stored information to prevent loss of update by change of selection
            'changeFlage = True
            btnSave.Visible = True
        End If

        ' Change by Deepak: Move to Next Item
        If vMoveNext = True Then
            MoveNext()
            vMoveNext = False
        End If


        'do not clear the TextBox.Text, dueto LostFocus Sub may be called twice 
        '(e.g. click mouse on a control different as the ListView)
        'else the list subitem.text will be overwritten again by an empty string 

    End Sub

End Class

Open in new window

- Deepak Lakkad




0
 

Author Comment

by:alicealiciasarawak
Comment Utility
deepaklakkad,

I have tried the code, but when I press enter, it didn't move to next subitem. I have tried to change the Microsoft.VisualBasic.ChrW(Keys.Return) to Microsoft.VisualBasic.ChrW(Keys.Enter), also not working. I don't know how to change it anymore.

I tried using the SharpDevelop to compile it, also got the problem. I really don't know what to do anymore.
Please advice.
0
 
LVL 11

Expert Comment

by:Deepak Lakkad
Comment Utility


Can you put your code here?
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:alicealiciasarawak
Comment Utility
This is code:
Public Partial Class MainForm
	Private WithEvents textBox1 As New TextBox
	
	'declare a global variable to store the column index
    Private selCol As Integer

    ' Change by Deepak: Move to Next Item
    Private vMoveNext As Boolean = False

	Public Sub New()
		' The Me.InitializeComponent call is required for Windows Forms designer support.
		Me.InitializeComponent()
	End Sub
	
	Sub MainFormLoad(sender As Object, e As EventArgs)
		listView1.Clear()

        listView1.Columns.Add("Category")
        listView1.Columns.Add("Monday")
        listView1.Columns.Add("Tuseday")
        listView1.Columns.Add("Wednesday")
        listView1.Columns.Add("Thursday")
        listView1.Columns.Add("Friday")
        listView1.Columns.Add("Satturday")
        listView1.Columns.Add("Sunday")
        listView1.Columns.Add("Average")

        'load criteria column values into list view
        listView1.Items.Add("quiet")
        listView1.Items.Add("light")
        listView1.Items.Add("medium")
        listView1.Items.Add("intensive")
        listView1.Items.Add("heavy")

        'create subitems
        'set the Tags for later identification of clicked subitem(index)
        For a = 0 To listView1.Items.Count - 1
            For c = 1 To listView1.Columns.Count - 1
                listView1.Items(a).SubItems.Add(a & c).Tag = a & c
            Next
        Next

        listView1.Visible = True
	End Sub

	
	Private Sub MoveNext()
        Dim selected As Integer
        selected = listView1.SelectedIndices(listView1.SelectedIndices.Count - 1)

        If selected = (listView1.Items.Count - 1) Then
            selected = 0
        Else
            selected = selected + 1
        End If

        If selected > 0 Then
            listView1.Items(selected - 1).Selected = False
            listView1.Items(selected).Selected = True
        End If
    End Sub

    Private Sub listView1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles listView1.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
            MoveNext()
        End If
    End Sub

    Private Sub listView1_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles listView1.MouseDoubleClick

        'detect clicked subitem / column
        Dim hit As ListViewHitTestInfo = listView1.HitTest(e.X, e.Y)


        For Each lItem As ListViewItem In listView1.Items
            If lItem.Selected = True Then
                For a = 0 To lItem.SubItems.Count
                    If hit.SubItem.Tag = lItem.SubItems(a).Tag Then
                        selCol = a
                        'prevent edit on first column
                        If selCol = 0 Then Exit Sub
                        'dim size and location of the TextBox
                        'TextBox.FontSize maybe 1 bigger than that of
                        'Listview Items to get the users eye catched on it.
                        textBox1.Left = listView1.Left + hit.SubItem.Bounds.Left + 3
                        textBox1.Top = listView1.Top + hit.SubItem.Bounds.Top
                        textBox1.Width = hit.SubItem.Bounds.Width
                        textBox1.Text = hit.SubItem.Text
                        'set TextBox to visible for user input
                        textBox1.Visible = True
                        textBox1.Focus()
                        textBox1.SelectAll()
                        Exit For
                    End If
                Next
            End If
        Next
    End Sub

    Private Sub textBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles textBox1.KeyPress
        'call LostFocus Sub in the event user pressed RETURN
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
            ' Change by Deepak: Move to Next Item
            vMoveNext = True

            textBox1_LostFocus(sender, Nothing)
        End If
    End Sub

    Private Sub textBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles textBox1.LostFocus

        Dim selDetail As String
        'hide textbox
        textBox1.Visible = False
        'load old value to detect if there is a change
        selDetail = listView1.SelectedItems(0).SubItems(selCol).Text

        'change the subitem.text only if the new value is different to the existing one
        'to prevent display of the save butten without changing values
        If selDetail <> textBox1.Text Then
            listView1.Items(listView1.SelectedItems(0).Index).SubItems(selCol).Text = textBox1.Text
            'set change flag for not stored information to prevent loss of update by change of selection
            'changeFlage = True
            buttonSave.Visible = True
        End If

        ' Change by Deepak: Move to Next Item
        If vMoveNext = True Then
            MoveNext()
            vMoveNext = False
        End If

    End Sub

	
End Class

Open in new window

0
 

Author Comment

by:alicealiciasarawak
Comment Utility
For the code below, when I press enter key after I key something in the textBox1 for any column, it will automatically select the whole row, I don't want it to be like that. I want it to move the focus to next subitem for user to key in the data, and so on.
Public Partial Class MainForm

	'declare a global variable to store the column index
    Private selCol As Integer

    ' Change by Deepak: Move to Next Item
    Private vMoveNext As Boolean = False

	Public Sub New()
		' The Me.InitializeComponent call is required for Windows Forms designer support.
		Me.InitializeComponent()
	End Sub
	
	Sub MainFormLoad(sender As Object, e As EventArgs)
		ListView1.Clear()

        ListView1.Columns.Add("Category")
        ListView1.Columns.Add("Monday")
        ListView1.Columns.Add("Tuseday")
        ListView1.Columns.Add("Wednesday")
        ListView1.Columns.Add("Thursday")
        ListView1.Columns.Add("Friday")
        ListView1.Columns.Add("Satturday")
        ListView1.Columns.Add("Sunday")
        ListView1.Columns.Add("Average")

        'load criteria column values into list view
        ListView1.Items.Add("quiet")
        ListView1.Items.Add("light")
        ListView1.Items.Add("medium")
        ListView1.Items.Add("intensive")
        ListView1.Items.Add("heavy")

        'create subitems
        'set the Tags for later identification of clicked subitem(index)
        For a = 0 To ListView1.Items.Count - 1
            For c = 1 To ListView1.Columns.Count - 1
                ListView1.Items(a).SubItems.Add(a & c).Tag = a & c
            Next
        Next

        ListView1.Visible = True

	End Sub
	
	Private Sub MoveNext()
        Dim selected As Integer
        selected = ListView1.SelectedIndices(ListView1.SelectedIndices.Count - 1)

        If selected = (ListView1.Items.Count - 1) Then
            selected = 0
        Else
            selected = selected + 1
        End If

        If selected > 0 Then
            ListView1.Items(selected - 1).Selected = False
            ListView1.Items(selected).Selected = True
        End If
    End Sub

	Private Sub ListView1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ListView1.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
            MoveNext()
        End If
    End Sub

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

        'detect clicked subitem / column
        Dim hit As ListViewHitTestInfo = ListView1.HitTest(e.X, e.Y)


        For Each lItem As ListViewItem In ListView1.Items
            If lItem.Selected = True Then
                For a = 0 To lItem.SubItems.Count
                    If hit.SubItem.Tag = lItem.SubItems(a).Tag Then
                        selCol = a
                        'prevent edit on first column
                        If selCol = 0 Then Exit Sub
                        'dim size and location of the TextBox
                        'TextBox.FontSize maybe 1 bigger than that of
                        'Listview Items to get the users eye catched on it.
                        textBox1.Left = ListView1.Left + hit.SubItem.Bounds.Left + 3
                        textBox1.Top = ListView1.Top + hit.SubItem.Bounds.Top
                        textBox1.Width = hit.SubItem.Bounds.Width
                        textBox1.Text = hit.SubItem.Text
                        'set TextBox to visible for user input
                        textBox1.Visible = True
                        textBox1.Focus()
                        textBox1.SelectAll()
                        Exit For
                    End If
                Next
            End If
        Next
    End Sub

    Private Sub textBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles textBox1.KeyPress
        'call LostFocus Sub in the event user pressed RETURN
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
            ' Change by Deepak: Move to Next Item
            vMoveNext = True

            textBox1_LostFocus(sender, Nothing)
        End If
    End Sub

    Private Sub textBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles textBox1.LostFocus

        Dim selDetail As String
        'hide textbox
        textBox1.Visible = False
        'load old value to detect if there is a change
        selDetail = ListView1.SelectedItems(0).SubItems(selCol).Text

        'change the subitem.text only if the new value is different to the existing one
        'to prevent display of the save butten without changing values
        If selDetail <> textBox1.Text Then
            ListView1.Items(ListView1.SelectedItems(0).Index).SubItems(selCol).Text = textBox1.Text
            'set change flag for not stored information to prevent loss of update by change of selection
            'changeFlage = True
            buttonSave.Visible = True
        End If

        ' Change by Deepak: Move to Next Item
        If vMoveNext = True Then
            MoveNext()
            vMoveNext = False
        End If

    End Sub

	
End Class

Open in new window

0
 
LVL 11

Expert Comment

by:Deepak Lakkad
Comment Utility
Hi,

In ListView, Selected SubItem can be identified by X and Y co-ordinates i.e. Location where Mouse is clicked.

Location cannot be identified with KeyPress Events...

So, it is difficult to find selected Item when particular Key is pressed ...

That's why, I think so, you cannot move to next SubItem when user presses Enter Key.


- Deepak Lakkad
0
 

Author Comment

by:alicealiciasarawak
Comment Utility
I manage to get the Row number and colunn number for the clicked textbox in the subitem.

textBox1.Text = hit.Item.Index
textBox2.Text = selCol
Dim rowV As Integer = Convert.ToInt32(TextBox1.Text)
Dim colV As Integer = Convert.ToInt32(TextBox2.Text) + 1

Now I want to move the focus to the next subitem, how to do so?

I tried to do as following:
ListView1.Items(rowV).SubItems(colV).Selected = True
But got error.

Is there any similar way to do this? Please advice and help.
0
 
LVL 11

Accepted Solution

by:
Deepak Lakkad earned 500 total points
Comment Utility
[x] Attachment Details  
   
   

Hi

I have attached a file here for DGV.

It contains a custom control with a Test Class.

may be this will solve your requirements (Replace ListItem with DataGridView)

- Deepak Lakkad MyDGV.zip
0
 

Author Closing Comment

by:alicealiciasarawak
Comment Utility
Partially solved
0

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

Suggested Solutions

Title # Comments Views Activity
.net VBA word safemode 1 22
Trouble with References... 5 21
Unable  to create new object 9 16
XML & .net 5 0
IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

728 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

10 Experts available now in Live!

Get 1:1 Help Now