Now we will try to create a simple application, the random number generator. It is called “random” because the numbers displayed will be randomized. However, it is not completely random. Why is that? Because we have previously determined what numbers will be displayed.
Let's get started right away!
Step One:
- Add 1 TextBox and 1 Button to the form.
- Next, design the appearance according to your taste - but leave the component names as default (for example: TextBox1, Button1, etc).
- Then, change the text on the Button to “Generate” through the Properties section.
- After the appearance is complete, we enter the coding section.
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
On Error Resume Next
TextBox1.Text = Int(Rnd() * 8)
Select Case TextBox1.Text
Case 0
TextBox1.Text = "masukan key disini"
Case 1
TextBox1.Text = "masukan key disini"
Case 2
TextBox1.Text = "masukan key disini"
Case 3
TextBox1.Text = "masukan key disini"
Case 4
TextBox1.Text = "masukan key disini"
Case 5
TextBox1.Text = "masukan key disini"
Case 6
TextBox1.Text = "masukan key disini"
Case 7
TextBox1.Text = "masukan key disini"
Case 8
TextBox1.Text = "masukan key disini"
End Select
End Sub
End Class
Notes:
- You can later replace the red text with whatever you want to display. Make sure you already have the data that will be displayed randomly.
- The Int(Rnd() * 8) part means we will take random numbers from 0 to 7. If you have more data (for example 9 or 10 items), change the number 8 according to the total amount of data you have.
Example :
Case 1
TextBox1.Text = "masukan key disini"
Case 2
TextBox1.Text = "masukan key disini"
Case 3
TextBox1.Text = "masukan key disini"
Case 4
TextBox1.Text = "masukan key disini"
Case 5
TextBox1.Text = "masukan key disini"
Case 6
TextBox1.Text = "masukan key disini"
Case 7
TextBox1.Text = "masukan key disini"
Case 8
TextBox1.Text = "masukan key disini"
Case 9
TextBox1.Text = "masukan key disini"
Adding New Data
If you want to add more data, simply copy-paste the following code snippet:
case 10
TextBox1.Text = "masukan key disini"
But remember, change the number 8 according to the number of additional items you enter, such as 9, 10, and so on.
Hope This Helps
0 Post a Comment:
Gunakan kolom komentar dengan baik dan bijak.