End Sub in .NET

Print QR Code 2d barcode in .NET End Sub

23. End Sub
Make Denso QR Bar Code In .NET
Using Barcode drawer for .NET framework Control to generate, create QR Code ISO/IEC18004 image in .NET framework applications.
www.OnBarcode.com
QR Code ISO/IEC18004 Scanner In .NET Framework
Using Barcode recognizer for Visual Studio .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
} 41. Add the following code for the Deal method: 42. Visual Basic 43. The deck is empty after dealing the cards. 44. Public Sub Deal(ByVal hands() As Hand)
Barcode Drawer In .NET Framework
Using Barcode creation for .NET framework Control to generate, create bar code image in .NET framework applications.
www.OnBarcode.com
Recognize Barcode In .NET
Using Barcode reader for Visual Studio .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
45. 46. 47. 48. 49. 50. 51. 52. 53. 55.
Denso QR Bar Code Creation In Visual C#
Using Barcode maker for VS .NET Control to generate, create Denso QR Bar Code image in .NET applications.
www.OnBarcode.com
Generate QR Code 2d Barcode In .NET
Using Barcode generator for ASP.NET Control to generate, create Quick Response Code image in ASP.NET applications.
www.OnBarcode.com
Dim handIndex As Integer = 0 While (m_cards.Count > 0) hands(handIndex).Add(CType(m_cards(0), Card)) m_cards.RemoveAt(0) handIndex += 1 If handIndex = hands.Length Then handIndex = 0 End If End While
Encode Quick Response Code In Visual Basic .NET
Using Barcode creator for .NET framework Control to generate, create QR image in Visual Studio .NET applications.
www.OnBarcode.com
Barcode Drawer In .NET
Using Barcode creator for .NET Control to generate, create barcode image in .NET framework applications.
www.OnBarcode.com
54. End Sub 56. // Visual C# 57. // The deck is empty after dealing the cards. 58. public void Deal(Hand[] hands) { 59. 60. 61. 62. 63. 64. int handIndex = 0; while (m_cards.Count > 0) { hands[handIndex].Add((Card)m_cards[0]); m_cards.RemoveAt(0); handIndex = (handIndex == hands.Length 1) 0 : handIndex+1; }
Code 39 Creator In VS .NET
Using Barcode encoder for .NET framework Control to generate, create USS Code 39 image in .NET applications.
www.OnBarcode.com
Create 2D Barcode In Visual Studio .NET
Using Barcode creation for Visual Studio .NET Control to generate, create 2D Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
} 65. Add this code for the Draw method. This method removes the top card from the deck and returns it. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. } } return topCard; // Visual C# public Card Draw() { Card topCard = null; if (m_cards.Count > 0) { topCard = (Card)m_cards[0]; m_cards.RemoveAt(0); Visual Basic Public Function Draw() As Card Dim topCard As Card = Nothing If m_cards.Count > 0 Then topCard = CType(m_cards(0), Card) m_cards.RemoveAt(0) End If Return topCard End Function
Print UCC-128 In .NET Framework
Using Barcode maker for .NET Control to generate, create UCC.EAN - 128 image in .NET applications.
www.OnBarcode.com
2/5 Standard Generation In VS .NET
Using Barcode creator for .NET framework Control to generate, create 2/5 Industrial image in Visual Studio .NET applications.
www.OnBarcode.com
The classes are complete. Now you can use the Card, Hand, and Deck classes to write a small application that looks something like a card game. Writing the Test Application Static and shared methods are fine for testing classes, but when you use Microsoft Visual Studio you can create some interesting user interfaces without much work. In this small application, you ll create a deck of cards, deal the cards to a couple of hands, and remove the pairs from the hand. You ll display the cards as controls that you can drag from one hand to another. By moving the cards from hand to hand and removing the pairs, you can eliminate all the cards from both hands. The user interface is shown here:
Code 128 Code Set B Creator In None
Using Barcode creator for Software Control to generate, create Code-128 image in Software applications.
www.OnBarcode.com
Drawing Barcode In None
Using Barcode maker for Microsoft Word Control to generate, create barcode image in Office Word applications.
www.OnBarcode.com
Create the user interface 1. Open Form1 in the designer. 2. Set the Text property of Form1 to Deck of Cards. 3. Drag two Panel controls onto Form1. Set the Name property of one to panel1. Set the Name of the other to panel2. The cards will be displayed as button controls on these panels. 4. Choose a value for the BackColor property for each panel. 5. Set the AutoScroll property of both panels to True. Scroll bars will be automatically added to the panels if there are buttons that can t been seen in the panel. 6. Set the AllowDrop property to True for both panel controls. With the AllowDrop property set to True, the user will be able to drag buttons around the form and drop them on the Panel controls. 7. Add a label above each panel. Set the Text property of one label to Player 1 and the Text property of the other to Player 2. In the code, you ll create two instances of Hand, one for each player, and display the cards in the panel for each player. 8. Add a button to the form. Set the Name property to removePairs and the Text property to Remove pairs. 9. Add another button to the form. Set the Name property to newGame and the Text property to New game. Here s the completed user interface:
Painting Code 3 Of 9 In None
Using Barcode creation for Word Control to generate, create Code39 image in Office Word applications.
www.OnBarcode.com
EAN / UCC - 14 Creator In None
Using Barcode creator for Office Excel Control to generate, create GS1 128 image in Office Excel applications.
www.OnBarcode.com
Add icons for the card suits A directory full of icons is installed with Visual Studio .NET. You can use these icons in your programs by copying them to your project folder. 1. Locate the directory where Visual Studio is installed. Browse to the Common7\Graphics\icons\Misc folder and find the four icons for the card suits. For your convenience, the icons are also included in the \04 folder on this book s companion CD. 2. Copy the four icons to your project folder. 3. Right -click on Form1, and click View Code on the shortcut menu. 4. Add these fields to the Form1 class. The m_icons field will hold a keyindexed collection of Image instances. The hand fields will hold cards for the two players. The Button field will be used in the drag-and-drop operations. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. // Visual C# private System.Collections.SortedList m_icons = new System.Collections.SortedList(); private Hand m_hand1 = new Hand(); private Hand m_hand2 = new Hand(); Visual Basic Private m_icons As New System.Collections.SortedList() Private m_hand1 As New Hand() Private m_hand2 As New Hand() Private m_pickedUp As Button
Printing ANSI/AIM Code 39 In Objective-C
Using Barcode printer for iPhone Control to generate, create Code39 image in iPhone applications.
www.OnBarcode.com
Quick Response Code Printer In Objective-C
Using Barcode drawer for iPhone Control to generate, create QR Code ISO/IEC18004 image in iPhone applications.
www.OnBarcode.com
private Button m_pickedUp; 16. Return to the designer and double-click on the form to create the Form1_Load event method in the code editor. Add the following code to load the icon files into the SortedList object. By using the SortedList object, you can retrieve the proper image using the Suit value of a Card object. You need to replace the folder shown in the code with the folder for your project. 17. Visual Basic 18. Private Sub Form1_Load(ByVal sender As System.Object, _ 19. ByVal e As System.EventArgs) Handles MyBase.Load 20. m_icons.Add(Suit.Clubs, Image.FromFile("projectPath\Clubs.i co")) 21. m_icons.Add(Suit.Diamonds, Image.FromFile("projectPath\Di amonds.ico"))
Generate Barcode In Objective-C
Using Barcode drawer for iPhone Control to generate, create barcode image in iPhone applications.
www.OnBarcode.com
Scanning ANSI/AIM Code 39 In .NET
Using Barcode recognizer for .NET framework Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
m_icons.Add(Suit.Hearts, Image.FromFile("projectPath\Heart s.ico")) 23. m_icons.Add(Suit.Spades, Image.FromFile("projectPath\Spad es.ico")) 24. End Sub 25. 26. // Visual C# 27. private void Form1_Load(object sender, System.EventArgs e) { 28. m_icons.Add(Suit.Clubs, Image.FromFile(@"projectPath\Club s.ico")); 29. m_icons.Add(Suit.Diamonds, Image.FromFile(@"projectPath\ Diamonds.ico")); 30. m_icons.Add(Suit.Hearts, Image.FromFile(@"projectPath\ Hea rts.ico")); 31. m_icons.Add(Suit.Spades, Image.FromFile(@"projectPath\Sp ades.ico")); } 32. Also add a call to the SetUp method, which you will create in the next section. The SetUp method creates a deck, deals the cards to the players, and displays the cards. Add this line of code after the four m_icons.Add calls: 33. 34. 35. 36. SetUp(); Create a new deck and deal to the hands 1. Double-click on the New Game button to create the Click event method. Add a call to the SetUp method, which you will create in the next step. 2. Visual Basic 3. Private Sub newGame_Click(ByVal sender As System.Object, _ 4. ByVal e As System.EventArgs) Handles newGame.Click 5. SetUp() 6. End Sub 7. 8. // Visual C# 9. private void newGame_Click(object sender, System.EventArgs e) { 10. SetUp(); } 11. Add this code for the SetUp method to the Form1 class to create a deck, shuffle it, deal the cards to two hands, and then display the hands on the form. Next you ll write the ShowHand method to display the cards. 12. Visual Basic 13. Private Sub SetUp() 14. 15. 16. 17. 18. 19. Dim suits() As Suit = New Suit() {Suit.Diamonds, Suit.Clubs} Dim values() As FaceValue = New FaceValue() {FaceValue.Ki ng, _ FaceValue.Queen, FaceValue.Jack, FaceValue.Ten} Dim aDeck As New Deck(suits, values) aDeck.Shuffle() m_hand1 = New Hand() // Visual C# Visual Basic SetUp()
20. 21. 22. 23. 25.
m_hand2 = New Hand() aDeck.Deal(New Hand() {m_hand1, m_hand2}) ShowHand(panel1, m_hand1) ShowHand(panel2, m_hand2)
24. End Sub 26. // Visual C# 27. private void SetUp() { 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. Deck aDeck = new Deck( new Suit[] { Suit.Diamonds, Suit.Clubs }, new FaceValue[] { FaceValue.King, FaceValue.Queen, FaceValue.Jack, FaceValue.Ten }); aDeck.Shuffle(); m_hand1 = new Hand(); m_hand2 = new Hand(); aDeck.Deal(new Hand[] { m_hand1, m_hand2 }); ShowHand(panel1, m_hand1); ShowHand(panel2, m_hand2);
} This method uses the Deck constructor that takes two parameters. This call creates only eight cards, which will make it easy for you to tell if the program is working correctly. The cards in the deck are dealt to the two hands, and the ShowHand method is called to display the cards in the panel. Display the cards on the form Add this code to display the cards in the hand on the form: Visual Basic Private Sub ShowHand(ByVal aPanel As Panel, ByVal aHand As Hand) aPanel.Controls.Clear() Dim aCard As Card Dim aButton As Button Dim i As Integer For i = 0 To aHand.Count - 1 aCard = aHand(i) Make the button and add it to the form. aButton = New Button() aPanel.Controls.Add(aButton) With aButton Modify the appearance of the button. .Image = CType(m_icons(aCard.Suit), Image) .Text = aCard.FaceValue.ToString() .TextAlign = ContentAlignment.BottomCenter .ImageAlign = ContentAlignment.TopCenter .FlatStyle = FlatStyle.Flat .Height = 40 Locate the button on the panel. .Top = 45 * i Save the associated card. .Tag = aCard End With
Down }
Add a MouseDown event to the new button. AddHandler aButton.MouseDown, AddressOf ButtonMouse Next End Sub // Visual C# private void ShowHand(Panel aPanel, Hand aHand) { aPanel.Controls.Clear(); Card aCard; Button aButton; for (int i = 0; i < aHand.Count; i++) { aCard = aHand[i]; // Make the button and add it to the form. aButton = new Button(); aPanel.Controls.Add(aButton); //Modify the appearance. aButton.Image = (Image)m_icons[aCard.Suit]; aButton.Text = aCard.FaceValue.ToString(); aButton.TextAlign = ContentAlignment.BottomCenter; aButton.ImageAlign = ContentAlignment.TopCenter; aButton.FlatStyle = FlatStyle.Flat; aButton.Height = 40; // Locate the button on the panel. aButton.Top = 45 * i; // Save the associated card. aButton.Tag = aCard; // Add a MouseDown event to the new button. aButton.MouseDown += new System.Windows.Forms.MouseEventHandler(ButtonMous eDown); }
These 30 lines of code do a lot of work, and an explanation is in order for this method: 1. If there are any controls on the panel, delete them. 2. Using the count and indexer properties of the Hand class, look at each Card instance. 3. Create a new Button object, and add it to the Controls collection of the panel. Any controls added to the panel s Controls collection will be displayed on the panel. 4. Set the Image property of the button from the m_icons SortedList object. 5. Set the Text property of the button to the FaceValue of the Card. Enumerated values have an implicitly defined ToString method that returns the symbolic name of the enumeration value. For example, FaceValue.King.ToString() returns King . 6. Make the button tall enough to hold both the image and the text. Display the image at the top of the button (TopCenter) and the value at the bottom of the button (BottomCenter). 7. Because the buttons are 40 pixels high, display them 45 pixels apart. When there are enough buttons on the panel so that the value of the Top property of one button is larger than the size of the panel, scroll bars will appear. 8. Set the FlatStyle property of the button so that the button is displayed as a flat rectangle instead of a 3-dimensional button.
Use the Tag property to associate each button with its Card instance. In 8, you ll see a more object-oriented way to handle this association by creating a specialized control through inheritance. 10. Associate a method with the MouseDown event of the button. Because the button doesn t exist in the form designer, you can t just click it to create the MouseDown event method. No matter how many cards are created, one method will respond to all the MouseDown events. You ll use the MouseDown event to start the drag-drop functionality. Add the MouseDown event method to start the drag A minimum of three steps is required to implement drag-and-drop behaviors: 1. When the user selects a control to move, usually by a mouse click or MouseDown event, call the control s DoDragDrop method to start the drag. In this case, the user will be dragging the buttons that represent cards. 2. When the user drags the control over another control, the DragEnter event is raised. In this event, you set the Effect property of the DragEventArgs object to allow dragging. In this case, the user will drag the button controls over the Panel controls. 3. When the user releases the mouse button, the DragDrop event is raised. In this event, perform the result of the drag. In this case, the button will be moved to a different panel. In the ShowHand method, the MouseDown event of each button was assigned to the ButtonMouseDown method. Now add this method, as shown here: Visual Basic Private Sub ButtonMouseDown(ByVal sender As Object, _ ByVal e As System.Windows.Forms.MouseEventArgs) m_pickedUp = CType(sender, Button) m_pickedUp.DoDragDrop(sender, DragDropEffects.Move) End Sub // Visual C# private void ButtonMouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { m_pickedUp = (Button)sender; ((Button)sender).DoDragDrop(sender,DragDropEffects.Move); } Enable dragging with the DragOver event Add code to the DragOver events of both panel controls to allow the button being dragged, m_pickedUp, to be dropped on the panels: Visual Basic Private Sub panel1_DragEnter(ByVal sender As Object, ByVal e _ As System.Windows.Forms.DragEventArgs) Handles panel1.Dra gEnter e.Effect = DragDropEffects.Move End Sub Private Sub panel2_DragEnter(ByVal sender As Object, ByVal e _ As System.Windows.Forms.DragEventArgs) Handles panel2.Dra gEnter
}
e.Effect = DragDropEffects.Move End Sub // Visual C# private void panel1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e) { e.Effect = DragDropEffects.Move; } private void panel2_DragEnter(object sender, System.Windows.Forms.DragEventArgs e) { e.Effect = DragDropEffects.Move;
Enable dropping using the DragDrop event Add code to the DragDrop events of the Panel controls to move the dragged control to the new panel. Before moving the button, the code checks that the button is being moved to a different panel. Visual Basic Private Sub panel1_DragDrop(ByVal sender As Object, ByVal e _ As System.Windows.Forms.DragEventArgs) Handles panel1.Dra gDrop Dim theCard As Card = CType(m_pickedUp.Tag, Card) If (Not m_hand1.Contains(theCard)) Then m_hand1.Add(theCard) m_hand2.Remove(theCard) End If ShowHand(panel1, m_hand1) ShowHand(panel2, m_hand2) m_pickedUp = Nothing End Sub Private Sub panel2_DragDrop(ByVal sender As Object, ByVal e _ As System.Windows.Forms.DragEventArgs) Handles panel2.Dra gDrop Dim theCard As Card = CType(m_pickedUp.Tag, Card) If (Not m_hand2.Contains(theCard)) Then m_hand2.Add(theCard) m_hand1.Remove(theCard) End If ShowHand(panel1, m_hand1) ShowHand(panel2, m_hand2) m_pickedUp = Nothing End Sub // Visual C# private void panel1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e) { Card theCard = (Card)m_pickedUp.Tag; if (!m_hand1.Contains(theCard)) { m_hand1.Add(theCard); m_hand2.Remove(theCard); } ShowHand(panel2, m_hand2); ShowHand(panel1, m_hand1); m_pickedUp = null; } private void panel2_DragDrop(object sender, System.Windows.Forms.DragEventArgs e) {
}
Card theCard = (Card) m_pickedUp. Tag; if (!m_hand2.Contains(theCard)) { m_hand2.Add(theCard); m_hand1.Remove(theCard); } ShowHand(panel2, m_hand2); ShowHand(panel1, m_hand1); m_pickedUp = null;
All that remains now is to program the Remove Pairs button to remove the pairs from the hands and from the form. Match the cards Add code to the Click event of the Remove Pairs button. This code simply calls the RemovePairs method for each Hand object and redisplays the Hand objects. Visual Basic Private Sub removePairs_Click(ByVal sender As System.Object, ByVal e _ As System.EventArgs) Handles removePairs.Click m_hand1.RemovePairs() m_hand2.RemovePairs() ShowHand(panel2, m_hand2) ShowHand(panel1, m_hand1) End Sub // Visual C# private void removePairs_Click(object sender, System.EventArgs e) { m_hand1.RemovePairs(); m_hand2.RemovePairs(); ShowHand(panel2, m_hand2); ShowHand(panel1, m_hand1); } Run the application 1. In the Solution Explorer window, right-click the project name and click Properties in the shortcut menu. 2. In the properties tree, expand the Common Properties folder if necessary and select General. 3. In the Startup Object list, click Form1, and click OK. 4. Press F5 to run the application. The following screen shot shows the results after the pairs have been matched. You can clear all the cards by dragging them all to one panel and clicking the Remove Pairs button.
Copyright © OnBarcode.com . All rights reserved.