14 Replies - 2428 Views - Last Post: 11 March 2012 - 07:55 PM Rate Topic: -----

#1 ddowns91  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-March 12

Cannot get number from list box to display in reverse in text box

Posted 10 March 2012 - 06:35 PM

Hi! I'm new here...anyway, I've been trying to solve the following problem and I just keep getting stuck:

Build a small application that fills a collection (list) with 10 sequential numbers, and then prints the collection(list) in reverse order, skipping every other member, until the entire collection has been displayed. For example, if the collection contained the numbers 1 through 10, they would print as:

10, 8, 6, 4, 2, 9, 7, 5, 3, 1 The print out should be in that order no matter what sequential numbers are in the collection (list). If it were 21 through 30, output would be 30,28,26,24,22,29,27,25,23,21 and so forth.

Note: you must use a collection - do not write down the numbers directly to produce the output. Loops should be employed but the loop control variable should be used as an index or subscript number. Your code should work even if I change the list of sequential numbers to something else. Remember that indexes count from 0 first -- the first item in a collection has index 0, the second item in a collection has index 1, and so forth.

I've got the first part of the problem, getting the numbers in the list box, done with no problem. It's the second part I am struggling with. I cannot get the numbers (I am using 1-10) from the list box to display in reverse order in an adjacent text box. Here's my code so far:

Option Explicit On
Option Strict On
Option Infer Off
Public Class Form1


    Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
        'closes the program without error
        Me.Close()
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'this will put numbers in the list box

        For lblOrgList As Double = 1 To 10
            lstOrgValues.Items.Add(lblOrgList.ToString)
        Next lblOrgList

    End Sub
End Class


I've tried everything I can think of for the 2nd part, but I can't figure it out at all! Any help, will be greatly appreciated. Thank you!

Is This A Good Question/Topic? 0
  • +

Replies To: Cannot get number from list box to display in reverse in text box

#2 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Cannot get number from list box to display in reverse in text box

Posted 10 March 2012 - 07:33 PM

For i As Integer = 0 To ListBox1.Items.Count - 1
ListBox2.Items.Add(ListBox1.Items(ListBox1.Items.Count-1-i))
Next

This hasn't been tested so it probably won't work. Please try it though!
Was This Post Helpful? 0
  • +
  • -

#3 ricardosms  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 73
  • View blog
  • Posts: 301
  • Joined: 02-April 10

Re: Cannot get number from list box to display in reverse in text box

Posted 10 March 2012 - 08:43 PM

Yo do like this
1. reverse sort
2. use loop with odd index
3. add to string, add comma
4. use loop with even index
5. add to string, add coma
6. display
Was This Post Helpful? 1
  • +
  • -

#4 ddowns91  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-March 12

Re: Cannot get number from list box to display in reverse in text box

Posted 10 March 2012 - 09:07 PM

View PostDimitriV, on 10 March 2012 - 07:33 PM, said:

For i As Integer = 0 To ListBox1.Items.Count - 1
ListBox2.Items.Add(ListBox1.Items(ListBox1.Items.Count-1-i))
Next

This hasn't been tested so it probably won't work. Please try it though!


I tried it, and all it does is add multiple number tens to the list box and does nothing to the text box--it is still blank.
Was This Post Helpful? 0
  • +
  • -

#5 ricardosms  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 73
  • View blog
  • Posts: 301
  • Joined: 02-April 10

Re: Cannot get number from list box to display in reverse in text box

Posted 10 March 2012 - 10:48 PM

Do:

ListBox1.Sorted = True



Use 2 loops starting with maximum index and counting down, even and odd indexes.
Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,498
  • Joined: 29-May 08

Re: Cannot get number from list box to display in reverse in text box

Posted 10 March 2012 - 11:35 PM

Strictly speaking I would classify the List Control as a Collection, but an UI Elements.

When they say collection, I'm think of an array or Collection.List<T>

It easier to learn vb.net by starting with console applications, since your not transfixed by the UI.
Focusing just of the core parts, to solve the problem.

myCollection -->--[ someFunction ]-->-- myNewCollection

The essence of the problem is how to write the EveryOther element part.
Spoiler

This post has been edited by AdamSpeight2008: 11 March 2012 - 12:50 AM

Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,498
  • Joined: 29-May 08

Re: Cannot get number from list box to display in reverse in text box

Posted 11 March 2012 - 12:32 AM

