Results 1 to 18 of 18

Thread: [RESOLVED]Problem regarding listbox and textbox

  1. #1
    New Member SOMALIA's Avatar
    Join Date
    Apr 12
    Posts
    8

    [RESOLVED]Problem regarding listbox and textbox


    As you can see at the above picture, im trying to make a program in which if the textbox text and the listbox text are the same order, which they are in the picture, a bluie fonted text saying well done appears. However only the red text appears which is only meant to come when the order of the text in both listbox and textbox isnt the same.

    Here is the code that i used for this:
    listarrays = name of listbox
    textbox1 = name of textbox
    l is the name of the label for correct answer(blue font)
    label3 is name of label with red font which should only appear for wrong answer



    Code:
     Dim text As String = Me.TextBox1.Text
            For Each item As Object In listarrays.Items
                If item.ToString = Me.TextBox1.Text Then
                    l.Visible = True
                Else
                    Label3.Visible = True
                End If
            Next
    Some people told me to use another textbox instead of the listbox but thats just making itmuch harder for me as I already made the full code working with the listbox. My lecturer told me i need to convert both in string so both textbox and listbox are compatible and i did so (see code above) but the correct label doesnt show;p

    Any help would be appreciated
    Last edited by SOMALIA; Apr 27th, 2012 at 10:22 AM.

  2. #2
    Fanatic Member proneal's Avatar
    Join Date
    May 11
    Posts
    762

    Re: Problem regarding listbox and textbox

    You would want to go through each index of the 'text' variable (as a New String())
    And compare with each corresponding Index of the Listbox.

    You are close, just need to rework it a little

  3. #3
    Burning Member Niya's Avatar
    Join Date
    Nov 11
    Posts
    3,096

    Re: Problem regarding listbox and textbox

    Couple things:

    Are those numbers in the TextBox seperated by spaces ?

    Do you want to display the labels after all the numbers have been entered or do evaluate the order while entering it into the TextBox ?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | Create Sortable BindingList(not mine) | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

  4. #4
    Fanatic Member proneal's Avatar
    Join Date
    May 11
    Posts
    762

    Re: Problem regarding listbox and textbox

    Good points Niya

  5. #5
    New Member SOMALIA's Avatar
    Join Date
    Apr 12
    Posts
    8

    Re: Problem regarding listbox and textbox

    when the button 'go' is clicked it sorts the numbers which are in each button(made an array for that) and displays them into the listbox and at the same time the label should appear depending on whether the answer is correct (if the textbox answer is the same as the listbox in terms of order or not).

    I would like the labels to appear at the same time the go button is clicked, so that the listbox with the correct answers and the appropriate label are shown at the same time.

    Also yeah, the textbox displays the text in each button when they are clicked and there is a space of 10 between each number in the textbox.

    Also as each number in the texbox will be different as there is a random number generator to geenrate 6 random numbers in the 6 buttons, and then each button is clicked and they appear in the textbox. So how would i make each number in the textbox as a strnig as they aren't constants;p
    Last edited by SOMALIA; Apr 26th, 2012 at 04:49 AM.

  6. #6
    Fanatic Member proneal's Avatar
    Join Date
    May 11
    Posts
    762

    Re: Problem regarding listbox and textbox

    Does the child or mentally challenged adult click on the label of numbers to have it entered into the textbox?
    I could charge 500 USD for that sort of question in a Fortune 500 Co.

  7. #7
    New Member SOMALIA's Avatar
    Join Date
    Apr 12
    Posts
    8

    Re: Problem regarding listbox and textbox

    No. The six buttons are initially enabled and they each have one number, which when clicked appear in the textbox in the order of clicking. the labels only show at the end.

  8. #8
    Fanatic Member proneal's Avatar
    Join Date
    May 11
    Posts
    762

    Re: Problem regarding listbox and textbox

    Oh, I see. I like that
    Did you write any more code relevant to your problem since you started this thread, may I ask?

  9. #9
    New Member SOMALIA's Avatar
    Join Date
    Apr 12
    Posts
    8

    Re: Problem regarding listbox and textbox

    I tried a few stuff but they didnt work lol, im thinking now to compare the first item of listbox with first item of textbox and so forth for the remaining items but do not know the code to do that lol;p Would you happen to know the general code to compare items in listbox and textbox man

  10. #10
    Fanatic Member proneal's Avatar
    Join Date
    May 11
    Posts
    762

    Re: Problem regarding listbox and textbox

    vb Code:
    1. '      
    2. Dim x As Integer
    3.         Dim s As String() = New String(5) {"0", "8", "3", "7", "7", "5"}
    4.         ListBox1.Items.AddRange(s)
    5.         TextBox1.Text = "0 2 3 5 7 8"
    6.         For Each item In ListBox1.Items
    7.             If item.ToString = TextBox1.Text.Split(" ")(x) Then
    8.                 MsgBox("matching " + item.ToString)
    9.             End If
    10.             X += 1
    11.         Next
    Last edited by proneal; Apr 26th, 2012 at 10:02 AM. Reason: commented ' what should not have been

  11. #11
    New Member SOMALIA's Avatar
    Join Date
    Apr 12
    Posts
    8

    Re: Problem regarding listbox and textbox

    The problem is that on line 3 it has fixed numbers, whereas in this program there are random generated numbers which are the text for each button here are the codes for each button and and the code for the go button which is meant to:
    1) show the listbox with sorted of the 6 buttons in ascending order which it does nicely.
    2)display either blue or red fonted label depending if both listbox and textbox text match.

    Code for go button:
    Code:
    Private Sub btnGo_Click(sender As System.Object, e As System.EventArgs) Handles btnGo.Click
    
            strnumbers(0) = CStr(btnNumberButton1.Text)
            strnumbers(1) = CStr(btnNumberButton2.Text)
            strnumbers(2) = CStr(btnNumberButton3.Text)
            strnumbers(3) = CStr(btnNumberButton4.Text)
            strnumbers(4) = CStr(btnNumberButton5.Text)
            strnumbers(5) = CStr(btnNumberButton6.Text)
    
            For num As Integer = 0 To strnumbers.Length - 1
                Array.Sort(strnumbers)
                listarrays.Items.Add(strnumbers(num))
    
            Next
    
            Dim x As Integer
            Dim s As String() = New String(5) {strnumbers(0), strnumbers(1), strnumbers(2), strnumbers(3), strnumbers(4), strnumbers(5)}
            listarrays.Items.AddRange(s)
            TextBox1.Text = "0 2 3 5 7 8"
            For Each item In listarrays.Items
                If item.ToString = TextBox1.Text.Split(" ")(x) Then
                    MsgBox("matching " + item.ToString)
                End If
                x += 1
            Next
            
            
    
    
    
            ProgressBar1.Increment(+12.8)
            listarrays.Visible = True
            lblAnswer.Visible = True
        End Sub
    code for button(its the same for all the buttons)
    Code:
    Private Sub btnNumberButton1_Click(sender As System.Object, e As System.EventArgs) Handles btnNumberButton1.Click
    
            TextBox1.Text = TextBox1.Text + "         " + btnNumberButton1.Text
            btnNumberButton1.Enabled = False
            ProgressBar1.Increment(+12.8)
            btnPlay.Enabled = False
    
        End Sub


    the bolded bit is the bit im stuck on everything else is smooth.

  12. #12
    Fanatic Member proneal's Avatar
    Join Date
    May 11
    Posts
    762

    Re: Problem regarding listbox and textbox

    Are you wanting something similar to this?
    vb Code:
    1. Dim matched As Integer
    2.         For Each item In ListBox1.Items
    3.             If item.ToString = s(x) Then
    4.                 matched += 1
    5.             End If
    6.             x += 1
    7.         Next
    8.         If matched = 6 Then
    9.             lblAnswer.Visible = True
    10.             MsgBox("Number of matches: " + matched.ToString)
    11.         End If

  13. #13
    New Member SOMALIA's Avatar
    Join Date
    Apr 12
    Posts
    8

    Re: Problem regarding listbox and textbox

    Thanks man it works and shows the correct label but the code you gave me shows the blue fonted label even if the order is different, since both boxes have the same numbers, just in different order. Would it be possible to add something to that code that would only show the blue label if the order of both boxes are the same;p Thanks alot btw man i think im almost there



    Code:
    Dim x As Integer
            Dim s As String() = New String(5) {strnumbers(0), strnumbers(1), strnumbers(2), strnumbers(3), strnumbers(4), strnumbers(5)}
    
            Dim matched As Integer
            For Each item In listarrays.Items
                If item.ToString = s(x) Then
                    matched += 1
                End If
    
                x += 1
            Next
            If matched = 6 Then
                l.Visible = True
            Else
                Label3.Visible = True
            End If
    This is what I got atm. Thing is i think i gotta replace the New String(5) {strnumbers(0), strnumbers(1), strnumbers(2), strnumbers(3), strnumbers(4), strnumbers(5)}

    with each item in the textbox but i dunno how to identify them

    PLZ HELP ME i have to hand this in soon and this is the only bit left (literally 99% done);p
    Last edited by SOMALIA; Apr 27th, 2012 at 06:22 AM.

  14. #14
    Fanatic Member proneal's Avatar
    Join Date
    May 11
    Posts
    762

    Re: Problem regarding listbox and textbox

    Array.Sort(s)?

    lblAnswer was not descriptive, and so I guessed if that was the correct label that shows correct blue message.
    I take it Label3 is for red, and l for blue?
    -
    I wont be back for several hours, so...sorry if Array.Sort(s) is not helping.

    Niya is subscribed to this Thread.
    Niya knows the solution.

    Good Luck!

  15. #15
    New Member SOMALIA's Avatar
    Join Date
    Apr 12
    Posts
    8

    Re: Problem regarding listbox and textbox

    yeah label3 is for red and l is for blue;p

    What i think is either i can replace the:
    Dim s As String() = New String(5) {strnumbers(0), strnumbers(1), strnumbers(2), strnumbers(3), strnumbers(4), strnumbers(5)} with
    Dim s As String() = New String(5) {put each item of the textbox inside here but i dunno how to call each indiviual item)}

    Since the strnumbers are for each button and of course the listbox will have each of the button text just in a different number. So i need to replace them with each item in the textbox.

    And yh lol, array.sort(s) doesnt work, it gives error lol

    Some info about the textbox:

    When each button is clicked the text in there appears from the text. the textbox itself has no coding only the buttons have coding which i posted a few posts ago(post#11) and they are coded to show there text in the textbox.
    Last edited by SOMALIA; Apr 27th, 2012 at 06:45 AM.

  16. #16
    New Member SOMALIA's Avatar
    Join Date
    Apr 12
    Posts
    8

    Re: Problem regarding listbox and textbox

    managed to fix it guys, thanks.

  17. #17
    Fanatic Member proneal's Avatar
    Join Date
    May 11
    Posts
    762

    Re: [RESOLVED]Problem regarding listbox and textbox

    Well, hells bells, alrighty then!
    Great!

    Woo hoo!

    Good Luck!

  18. #18
    Burning Member Niya's Avatar
    Join Date
    Nov 11
    Posts
    3,096

    Re: Problem regarding listbox and textbox

    Quote Originally Posted by proneal View Post
    Niya is subscribed to this Thread.
    Niya knows the solution.
    I was away for almost 12 hours. Daddy had to use my monitor almost the entire day today so I wasn't able to check on this. Looks like you got it solved though. Congrats
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | Create Sortable BindingList(not mine) | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

Posting Permissions

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