VS.NET VB Project to C# : Lucky Seven
Creating the user Interface:
-
On the file menu, click the New Project command.
-
Select VB Project and Windows Application. Set the project name and
location. Click OK.
-
Resize the Form so it will be large enough for all your objects.
-
Click the Button control.
-
Move the mouse pointer to the form where you want the button and drag to the
size you want.
Move and resize a button:
-
Drag the button the the right by using the mouse.
-
Resize and reshape as needed.
Add a second command button:
-
Click the Button control in the toolbox.
-
Draw a button below the first button on the form.
-
Move or resize the button.
Add the number labels:
-
Click the Label control in the toolbox, and then place the mouse pointer over
the form.
-
Create a small rectangular box to the right of the first button.
-
Click the label control and draw a label box to the right of the first label
box.
-
Click the label control and draw a third label box to the right of the last
box.
-
Click the label control and create a larger rectangle directly below the two
buttons.
Add a picture
-
Click the picture control in the toolbox.
-
Using the picture control, click and draw a large rectangular box directly beneath the
three number labels.
Set the button properties
-
Click the first button on the form.
-
Double-click the Properties window title bar.
-
Double-click the text property in the left column of the Properties window.
-
Type Spin and press Enter.
-
Open the object drop-down list box at the top of the Properties window.
-
Click button2 in the list box.
-
Double-click the current text property, type End, and press Enter.
Set the number label properties
-
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.
-
Click the text align property, and then click the drop-down list box arrow that
appears to the right.
-
Click the center button in the map.
-
Click the BorderStyle property, and then click the drop-down list box arrow
that appears to the right.
-
Click - Fixed Single in the list box to add a thin border around each
label.
-
Click the Font Property, press the button with the ellipses to open
the font dialogue box.
-
Change the font to Times New Roman, the font style to Bold, and the point size
to 24, and then click OK.
-
Click the form to remove the selection handles from the three labels, and then
click the first label.
-
Double-click the text property, and then press Del.
-
Delete the text in the second and third labels on the form.
Set the descriptive label properties
-
Click the fourth label object on the form.
-
Change the text property to "Lucky Seven".
-
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.
-
Double-click the ForeColor property in the Properties window.
-
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
-
Navigate to the www.mvhs.net/images page
with your browser.
-
Download COINS.wmf by right clicking the mouse and save it in your server
drive.
-
Click the image box object on the form.
-
Click the Size Mode property in the Properties window,
click the drop-down list
box arrow, and then click stretch image.
-
Click the image property. Select the file Coins.wmf in the dialog box, and
then click Open.
-
Click the Visible property. Click the Visible drop-down list box arrow.
-
Click False to make the image invisible when the program starts.
Use the Code Window
-
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.
-
Type End, and press the Down arrow key.
-
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
-
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).
-
Click Button1 in the list box. Go to event
window and select the click event.
-
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
-
On the File menu, click Save All.
Run the program
-
Click the Play arrow button on the toolbar.
-
Click the Spin button.
-
Click the Spin button 15 or 16 more times, watching the results of the spins in
the number windows.
-
When you have finished experimenting with your new creation, Click the End
button and the program stops.
Adding to a Program
-
Load Visual Basic by clicking the VB icon on the desktop.
-
Click the Recent tab in the New Project dialog box.
-
Double-click MyLucky7 to load the Lucky Seven program from disk.
-
Double-click the form (not one of the objects) to display the Form_Load
procedure.
-
Press the Spacebar four times, type Randomize, and press the Down Arrow key.
-
Run the new version of Lucky Seven, and then save the project to disk.
Build this program in C#
- Keep your Lucky Seven program open.
- Click the vs.net icon to open another IDE.
- Select a new windows application project template in C#.
- Specify the file name and location.
- Leave the C# IDE open and switch to the VB project by pressing alt
tab or clicking the project name on the test bar.
- Select the objects on the VB project form by pressing with control
A and copy them to the
clipboard using control C.
- Switch to the C# project and paste using control V.
- 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
- Click the play button to execute the file.