For an hint look at the documentation about the For Loop
Was This Post Helpful? 0
  • +
  • -

#8 ddowns91  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-March 12

Re: Cannot get number from list box to display in reverse in text box

Posted 11 March 2012 - 06:54 AM

But, how can I get something from a For loop to display numbers in the text box? It always says "text is not a member of double" or "text is not a member of single."
Was This Post Helpful? 0
  • +
  • -

#9 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,498
  • Joined: 29-May 08

Re: Cannot get number from list box to display in reverse in text box

Posted 11 March 2012 - 07:27 AM

Show the code you are using.
Was This Post Helpful? 0
  • +
  • -

#10 ricardosms  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 73
  • View blog
  • Posts: 301
  • Joined: 02-April 10

Re: Cannot get number from list box to display in reverse in text box

Posted 11 March 2012 - 09:46 AM

View Postddowns91, on 11 March 2012 - 08:54 AM, said:

But, how can I get something from a For loop to display numbers in the text box? It always says "text is not a member of double" or "text is not a member of single."


Use <Number>.ToString
Was This Post Helpful? 0
  • +
  • -

#11 ddowns91  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-March 12

Re: Cannot get number from list box to display in reverse in text box

Posted 11 March 2012 - 11:06 AM

Here is the code I have so far. I don't have a "Next" statement for my "For" at the bottom, yet.

Option Explicit On
Option Strict On
Option Infer Off
Public Class Form1


    Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
        'closes the program without error
        Me.Close()
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'this will put numbers in the list box

        For lblOrgList As Double = 1 To 10
            lstOrgValues.Items.Add(lblOrgList.ToString)
        Next lblOrgList

    End Sub

    Private Sub btnReorder_Click(sender As System.Object, e As System.EventArgs) Handles btnReorder.Click
        'this button will reorder the numbers in the list box and display them in reverse order skipping every other number
        For txtReordered As Double = 10 To 1 Step -1
    End Sub
End Class


I know that I need to have 2 "For" statements, one with a step that will display the numbers "10,8,6,4,2" and one that will display the numbers "9,7,5,3,1"...I just don't know how to code it! It's driving me nuts!
Was This Post Helpful? 0
  • +
  • -

#12 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,498
  • Joined: 29-May 08

Re: Cannot get number from list box to display in reverse in text box

Posted 11 March 2012 - 12:48 PM

If the problem is to big or complex, break the problem down it smaller less complex parts.


1. Put numbers in to a collection.
2. Get a sorted ordering of that collection.
3. Get a Reversed ordering of that collection.
4. Go through that list, outputting the first then every other number (or step over it)
How do I access an element at particular index of a collection?
What is stepping distance between these the index of these items?
5. Go through the list again, output from the second than every other number (or step it)
What is stepping distance between these the index of these items?
Was This Post Helpful? 0
  • +
  • -

#13 ddowns91  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-March 12

Re: Cannot get number from list box to display in reverse in text box

Posted 11 March 2012 - 03:24 PM

Okay, so I have deduced that I need the following "For" statements, I think"

For "whatever should go here" as Double= 10 to 1 Step -1
     Not sure what line of code I would need here
For "whatever should go here"

For "whatever should go here" as Double= 10 to 1 Step -2
     Not sure what line of code I would need here
For "whatever should go here"


As for the middle, I'm clueless as what to put! Whatever I try, I keep getting errors. It can't be a String in a For loop because the type does not support the required operators. For those who may want to know, this link here, http://elearning.kct.../xid-95510164_2, is a picture of what the finished product should look like. Different numbers were used, but I am allowed to use whatever I want. Maybe this will help some people
Was This Post Helpful? 0
  • +
  • -

#14 meowbits  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 107
  • Joined: 31-January 12

Re: Cannot get number from list box to display in reverse in text box

Posted 11 March 2012 - 03:35 PM

There several ways to reverse a collection of items.

For example,

You could take,

for i as int = 0 to oldListBox(max) -1
newListBox(oldListBox(max) - i)
next


That should help you.

*had crap logic, fixed now

This post has been edited by meowbits: 11 March 2012 - 03:46 PM

Was This Post Helpful? 0
  • +
  • -

#15 ddowns91  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-March 12

Re: Cannot get number from list box to display in reverse in text box

Posted 11 March 2012 - 07:55 PM

Well, I think I have figured it out, at least what I tried appears to have worked. I'm crossing my fingers, and I'm hoping so...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1