Monday, February 28, 2011

Programming 2 */ A few programs with Loops */

'Loops
 Public Class Form1  
   '1. Put a textbox and a button on the form. The user must enter a number and press the button.   
   'When the button is pressed, it will add that many buttons to the form.  
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click  
     Dim num As Integer  
     num = CInt(TextBox1.Text)  
     For num = 1 To num  
       ListBox1.Items.Add(num)  
     Next  
   End Sub  
   Private Sub TextBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseClick  
     ListBox1.Items.Clear()  
   End Sub  
   '2. If you were to put away a certain amount of money every month, how many years would it take   
   'you to save up $10,000. Use a textbox to gather the amount that is to be put away every month.   
   'Use a label to display the number of years it would take.   
   Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click  
     Dim pay, count, total, goal As Integer  
     goal = 10000  
     count = 1  
     pay = CInt(TextBox2.Text)  
     Do While total < goal  
       total += pay  
       count += 1  
     Loop  
     Label1.Text = FormatNumber((count / 12), 1) & " years left until you get to $10,000.00"  
   End Sub  
   '3. Write a program that will create a times table.  
   '1 2 3 4 5  6  7  8  9 10  
   '2 4 6 8 10 12  14 16 18 20  
   '..…  
   'You could use labels or buttons to hold the numbers. Generate these dynamically and add them   
   'to the Me.Controls collection.  
   'You will have to use two loops, one inside of the other to get this to work.  
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load  
     For x = 1 To 10  
       For y = 1 To 10  
         Dim button As New Button  
         button.Location = New Point(35 * x, 25 * y)  
         button.Width = 35  
         Me.Controls.Add(button)  
         button.Text = (x * y)  
       Next  
     Next  
   End Sub  
 End Class  

No comments:

Post a Comment

Classes

Programming II

Advanced Relational Database

Followers