Subject: Beginner NEED HELP!!
Posted By: nvillare Post Date: 9/18/2003 11:39:54 AM
I am new to VB.net and am having much difficulty with it.

I am trying to create a form that has a listbox, textbox and a button name remove. THe listbox has 15 items that the user can choose from, they can chose as many as they want. When they press the remove button, the selected items are insrted into the textbox and are displayed in combinations,

ie a b c displays
a ab ac
b ba bc
c ca cb

the button is also suppose to remove the selected items at the same time.

Does anyone have any ideas, I have the form set up, but I am having so much trouble with the code.

Any help is greatly appreciated!!  


Thanks!

nvillare
Reply By: planoie Reply Date: 9/18/2003 12:43:34 PM
In the remove button event, you can loop thru the list control's "Items" collection to retrieve all the selected items.  For each item in the list, you can check the "Selected" property.  Then you can issue a remove command like this: lstMyListControl.Items.Remove(currentItem).  You'll need to use a loop suitable for this operation. You can't use For Each because as soon as you remove an item from the collection (that you are looping thru) the enumerator used in the For Each will break.  Probably need to use something like this...

i = 0
While i < lstMyListControl.Items.Count
    If lstMyListControl.Items(i).Selected Then
        'Do other stuff you need here...
        lstMyListControl.Items.Remove(lstMyListControl.Items(i))
    Else
        i += 1
    End if
End While

Notice that in there you'll need to do something else do create your combinations.  You should probably save off all the selected items to another variable (maybe an ArrayList?) so you can generate the combinations later in the button click event.  Which type of combinations you want to create will dictate how you need to code that part.

Peter
Reply By: nvillare Reply Date: 9/18/2003 1:12:54 PM
Thanks, but I have no idea where to start from. Do I write the code in the button event handler? Does anything go in the List box or the text box? Also, I know I'll need an array, but again I'm not to sure how to start or set it up.

Can anyone help??

Thanks!

nvillare
Reply By: nvillare Reply Date: 9/18/2003 2:14:44 PM
THis is what I have so far, and it's not working Can someone please help!!

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.ListBox1 = New System.Windows.Forms.ListBox()
        Me.Button1 = New System.Windows.Forms.Button()
        Me.TextBox1 = New System.Windows.Forms.TextBox()
        Me.SuspendLayout()
        '
        'ListBox1
        '
        Me.ListBox1.Items.AddRange(New Object() {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o"})
        Me.ListBox1.Location = New System.Drawing.Point(8, 8)
        Me.ListBox1.Name = "ListBox1"
        Me.ListBox1.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended
        Me.ListBox1.Size = New System.Drawing.Size(120, 199)
        Me.ListBox1.TabIndex = 0
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(104, 224)
        Me.Button1.Name = "Button1"
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "Remove"
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(160, 8)
        Me.TextBox1.Multiline = True
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(120, 184)
        Me.TextBox1.TabIndex = 2
        Me.TextBox1.Text = "TextBox1"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 262)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.TextBox1, Me.Button1, Me.ListBox1})
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Integer = 0
        While i < ListBox1.Items.Count
            If ListBox1.Items(i).SelectedIndex <> -1 Then
                ListBox1.Items.RemoveAt(ListBox1.Items(i))
            Else
                i += 1
            End If
        End While
        'If ListBox1.SelectedIndex <> -1 Then
        '    ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
        'End If
    End Sub
End Class

Thanks!

nvillare
Reply By: planoie Reply Date: 9/18/2003 2:44:15 PM
You're on the right track.  I must apologize, I work mostly in ASP.Net so I sometimes miss that a post is regarding windows forms.

In the forms listbox control there is a member called SelectedItems.  This is a collection of all the selected items.  You should be able to do everything that you need with this collection.

Here, this should get you going...

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i, j As Integer
        Dim sBuilder As New System.Text.StringBuilder()
        For i = 0 To ListBox1.SelectedItems.Count - 1
            sBuilder.Append(ListBox1.SelectedItems(i))
            For j = 0 To ListBox1.SelectedItems.Count - 1
                If i <> j Then
                    sBuilder.AppendFormat(" {0}{1} ", ListBox1.SelectedItems(i), ListBox1.SelectedItems(j))
                End If
            Next
            sBuilder.Append(Environment.NewLine)
        Next

        While ListBox1.SelectedItems.Count > 0
            ListBox1.Items.Remove(ListBox1.SelectedItems(0))
        End While
        TextBox1.Text = sBuilder.ToString
    End Sub
End Class



Peter

Go to topic 4262

Return to index page 1043
Return to index page 1042
Return to index page 1041
Return to index page 1040
Return to index page 1039
Return to index page 1038
Return to index page 1037
Return to index page 1036
Return to index page 1035
Return to index page 1034