Solved

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

Posted on 2010-11-29
12
1,258 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
12 Comments
 
LVL 17

Expert Comment

by:Zhaolai
ID: 34236233
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
ID: 34236345
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
ID: 34236543
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 11

Expert Comment

by:Deepak Lakkad
ID: 34236608
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
ID: 34237842
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
ID: 34237910


Can you put your code here?
0
 

Author Comment

by:alicealiciasarawak
ID: 34244407
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
ID: 34244457
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
ID: 34246880
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
ID: 34247146
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
ID: 34255081
[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
ID: 34331343
Partially solved
0

Featured Post

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

739 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