Results 1 to 7 of 7

Thread: Printing Arrays in Listbox

  1. #1

    Thread Starter
    Registered User
    Join Date
    Jul 2021
    Posts
    2

    Question Printing Arrays in Listbox

    Button 1 prompts users to populate an array while button 2 should print that array into a list box.
    When I run it, button 1 seems to function properly, but clicking button 2 doesnt print anything. What should I change?

    Name:  image_2021-07-22_182538.png
Views: 150
Size:  49.9 KB

  2. #2
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    703

    Re: Printing Arrays in Listbox

    use
    Code:
    redim my_original(elems-1)
    instead of
    Code:
    dim my_original(elems-1) as
    else you create a new local variable named my_original and the you store your data in this new variable that will disappear at the end of the method and so you cannot populate your listbox as the other "local" variable (at the form level) my_original variable is empty when you use it in button 2.

    by the way never put a picture of code (we cannot test it without rewrite everything), copy/paste you code and use the # icon to wrap the code posted.

    In this case also I would advise to use a list of string instead of an array.

    also create you own "input box" with a form (that you can named dialog) so you can add all the features you want like buttons asking if you want more entry or to stop, etc so you don't need to ask for the number of elements

    Side comment : I know a gentleman named JMC that will jump like a devil out of the box at the sight of the use of inputbox
    Last edited by Delaney; Jul 22nd, 2021 at 05:37 PM. Reason: typo and details
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    10,441

    Re: Printing Arrays in Listbox

    It's worth mentioning that you should probably be using a List(Of String) for this since the length of the collection can grow or shrink. Also, to improve performance, you can use AddRange. Finally, you really should be using TryParse if you're relying on user input (like from an InputBox, which is crap).

    Take a look at this example:
    Code:
    Private ReadOnly My_Original As List(Of String) = New List(Of String)
    Pirvate Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim count As Integer
        Dim input = InputBox("Enter Number of Elements (1-100)", "Setting Collection Size")
        If (Not Integer.TryParse(input, count) OrElse count < 1 OrElse count > 100) Then
            MessageBox.Show("You've entered an invalid number.", "Invalid Input", MessageBoxButtons.Ok, MessageBoxIcon.Error)
            Return
        End If
    
        My_Original.Clear()
        For index = 0 To count
            My_Original.Add(InputBox("Enter Values", "Setting Collection Values"))
        Next
    End Sub
    
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        ListBox1.Items.AddRange(My_Original.ToArray())
    End Sub
    "Code is like humor. When you have to explain it, it’s bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    33,471

    Re: Printing Arrays in Listbox

    While normally a List would be advisable, I'm going to caution against it. This reeks of a homework assignment; prasumably to learn how to use arrays. THis is also a good time to learn debugging skills. When something doesen't go according to plan, the first thing you should do is set breakpoints, and then step through the code. Make sure that the things you think they are are what they are; often they are not. This is one of those times, and that's because you were basically using two variables by the same name, and not using the one you thought you were.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Registered User
    Join Date
    Jul 2021
    Posts
    2

    Re: Printing Arrays in Listbox

    Thank you very much for the explanation. I did try to use TryParse in my previous versions of the code but kept getting confused where to set it.
    Just a small question though. In these kinds of situations where we're trying to get user input, is there an alternative to inputboxes that a beginner like me can use?

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,470

    Re: Printing Arrays in Listbox

    Quote Originally Posted by HLYF View Post
    is there an alternative to inputboxes that a beginner like me can use?
    InputBox is a function that, internally, creates a form with a TextBox and a couple of Buttons and then returns a String based on what's in the TextBox and what Button was clicked. You should just design your own form with the appropriate control(s) on it for the requirements of your application, display it like any other form and then get data from it as required. For instance, you could create a form with a TextBox and two Buttons, call its ShowDialog method and then get the Text of the TextBox via a property if and only if ShowDialog returns OK. If you actually want a number rather than a String then you could use a NumericUpDown rather than a TextBox.

  7. #7
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    10,441

    Re: Printing Arrays in Listbox

    InputBoxes can be useful in a narrow set of circumstances, but in this case I would not advocate using one. Think about the UX for a moment: you prompt the user for a number between 1 - 100, the user enters 100, the user then must deal with 100 separate InputBox popups with no way to escape except to kill the application via the task manager.

    Is this actually a homework assignment? If not, then I would propose a dramatically different approach. But if it is, then you might as well leave it be to get the grade.
    "Code is like humor. When you have to explain it, it’s bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width