Upload
geraldine-saenz
View
213
Download
1
Embed Size (px)
DESCRIPTION
xvdsbsbsbsbssssssssssssssssssssssssssssssssssssbvvvvlkjnvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
Citation preview
EJERCICIOS
EJERCICIOS DE VISUAL STUDIO .NETSTEP BY STEP
1. Using Application Class Properties
1. Launch Visual Studio .NET. From the File menu select Open, Project. Navigate to and open the existing project 306C01.2. In the Solution Explorer, right-click the project name and select Add, Add Windows Form. (Alternatively, you can select Add Windows Form from the Project menu.) Name the new form StepByStep1-3.vb.3. Switch to the Code view, and just after the Windows Forms Designer generated code block insert the following procedure:
[View full width]Private Sub StepByStep1_3_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
'Display a MessageBox
MessageBox.Show(Application.ExecutablePath, "Location of
Executable", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
4. In the Solution Explorer, right-click the project name and select Properties from the context menu. In the Project Property page, set the startup object to StepByStep1_3, as shown in Figure 1.9. Click OK to close the Project Property page.
Figure 1. Changing the startup object by using the Project Property page.
5. Select Run from the Debug menu. The application will display a message box showing the application's executable path (see Figure 1.10). When you close the message box, the application will display the new form.
Figure 2. A message box displaying the executable's path.
STEP BY STEP
2. Setting Windows Form Properties Programmatically
1. Open the project 306C01. In the Solution Explorer right-click on the project name and select Add, Add Windows Form from the context menu. Name the new form StepByStep1-5.2. Right-click anywhere on the form and select View Code from the context menu. In the Code window insert the following code just after the Windows Forms Designer Generated Code section.
3. Private Sub StepByStep1_5_Load( _
4. ByVal sender As System.Object, _
5. ByVal e As System.EventArgs) Handles MyBase.Load
6. Me.BackColor = Color.AntiqueWhite
7. Me.FormBorderStyle = FormBorderStyle.FixedSingle
8. Me.Size = New Size(400, 200)
9. Me.StartPosition = FormStartPosition.CenterScreen
10. Me.MinimizeBox = False
11. End Sub
12. In the Solution Explorer, right-click the project name and select Properties from the context menu. In the Project Property page, select StepByStep1_5 as the startup object. Click OK to close the Project Property page.13. Select Run from the Debug menu. The form displayed looks similar to the form that you created in the previous exercise (refer to Figure 1.12).STEP BY STEP
3. Adding New Properties to a Windows Form
1. Open the project 306C01. In the Solution Explorer right-click on the project name and select Add, Add Windows Form from the context menu. Name the new form StepByStep1-7.2. Right-click the form anywhere and select View Code from the context menu. Add one line of code at the very top of the code:
3. Imports System.ComponentModel
4. In the Code window insert the following code just after the Windows Forms Designer Generated Code section.
5. ' define constant values for State
6. Public Enum State
7. Idle
8. Connecting
9. Processing
10. End Enum
11. ' use this field as storage location for FormState property
12. Private mFormState As State
13. ' set attributes for FormState property
14. _
18. Public Property FormState() As State
19. Get
20. FormState = mFormState
21. End Get
22. Set(ByVal Value As State)
23. mFormState = Value
24. Select Case mFormState
25. Case State.Idle
26. Me.BackColor = Color.Red
27. Me.Text = "Idle"
28. Case State.Connecting
29. Me.BackColor = Color.Orange
30. Me.Text = "Connecting..."
31. Case State.Processing
32. Me.BackColor = Color.Green
33. Me.Text = "Processing"
34. End Select
35. End Set
36. End Property
37. Expand the Windows Form Designer Generated Code section. Change the form's constructor so that it looks like this:
38. Public Sub New()
39. MyBase.New()
40. 'This call is required by the Windows Form Designer.
41. InitializeComponent()
42. 'Add any initialization after the InitializeComponent() call
43. 'set the FormState property of this form
44. Me.FormState = State.Processing
45. End Sub
46. In the Solution Explorer, right-click the project name and select Properties from the context menu. In the Project Property page, select StepByStep1_7 as the startup object. Click OK to close the Project Property page.47. Select Run from the Debug menu. The project will show a green form onscreen with the title text Processing....STEP BY STEP
4. Using Visual Inheritance
1. Open the project 306C01. In the Solution Explorer right-click on the project name and select Add, Add Inherited Form from the context menu. Name the new form StepByStep1-8 and click the Open button.2. From the Inheritance Picker dialog box (see Figure 1.14), select the component named StepByStep1-7 and click OK.
Figure 1. The Inheritance Picker dialog box.
3. Open the Properties window. Click the Categorized icon on its toolbar. Look for the Custom category. The FormState property within this category will be set to Processing, as shown in Figure 1.15. Change the FormState property to Idle.
Figure 2. The custom property is now available in the Properties window in the specified category along with proper description.
4. In the Solution Explorer, right-click the project name and select Properties from the context menu. In the Project Property page, select StepByStep1_8 as the startup object. Click OK to close the Project Property page.5. Select Run from the Debug menu. Since you have set the FormState property to Idle, the form will be displayed in red.STEP BY STEP
5. Handling the MouseDown Event
1. Open the project 306C01. In the Solution Explorer right-click the project name and select Add, Add Windows Form from the context menu. Name the new form StepByStep1-9 and click the Open button.2. Open the Properties window of this form. Change the form's Text property to Event Handling Form.3. Right-click anywhere on the form and select View Code from the context menu.4. In the left combo box at the top of the Code window, select Base Class Events. In the right combo box at the top of the Code window select MouseDown. This will insert a template for the MouseDown event handler, as shown in Figure 1.16. Add code to the event handler so that it looks like this:
5. Private Sub StepByStep1_9_MouseDown( _
6. ByVal sender As Object, _
7. ByVal e As System.Windows.Forms.MouseEventArgs) _
8. Handles MyBase.MouseDown
9. MessageBox.Show(String.Format( _
10. "X ={0}, Y={1}", e.X, e.Y), _
11. String.Format("The {0} mouse button hit me at:", _
12. e.Button.ToString()))
13. End Sub
Figure 1. The combo boxes in Code view let you insert empty event handlers.
14. Set this form as the startup object. Run the project. You will observe that whenever you click on the form area, it invokes a message box displaying the position of the click and which mouse button was clicked (see Figure 1.17).
Figure 2. The event handler displays a message box showing event-related data.
STEP BY STEP
6. Handling Events by Using a Delegate
1. Open the project 306C01. In the Solution Explorer right-click on the project name and select Add, Add Windows Form from the context menu. Name the new form StepByStep1_10 and click the Open button.2. Open the Properties window of this form. Change the Text property of the form to Events Handling Through a Delegate.3. Right-click on the form and select View Code from the context menu. Enter this code after the Windows Form Designer Generated Code section:
4. ' Delegate to handle the MouseDown event
5. Sub StepByStep_1_10_Mousedown(ByVal sender As Object, _
6. ByVal e As MouseEventArgs)
7. MessageBox.Show(String.Format( _
8. "X ={0}, Y={1}", e.X, e.Y), _
9. String.Format("The {0} mouse button hit me at:", _
10. e.Button.ToString()))
11. End Sub
12. Expand the Windows Form Designer Generated Code section and modify the form's constructor to look like this:
13. Public Sub New()
14. MyBase.New()
15. 'This call is required by the Windows Form Designer.
16. InitializeComponent()
17. 'Add any initialization after the InitializeComponent() call
18. ' Add an event handler for the form's MouseDown event
19. AddHandler Me.MouseDown, _
20. AddressOf Me.StepByStep_1_10_MouseDown
21. End Sub
Set this form as the startup object. Run the project and click on the form surface with left and right mouse buttons. You will see the form responding to MouseDown event by displaying a message box.
STEP BY STEP
7. Attaching Multiple Event Handlers to the MouseDown Event
1. Open the project 306C01. In the Solution Explorer right-click on the project name and select Add, Add Windows Form from the context menu. Name the new form StepByStep1_11 and click the Open button.2. Open the Properties window for this form. Change the form's Text property to Multiple Events Handling Form.3. Right-click on the form and select View Code from the context menu. Enter this code after the Windows Form Designer Generated Code section:
4. ' Delegate to handle the MouseDown event
5. Sub StepByStep_1_11_MouseDown(ByVal sender As Object, _
6. ByVal e As MouseEventArgs)
7. MessageBox.Show(String.Format( _
8. "X ={0}, Y={1}", e.X, e.Y), _
9. String.Format("The {0} mouse button hit me at:", _
10. e.Button.ToString()))
11. End Sub
12. ' Delegate to handle the MouseDown event
13. Sub StepByStep_1_11_MouseDown2( _
14. ByVal sender As Object, ByVal e As MouseEventArgs)
15. Dim frm1_11 As Form = CType(sender, Form)
16. If frm1_11.BackColor.ToArgb = _
17. Color.AntiqueWhite.ToArgb Then
18. frm1_11.BackColor = Color.LightCoral
19. Else
20. frm1_11.BackColor = Color.AntiqueWhite
21. End If
22. End Sub
23. Expand the Windows Form Designer Generated Code section and modify the form's constructor to look like this:
24. Public Sub New()
25. MyBase.New()
26. 'This call is required by the Windows Form Designer.
27. InitializeComponent()
28. 'Add any initialization after the InitializeComponent() call
29. ' Add two event handlers for the form's MouseDown event
30. AddHandler Me.MouseDown, _
31. AddressOf Me.StepByStep_1_11_MouseDown
32. AddHandler Me.MouseDown, _
33. AddressOf Me.StepByStep_1_11_MouseDown2
34. End Sub
35. In the left-hand combo box at the top of the Code window, select Base Class Events. In the right-hand combo box at the top of the Code window select MouseDown to insert a template for the MouseDown event handler, as shown in Figure 1.16. Add code to the event handler so that it looks like this:
36. Private Sub StepByStep1_9_MouseDown( _
37. ByVal sender As Object, _
38. ByVal e As System.Windows.Forms.MouseEventArgs) _
39. Handles MyBase.MouseDown
40. MessageBox.Show(String.Format( _
41. "X ={0}, Y={1}", e.X, e.Y), _
42. String.Format("The {0} mouse button hit me at:", _
43. e.Button.ToString()))
44. End Sub
Set this form as the startup object. Run the project. Try clicking on the form with left and right mouse buttons, and you will see that the form changes its background color in addition to responding with a message box on every click.
STEP BY STEP
8. Adding Controls to a Windows Form Using Windows Forms Designer
1. Create a new Visual Basic .NET Windows Application project in the Visual Studio .NET IDE.2. Add a new Windows Form to the project.3. Select the Toolbox window. From the Windows Forms tab of the Toolbox, place two controls each of type Label and TextBox and a control of type Button on the form's surface. Arrange the controls as shown in Figure 1
Figure 1. Using Windows Forms Designer to add controls to a form.
4. Set the form as the startup object for the project.5. Run the project. You will see a Windows Form as shown in Figure 2. You can navigate between controls using the Tab key or by clicking on them with the mouse.
Figure 2. What you see at design time is what you get at runtime.
STEP BY STEP
9. Adding Controls to a Windows Form Programmatically
1. Add a new Windows Form to your Visual Basic .NET project.2. Switch to the Code view and add the following variables just below the Windows Forms Designer generated code:
3. ' Create variables to hold controls
4. Dim lblName, lblPassword As Label
5. Dim txtName, txtPassword As TextBox
6. Dim btnLogin As Button
7. Expand the Windows Forms Designer Generated region. Modify the New() procedure as follows:
8. Public Sub New()
9. MyBase.New()
10. 'This call is required by the Windows Form Designer.
11. InitializeComponent()
12. 'Add any initialization after the
13. ' InitializeComponent() call
14. ' apecify the form's size
15. Me.ClientSize = New System.Drawing.Size(272, 182)
16. 'Set up the label the Name textbox
17. Dim lblName As Label = New Label()
18. lblName.Text = "Name: "
19. ' Specify the location for the control
20. ' the default location will be (0, 0) otherwise
21. lblName.Location = New Point(16, 16)
22. ' Set up the label for the Password textbox
23. Dim lblPassword As Label = New Label()
24. lblPassword.Text = "Password: "
25. lblPassword.Location = New Point(16, 80)
26. ' Set up a Name textbox
27. Dim txtName As TextBox = New TextBox()
28. txtName.Location = New Point(152, 16)
29. ' Set up a Password textbox
30. Dim txtPassword As TextBox = New TextBox()
31. txtPassword.Location = New Point(152, 80)
32. txtPassword.PasswordChar = "*"
33. ' Set up a command button
34. Dim btnLogin As Button = New Button()
35. btnLogin.Text = "Login"
36. btnLogin.Location = New Point(96, 128)
37. ' Add controls to the form
38. ' Method 1: Specify the current form as
39. ' parent container for a control
40. lblName.Parent = Me
41. ' Method 2: Add a control to form's control collection
42. Me.Controls.Add(txtName)
43. ' Method 3: Add an array of controls to form's
44. ' control collection
45. Me.Controls.AddRange(New Control() { _
46. lblPassword, txtPassword, btnLogin})
47. Set the form as the startup object for the project.48. Run the project. The form will be displayed as shown in Figure 1
Figure 1. A form with controls loaded programmatically.
STEP BY STEP
10. Programming Control Events
1. Add a Windows Form to your Visual Basic .NET project.2. Place a Label control on the form and name it lblStatus. Change the Dock property of the control to Top; set its Font property to size 12 and italic; clear the Text property; and change the TextAlign property to MiddleCenter.3. Place another Label control on the form and name it lblStack. Change the Dock property of the control to Bottom; set its Font property to size 16 and bold; change the Text property to A Button Stack; and finally change the TextAlign property to MiddleCenter.4. Place a Button control on the form. Name it btnAdd, set its Text property to &Add and its Anchor property to Bottom, Left.
5. Place another Button control on the form. Name it btnRemove. Set its Text property to &Remove and its Anchor property to Bottom, Right.6. Switch to the Code view and add the following code directly after the Windows Forms Designer generated code:
7. ' Stores the top of stack value
8. Dim intTos As Integer
9. ' Stores initial control count
10. Dim intInitCtrlCount As Integer
11. Expand the Windows Forms Designer Generated Code region and add the following code in the Form's New method code after the call to the InitializeComponent method:
12. 'Add any initialization after the InitializeComponent() call
13. ' Initially the stack is empty
14. intTos = 0
15. ' Get the initial control count. Be sure to put this
16. ' statement after the call to the InitializeComponent method
17. intInitCtrlCount = Me.Controls.Count
18. ' Redraw form if it is resized
19. Me.ResizeRedraw = True
20. Using the combo boxes in the Code window, add an event handler for the form's Paint event. Add the following code to the event handler:
21. Private Sub StepByStep2_4_Paint(ByVal sender As Object, _
22. ByVal e As System.Windows.Forms.PaintEventArgs) _
23. Handles MyBase.Paint
24. ' Gets the Graphics object for the form
25. Dim grfx As Graphics = e.Graphics
26. ' Draw a line, end-to-end on the form
27. grfx.DrawLine(Pens.Black, 0, _
28. Me.lblStack.Location.Y - 5, _
29. Me.Width, Me.lblStack.Location.Y - 5)
30. ' Set the location for Add and remove buttons so that
31. ' they get repositioned after the form is
32. resized.
33. Me.btnAdd.Location = _
34. New Point(0, Me.lblStack.Location.Y - 40)
35. Me.btnRemove.Location = _
36. New Point(Me.Width - Me.btnRemove.Width - 7, _
37. Me.lblStack.Location.Y - 40)
38. End Sub
39. Insert the following event handler in the code; you will later attach it programmatically to the dynamically created Button objects:
40. ' This event handler will handle the Click event of all of
41. ' the buttons in the stack
42. Private Sub Button_Click(ByVal sender As System.Object, _
43. ByVal e As System.EventArgs)
44. ' Cast the object to a Button
45. Dim btnSender As Button = CType(sender, Button)
46. ' Change the lblStatus to show that
47. ' this button was clicked.
48. lblStatus.Text = "Status: " & _
49. btnSender.Text & " is clicked."
50. End Sub
51. In the Design view, double-click the Add button. This will attach an event handler for the Click event (the default event for a button). Modify this event handler as follows:
52. Private Sub btnAdd_Click(ByVal sender As System.Object, _
53. ByVal e As System.EventArgs) Handles btnAdd.Click
54. 'If stack is not yet full
55. If (intTos < 8) Then
56. Dim btnSender As Button = CType(sender, Button)
57. ' Create a new Button to add to the Stack
58. Dim btnNew As Button = New Button()
59. btnNew.Name = "Element" & intTos
60. btnNew.Text = "Element " & intTos
61. btnNew.Location = New Point( _
62. (Me.Width - btnNew.Width) / 2, _
63. btnSender.Location.Y - btnSender.Height * intTos)
64. ' Attach a event handler to the
65. ' Click event of newly created button
66. AddHandler btnNew.Click, AddressOf Me.Button_Click
67. ' Add the Button to the Container's
68. ' Control collection
69. Me.Controls.Add(btnNew)
70. lblStatus.Text = "Status: Element " & _
71. intTos & " added."
72. intTos += 1
73. Else
74. ' Stack is full, can't add a button
75. lblStatus.Text = "Status: Stack is full!"
76. End If
77. End Sub
78. In the Design view, double-click the Remove button. In its Click event handler code, insert the following lines:
79. Private Sub btnRemove_Click(ByVal sender As System.Object, _
80. ByVal e As System.EventArgs) Handles btnRemove.Click
81. Dim btnSender As Button = CType(sender, Button)
82. ' Current control count in the Form's Control collection
83. Dim intCtrlCount As Integer = Me.Controls.Count
84. ' If any new buttons were created in the stack
85. If (intCtrlCount > intInitCtrlCount) Then
86. ' Remove the most recetly added control
87. ' in the collection
88. Me.Controls.Remove(Me.Controls( _
89. Me.Controls.Count - 1))
90. ' Adjust the top of stack
91. intTos -= 1
92. lblStatus.Text = "Status: Element " & _
93. intTos & " removed."
94. Else
95. ' Stack is empty, Can't remove a button
96. lblStatus.Text = "Status: Stack is empty!"
97. End If
98. End Sub
99. Set this form as the startup object for the project.Run the project. Click the Add and Remove buttons. You will see that the Button controls are dynamically created and removed. When you click on one of the dynamically created button controls, its Click event is fired, and it will display a message on the top Label control, as shown in Figure 1.
Figure 1. Adding and removing controls dynamically.