Next Empty Textbox

rickb

Freshman
Joined
Dec 31, 2002
Messages
26
Location
St. Louis, Missouri
Here's what I have and what I want to do: On a form, I have a listview, a context menu (cmSouth) and a series of textboxes in a groupbox. The context menu is linked to the listview. What I want to happen is, when i right-click on an item in the listview, I want to move that item to the next empty textbox in the groupbox named after the contextmenu item that is clicked.

Here is the code I've been playing with:

Code:
Private Sub TextboxList()
        Dim i As Integer = 0
        Dim ctrl As Control


        For Each ctrl In Me.Controls
            If TypeOf ctrl Is TextBox Then
                dragbox = DirectCast(ctrl, TextBox)
                If dragbox.Name = "Textbox" & i.ToString Then
                    If dragbox.TextLength = "" Then
                        i += 1
                        dragbox.Text = Me.ListView1.SelectedItems(0).Text.ToString
'The below line is commented out because I've been trying different things and I didn't want to delete it.
                        'AddHandler cmSouth.Click, AddressOf cmSouth_Click
                    End If

                End If
            End If

        Next ctrl

    End Sub

Private Sub cmSouth_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim dragbox() As TextBox = {TextBox1, TextBox2,TextBox3,Textbox4}

        dragbox(i).Text = Me.ListView1.SelectedItems(0).Text.ToString

'This is left over from when I was trying to do this with the listbox.
Me.ListBox1.Items.Add(Me.ListView1.SelectedItems(0).Text).ToString()
        
    End Sub

None of this works, and I'm sure the code isn't even close to being able to work. I would appreciate any guidance.
 
Ok, I found this on a Google search and altered it to fit my needs. I added two groupboxes.

Code:
Private Sub cmSouth_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmSouth.ItemClicked
        Dim Group As New GroupBox

        Select Case Group.Name
            Case "GroupBox1"
                Dim c As Control
                For Each c In Group.Controls
                    If c.Text = String.Empty Then
                        c.Text = Me.ListView1.SelectedItems(0).Text.ToString
                        Me.ListView1.Items.Remove(Me.ListView1.SelectedItems(0))
                        c.Focus()
                        Exit Sub
                    End If
                Next
            Case "GroupBox2"
                Dim c As Control
                For Each c In Group.Controls
                    If c.Text = String.Empty Then
                        c.Text = Me.ListView1.SelectedItems(0).Text.ToString
                        Me.ListView1.Items.Remove(Me.ListView1.SelectedItems(0))
                        c.Focus()
                        Exit Sub
                    End If
                Next

        End Select
    End Sub

This code works like I want it to if I don't have it in the Select Case statement, and doesn't when I wrap it in the Select Case. I know I must have the Select set up incorrectly, but I'm not finding it. Any ideas?
 
I'm not clear on exactly what the code is doing or how the app works.

First of all, are the menu items and group boxes predefined, or are they created dynamically at runtime? If they are predefined it would probably make more sense to have a separate event handler for each menu item's Click event.

Also, you haven't really explained how the code is not working when you use the Select Case. I'm guessing the problem is partly related to this code:
Code:
Dim Group As New GroupBox

Select Case Group.Name
You are creating a new group box every time you run this code. It looks like you want to access an existing group box. How you access the group box will again depend on whether they are generated dynamically or predefined. I think it would help you a lot to work this sort of issue by starting with some pseudo-code.

In this case, what you want will probably be along the lines of:
Code:
Sub ContextMenuItem[COLOR="Red"]A[/COLOR]_Click(parameters)
     Dim text As String = Me.ListView1.SelectedItems(0)
     Me.ListView1.Items.Remove(Me.ListView1.SelectedItems(0))
     AddItemToGroupBox(GroupBox[COLOR="Red"]A[/COLOR], text)
End Sub

Sub ContextMenuItem[COLOR="Red"]B[/COLOR]_Click(parameters)
     Dim text As String = Me.ListView1.SelectedItems(0)
     Me.ListView1.Items.Remove(Me.ListView1.SelectedItems(0))
     AddItemToGroupBox(GroupBox[COLOR="Red"]B[/COLOR], text)
End Sub

Sub AddItemToGroupBox(group As GroupBox, text As String)
    For Each C As Control In group
        If TypeOf C Is TextBox And String.IsNullOrEmpty(C.Text) Then
            C.Text = text
            Return
        End If
    Next
End Sub
Here each menu item has its own event handler, which removes the item from the listview and calls a sub that will add the text to the first empty textbox in the specified group box.

Hope that helps.
 
Thanks, marble-eater.

The menu items and the groupboxes are predefined.

I'm playing with two menu items and two groupboxes for the moment; eventually, there will be 12 of each. I had thought about an event for each menu item, but I was hoping to use one event for all 12 by using a Select Case statement. If it's not possible, I'm ok with that.

Obviously I'm not adept at using the Select Case statement, because when the code in the cmSouth_Click event is not wrapped in Select Case, a right click on a listview item sends it to the groupbox associated with the context menu item selected and removes it from the listview. Wrap it in the Select Case, nothing happens. Maybe I can't use Select Case in this instance; I really don't know.

Thanks again. I appreciate all help.
 
A select case works great when you need to make a decision based on a string or numeric value, but it doesn't work as well for most other types, such as controls. You can do a select case with the control's Name property, but that doesn't offer any advantage over an if statement.

You can use a single event handler, but you need to identify which menu item raised the event and act based on that. For example, the two code listings will produce the same results:
Code:
Private Sub MenuStrip1_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles MenuStrip1.ItemClicked
    [COLOR="Blue"]If e.ClickedItem Is menuItem1 Then[/COLOR]
        Dim text As String = Me.ListView1.SelectedItems(0)
        Me.ListView1.Items.Remove(Me.ListView1.SelectedItems(0))
        AddItemToGroupBox(GroupBoxB, text)
    [COLOR="Blue"]ElseIf e.ClickedItem Is menuItem2 Then[/COLOR]
        Dim text As String = Me.ListView1.SelectedItems(0)
        Me.ListView1.Items.Remove(Me.ListView1.SelectedItems(0))
        AddItemToGroupBox(GroupBoxB, text)
    End If
End Sub
Code:
Private Sub munuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) [COLOR="Blue"]Handles munuItem1.Click[/COLOR]
    Dim text As String = Me.ListView1.SelectedItems(0)
    Me.ListView1.Items.Remove(Me.ListView1.SelectedItems(0))
    AddItemToGroupBox(GroupBoxB, text)
End Sub

Private Sub menuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) [COLOR="Blue"]Handles menuItem2.Click[/COLOR]
    Dim text As String = Me.ListView1.SelectedItems(0)
    Me.ListView1.Items.Remove(Me.ListView1.SelectedItems(0))
    AddItemToGroupBox(GroupBoxB, text)
End Sub
I don't see any compelling reason to choose one over another. I personally would prefer the second one because I'd rather separate the code for different controls into different functions, but that's mostly a matter of taste.
 
Back
Top