VS.NET VB Project to C# : Lucky Seven

Creating the user Interface:

  1. On the file menu, click the New Project command.
  2. Select VB Project and Windows Application. Set the project name and location. Click OK.
  3. Resize the Form so it will be large enough for all your objects.
  4. Click the Button control.
  5. Move the mouse pointer to the form where you want the button and drag to the size you want.

Move and resize a button:

  1. Drag the button the the right by using the mouse.
  2. Resize and reshape as needed.

Add a second command button:

  1. Click the  Button control in the toolbox.
  2. Draw a button below the first button on the form.
  3. Move or resize the button.

Add the number labels:

  1. Click the Label control in the toolbox, and then place the mouse pointer over the form.
  2. Create a small rectangular box to the right of the first button.
  3. Click the label control and draw a label box to the right of the first label box.
  4. Click the label control and draw a third label box to the right of the last box.
  5. Click the label control and create a larger rectangle directly below the two buttons.

Add a picture

  1. Click the picture control in the toolbox.
  2. Using the picture control, click and draw a large rectangular box directly beneath the three number labels.

Set the button properties

  1. Click the first button on the form.
  2. Double-click the Properties window title bar.
  3. Double-click the text property in the left column of the Properties window.
  4. Type Spin and press Enter.
  5. Open the object drop-down list box at the top of the Properties window.
  6. Click button2  in the list box.
  7. Double-click the current text property, type End, and press Enter.

Set the number label properties

  1. Click the first number label, and then, holding down the Shift key, click the second and third number labels.
    When all three labels are selected, release the Shift key.
  2. Click the text align  property, and then click the drop-down list box arrow that appears to the right.
  3. Click the center button in the map.
  4. Click the BorderStyle property, and then click the drop-down list box arrow that appears to the right.
  5. Click - Fixed Single in the list box to add a thin border around each label. 
  6. Click the Font Property, press the button with the ellipses to open the font dialogue box.
  7. Change the font to Times New Roman, the font style to Bold, and the point size to 24, and then click OK. 
  8. Click the form to remove the selection handles from the three labels, and then click the first label.
  9. Double-click the text property, and then press Del.
  10. Delete the text  in the second and third labels on the form.

Set the descriptive label properties

  1. Click the fourth label object on the form.
  2. Change the text property to "Lucky Seven".
  3. Double-click the Font property, and use the Font dialog box to change the font to Arial, the font style to Bold,
    and the point size to 20. Click OK.
  4. Double-click the ForeColor property in the Properties window. 
  5. Click the drop down button on the right and push the Custom Palette tab, then click the box containing dark purple.

Set the Picture Box properties

  1. Navigate to the www.mvhs.net/images page with your browser.
  2. Download COINS.wmf by right clicking the mouse and save it in your server drive. 
  3. Click the image box object on the form.
  4. Click the Size Mode property in the Properties window,
    click the drop-down list box arrow, and then click stretch image.
  5. Click the image property. Select the file Coins.wmf in the dialog box, and then click Open.
  6. Click the Visible property. Click the Visible drop-down list box arrow.
  7. Click False to make the image invisible when the program starts.

Use the Code Window 

  1. Double-Click the End button on the form. Inside the Code window are program statements that mark the beginning and the end of this particular Visual Basic subroutine. The body of the procedure always fits between these lines and is executed whenever a user activates the interface element associated with the procedure.
  2. Type End, and press the Down arrow key.
  3. Move the cursor to the beginning of the line with the End statement in it, and press the Spacebar four times.

Write code for the Spin Button

  1. Double click the Spin Button. This will open the Object drop-down list box in the Code window (list on the top left that states classname).
  2. Click Button1 in the list box. Go to event window and select the click event.
  3. Type the program lines below between the Private Sub and End Sub statements, pressing Enter
    after each line and taking care to type the program statements exactly as they appear below.

For vs.net

PictureBox1.Visible = False 'hide coins

   Label1.Text = Int(Rnd() * 10) 'pick numbers
  
Label2.Text = Int(Rnd() * 10)
   Label3.Text = Int(Rnd() * 10)

'if any caption is 7 display coin stack and beep

   If (Label1.Text = 7) Or (Label2.Text = 7) _
    
Or (Label3.Text = 7) Then
         
PictureBox1.Visible = True

   Beep()

End If
_________________________________________________

Private Sub Command2_Click()
    End
End Sub

Save the Lucky 7 program

  1. On the File menu, click Save All.

Run the program

  1. Click the Play arrow button on the toolbar.
  2. Click the Spin button.
  3. Click the Spin button 15 or 16 more times, watching the results of the spins in the number windows.
  4. When you have finished experimenting with your new creation, Click the End button and the program stops.

Adding to a Program

  1. Load Visual Basic by clicking the VB icon on the desktop.
  2. Click the Recent tab in the New Project dialog box.
  3. Double-click MyLucky7 to load the Lucky Seven program from disk.
  4. Double-click the form (not one of the objects) to display the Form_Load procedure.
  5. Press the Spacebar four times, type Randomize, and press the Down Arrow key.
  6. Run the new version of Lucky Seven, and then save the project to disk.

 Build this program in C#

  1. Keep your Lucky Seven program open.
  2. Click the vs.net icon to open another IDE.
  3. Select a new windows application project template in C#.
  4. Specify the file name and location.
  5. Leave the C# IDE open and switch to the VB project by pressing alt tab or clicking the project name on the test bar.
  6. Select the objects on the VB project form by pressing with control A and copy them to the clipboard using control C.
  7. Switch to the C# project and paste using control V.
  8. Now modify the code as follows (keep the the functions separate):
private void Button2_Click(object sender, System.EventArgs e)
 //the parameter list 1 item indicates the object the event happened to
 //the parameter list 2 item is the even information
		{
			Application.Exit();  //like end in VB
		}

		private void Button1_Click(object sender, System.EventArgs e)
		{
			Random dice = new Random();  
                           //creating a new instance of a random object
			PictureBox1.Visible = false; 
			int rand1 = dice.Next(1,9);
                           //a random integer between 1 and 9 is received
			int rand2 = dice.Next(1,9);
			int rand3 = dice.Next(1,9);
			Label1.Text = rand1.ToString();
                            //ToString converts an object reference 
                            //to a string representation
			Label2.Text = rand2.ToString();
			Label3.Text = rand3.ToString();

			if (rand1 == 7 || rand2 == 7 || rand3 == 7)  // or is ||
			{
				PictureBox1.Visible = true;
			}
		}

Run the C# Program

  1. Click the play button to execute the file.