Wrox Programmer Forums
|
VB.NET General VB.NET discussions for issues that don't fall into other VB.NET forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 18th, 2003, 11:39 AM
Authorized User
 
Join Date: Jul 2003
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to nvillare
Default Beginner NEED HELP!!

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
__________________
Thanks!

N
 
Old September 18th, 2003, 12:43 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

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.Ite ms(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
 
Old September 18th, 2003, 01:12 PM
Authorized User
 
Join Date: Jul 2003
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to nvillare
Default

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
 
Old September 18th, 2003, 02:14 PM
Authorized User
 
Join Date: Jul 2003
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to nvillare
Default

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
 
Old September 18th, 2003, 02:44 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
c++ beginner, need help please jmarsh56 Visual C++ 0 December 7th, 2005 10:38 AM
Beginner Tilak_1 C# 2 January 21st, 2005 11:19 PM
Beginner [email protected] Beginning PHP 4 December 23rd, 2004 03:22 PM
Need help - C++ beginner Satheesh C++ Programming 13 August 7th, 2004 09:18 PM
Beginner programmed XML 3 February 21st, 2004 05:12 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.