1 [VB.NET] Randomizer {{TUT}} Tue Feb 08, 2011 12:50 am
Apocalypse
Master
What i am simply going to show you today, is how to make a program where you add items in a listbox, you press a button and a random word or name you added will pop up. This is a great tool for raffles.
What you will need:
1 ListBox
1 TextBox
1 Label
3 Buttons
First thing your gonna do is Re-Size the form so its 320x470, once you did that, add the list box, and resize that, but dont take up all the room. then, on the bottom right corner of the list box, just bellow it, place your label. then, place the buttons, and the textbox.
Rename Label1 to "0 Items"
Rename Button1 to "Add"
Rename Button2 to "Randomize"
Rename Button3 to "Clear"
once you have done that, it should look something like this:
thats done now all wee need is the coding.
Double Click Form1, and just bellow Public Class Form1 you are going to enter the following:
then, under where it says " Private Sub Form1_Load" type this in:
Now go back to the Design Page, and double click TextBox1. Once in the coding page, type this in:
Next thing your gonna do, Right above Private Sub TextBox1_TextChanged, you will type:
Back to the Design Page, double click Button1 and type:
Now go back and double click Button2, type:
Last thing, double click Button3:
Thats basically it. if you need any help, or have any questions, feel free to PM Me, if this helped at all, or if you liked the tutorial, just + Rep me.
What you will need:
1 ListBox
1 TextBox
1 Label
3 Buttons
First thing your gonna do is Re-Size the form so its 320x470, once you did that, add the list box, and resize that, but dont take up all the room. then, on the bottom right corner of the list box, just bellow it, place your label. then, place the buttons, and the textbox.
Rename Label1 to "0 Items"
Rename Button1 to "Add"
Rename Button2 to "Randomize"
Rename Button3 to "Clear"
once you have done that, it should look something like this:
thats done now all wee need is the coding.
Double Click Form1, and just bellow Public Class Form1 you are going to enter the following:
- Code:
Dim I As Integer = 1
Dim Int As Integer = 1
then, under where it says " Private Sub Form1_Load" type this in:
- Code:
Button1.Enabled = False
Now go back to the Design Page, and double click TextBox1. Once in the coding page, type this in:
- Code:
If TextBox1.Text = "" Then
Button1.Enabled = False
Else
Button1.Enabled = True
End If
Next thing your gonna do, Right above Private Sub TextBox1_TextChanged, you will type:
- Code:
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
If TextBox1.Text = "" Then
Button1.Enabled = False
Else
Button1.PerformClick()
End If
End If
End Sub
Back to the Design Page, double click Button1 and type:
- Code:
Dim A As Object
A = TextBox1.Text
ListBox1.Items.Add(I.ToString + ". " + A)
I = I + 1
TextBox1.Clear()
TextBox1.Focus()
If ListBox1.Items.Count = 1 Then
Label1.Text = "1 Item"
Else
Label1.Text = ListBox1.Items.Count.ToString + " Items"
End If
Now go back and double click Button2, type:
- Code:
If ListBox1.Items.Count = 1 Then
MsgBox("Error, you need atleast 2 or more items.", MsgBoxStyle.Critical, "Apocalypse Randomizer")
Else
Dim Random As New Random
Random.Next(0, ListBox1.Items.Count)
Dim A As Object
A = Random.Next(0, ListBox1.Items.Count.ToString)
Dim B As Object
B = ListBox1.Items.Item(A)
MsgBox("The Winner Is " + B + "! Gratz!", 0, "Randomizer")
End If
Last thing, double click Button3:
- Code:
ListBox1.ClearSelected()
Thats basically it. if you need any help, or have any questions, feel free to PM Me, if this helped at all, or if you liked the tutorial, just + Rep me.