Support Forums

Full Version: VB.NET Help?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey, I'm trying to make a program which picks one text string each time you click a button. I have a bunch of text strings and I was just wondering how I could make it pick a different one each time I click the button.

This doesn't work, but is what I tried:
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox1.Text = ("Item 1") Or TextBox1.Text = ("Item 2") Or TextBox1.Text = ("Item 3") Or TextBox1.Text = ("Item 4") Or TextBox1.Text = ("Item 5")
    End Sub
lol the debugger doesn't have a mind of it's own, you can't give it choices based on no logical calculation.

Code:
Public KeyA() As String = {"item 1", "item 2", "item 3", "item 4", "item 5"}

    Private Shared Function ReturnKey(ByVal InputArray() As String, ByVal LastKeyIndex As Integer)
        Dim Key As New Random
        Return InputArray(Key.Next(0, LastKeyIndex))
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Textbox1.Text = ReturnKey(KeyA, 4)
    End Sub

that's what I would do.

My function ReturnKey takes 2 parameters, first parameter specifies the input string array, which i've created above called KeyA(), and the second parameter is used to identify the last index of that array so that our random calculation doesn't try to specify or return an index out of the array.
Works perfectly Ace, thanks a lot mate.
No problem, it's a function I previously posted on the forum, so I know it works, I created and debugged it myself before I posted to help another member out here.
(01-15-2012, 06:01 AM)AceInfinity Wrote: [ -> ]No problem, it's a function I previously posted on the forum, so I know it works, I created and debugged it myself before I posted to help another member out here.

Ah, I just needed it for my next release. I'll post it on TLF when it's done. I can't post it here because I guess it can be classed as illegals.
(01-15-2012, 08:12 AM)BreShiE Wrote: [ -> ]Ah, I just needed it for my next release. I'll post it on TLF when it's done. I can't post it here because I guess it can be classed as illegals.

TLF's rules isn't any different if you read them.