Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Beginning Visual Basic 2005 (2006)

.pdf
Скачиваний:
220
Добавлен:
17.08.2013
Размер:
14.97 Mб
Скачать

Appendix D

Public Class Server

Inherits MarshalByRefObject

Public ReadOnly Property ServerTime() As String

Get

Return Now().ToShortTimeString

End Get

End Property

Public ReadOnly Property ServerDate() As Date

Get

Return Now().Date

End Get

End Property

End Class

Here is an example of the server code used to register the channel and port for the Remoting engine:

Imports System.Runtime.Remoting

Imports System.Runtime.Remoting.Channels

Imports System.Runtime.Remoting.Channels.Tcp

Module Module1

Sub Main()

Dim channel As TcpChannel = New TcpChannel(8500)

ChannelServices.RegisterChannel(channel)

RemotingConfiguration.RegisterWellKnownServiceType( _

GetType(Chapter20Exercise2.Server), “Engine”, _

WellKnownObjectMode.SingleCall)

Console.Write(“Engine Registered.” + Environment.NewLine)

Console.Write(“Server Active . . .”)

Console.Read()

End Sub

End Module

Remember that for the server and client you also have to add a reference to System.Runtime.Remoting and to the server class you created. The final part of the exercise was to create the client. Your code will look similar to this:

Sub Main()

Dim client As Chapter20Exercise2.Server

Console.Write(“Trying to obtain Type from Server . . .” + Environment.NewLine) client = CType(Activator.GetObject(GetType(Chapter20Exercise2.Server), _

“tcp://Maincpu:8500/Engine”), Chapter20Exercise2.Server) Console.Write(“Type Created and returned” + Environment.NewLine) Console.Write(“Server date: “ + client.ServerDate + Environment.NewLine) Console.Write(“Server time: “ + client.ServerTime + Environment.NewLine) Console.Read()

End Sub

766

Solutions

After creating the client, make sure that you start the project that registers and starts the server. If you used a console application to test your server, you saw a command window with the return values from the remoting object. Congratulations, you have just created a Remoting system.

Chapter 21 Exercise Solutions

Exercise 1

For this example, you created a setup project for Notepad. We created a new setup project named Chapter21Exercise1. Under the Application folder, we browsed for and added the notepad.exe file. After adding the file, we created a shortcut to the executable and moved the shortcut to User’s Program. Menu. Next, we selected the project in Solution Explorer and then found and changed the author and manufacturer properties in the Properties window. Finally, we built and then ran the setup.exe file.

You may be asking why we changed the author and manufacturer properties. The manufacturer is used to determine the default location for the installed application. When we installed the application, C:\Program Files\Wrox\Chapter21Exercise1\ was the default installation directory. Without updating the manufacturer, the default directory would have been C:\Program Files\Default Company Name\Chapter21Exercise1\. The second reason to change the manufacturer was the support info screen under Add/Remove Programs. When you look at your application’s support info screen, you’ll see that the publisher is Wrox.

Exercise 2

In the completed exercise, you would have added a bitmap image to the application. You should have added the image to the application folder or a subfolder of the application folder. Next, you would have added a splash screen via the user interface editor. The SplashBitmap property of the Splash dialog box was changed to the bitmap you added, and the dialog box was moved up to the first screen shown. When you ran the installation, you saw the splash screen as the first dialog box.

Chapter 22 Solution

This exercise has numerous correct answers. If you ask 10 programmers to complete it, you will get 10 different answers. So, if your changes work, you have a valid answer. The following is what we came up with to solve the problem.

You need to add a call to the new function, ComputerPlayToWin, from ComputerPlay. It should be the first call in the procedure. If you find a win here and make a move, you can exit the subroutine without allocating any of the local variables in ComputerPlay.

Sub ComputerPlay()

If ComputerPlayToWin() Then Exit Sub

Your solution will look different from ours. Compare your solution to ours and see which you think is better. The first function, CheckForWin, allows you to check an entire row or column of buttons for a

767

Appendix D

chance to win. If two squares are marked and the third is empty, the computer will make this move by changing the text for all buttons. This is done by passing the buttons ByRef to the function.

ComputerPlayToWin calls this function for every row, column, or diagonal win possibility on the board.

Private Function CheckForWin(ByRef btnFirst As Windows.Forms.Button, _ ByRef btnSecond As Windows.Forms.Button, ByRef btnThird As _ Windows.Forms.Button, ByVal stringToFind As String, _

ByVal strOpponentsMark As String) As Boolean Dim intSum As Int16 = 0S

‘Check to see if we can win on this row

‘We can we if we have two marks and no opponent marks on the row ‘If there is an opponents mark we are blocked so return false

If btnFirst.Text = stringToFind Then intSum += 1S

ElseIf btnFirst.Text = strOpponentsMark Then Return False

End If

If btnSecond.Text = stringToFind Then intSum += 1S

ElseIf btnSecond.Text = strOpponentsMark Then Return False

End If

If btnThird.Text = stringToFind Then intSum += 1S

ElseIf btnThird.Text = strOpponentsMark Then Return False

End If

‘We will win on this turn

‘so just mark the entire row to save some resources

If intSum = 2 Then btnFirst.Text = stringToFind

btnSecond.Text = stringToFind btnThird.Text = stringToFind Return True

Else

Return False End If

End Function

All that the ComputerPlayToWin function does is pass the buttons and strings to check CheckForWin for each possible win. If a win is found, the game is over. The computer will not make a random play if it can win.

Private Function ComputerPlayToWin() As Boolean

If CheckForWin(btn00, btn01, btn02, “O”, “X”) Then

‘Winner on top Row Call Winner(“0”)

768

Solutions

Return True End If

If CheckForWin(btn10, btn11, btn12, “O”, “X”) Then ‘Winner on middle Row

Call Winner(“O”) Return True

End If

If CheckForWin(btn20, btn21, btn22, “O”, “X”) Then ‘Winner on third Row

Call Winner(“O”) Return True

End If

If CheckForWin(btn00, btn10, btn20, “O”, “X”) Then ‘Winner on first column

Call Winner(“O”) Return True

End If

If CheckForWin(btn01, btn11, btn21, “O”, “X”) Then ‘Winner on second column

Call Winner(“O”) Return True

End If

If CheckForWin(btn02, btn12, btn22, “O”, “X”) Then ‘Winner on third column

Call Winner(“O”) Return True

End If

If CheckForWin(btn00, btn11, btn22, “O”, “X”) Then ‘Winner on diagonal top left to bottom right Call Winner(“O”)

Return True End If

If CheckForWin(btn20, btn11, btn02, “O”, “X”) Then ‘Winner on diagonal bottom left to top right Call Winner(“O”)

Return True End If

End Function

769

Index

771

SYMBOLS AND

NUMERICS

& (ampersand)

concatenation of strings, 21, 53, 54–55 in Text property of buttons for hot key, 16

<...> (angle brackets), enclosing XML tags, 613 * (asterisk)

multiplication operator, 47 wildcard in Access queries, 480

wildcard in SQL statements, 477, 478

@ (at sign), parameters in SQL statements, 498–499 {...} (braces), enclosing array values, 135–136

[...] (brackets), enclosing fields in SQL statements, 477

“” (double quotes), enclosing strings, 52 / (forward slash), 47

> (greater than operator), 92–93

>= (greater than or equal to operator), 92–93 < (less than operator), 90–91

<= (less than or equal to operator), 92 - (minus sign), subtraction operator, 46 + (plus sign), addition operator, 46

# (pound sign), enclosing dates, 64–65 ‘ (single quote)

preceding comments, 21, 43 three (‘’’), for comment blocks, 43

_ (underscore), continuation character in code, 19, 21 32-bit numbers, 68–69

64-bit numbers, 68–69

A

AbortRetryIgnore button, MessageBox dialog box, 207 About dialog box, 199–203

absolute position on screen, 432–433 abstraction of hardware, 2

access (accelerator) keys, in menus, 248, 249 Access Control project example, 422–424 Access, Microsoft

creating queries, 479–482 database objects, 476

Index

Index

access rules, WAT, 592 Active Server Methods, 654 Active Server Pages

benefits of, 553–554 development using, 554–555 files used by, 554 reinstalling, 702

ActiveControl property, 257–258

ActiveSync software, 706–707

ActiveX controls, 2

ActiveX Data Object (ADO), 3

Add method

ArrayList, 157 collections, 158

ControlBindingsCollection class, 517–518

Hashtable, 162

AddDays method, dates, 66

AddExtension property

OpenFileDialog control, 215

SaveFileDialog control, 220

AddHours method, dates, 66 addition operator, 46

AddMilliseconds method, dates, 66

AddMinutes method, dates, 66

AddMonths method, dates, 66

Address Book project example

address book

creating from addresses, 627–631 reading from another application, 642–646

addresses

adding new, 634–636 changing, 625 deleting, 638–639

loading from XML (deserializing), 622–625, 633–634 navigating, 636–637

storing and ignoring members, 631–632 storing in XML (serializing), 616–622

creating project, 615–616

e-mail, sending with address data, 625–627 testing, 639–640

Address List project example

Address List project example, 642–646

for Web Forms applications, 702

AddSeconds method, dates, 66

for Web Services, 702

AddYears method, dates, 66

for Windows Forms applications, 702

ADO (ActiveX Data Object), 3

XCOPY deployment, 693

ADO.NET, 494–495. See also database

forcing to stop, 123–124

aggregate controls, 400

installation of

aggregate functions, 537

customizing user interface for, 697–700

algorithms, 38–39

definition of, 687–688

AllowFullOpen property, ColorDialog control, 230

multitiered, 388–389

AllowPaging attribute, GridView control, 579

arcs, drawing, 473

AllowPrintToFile property, PrintDialog

Array Demo project example

control, 233

defining and using arrays, 126–128

AllowScriptChange property, FontDialog

dynamic arrays, 169–171

control, 225

initializing arrays, 135–136

AllowSelection property, PrintDialog

passing arrays as parameters, 130–132

control, 233

reversing arrays, 134–135

AllowSomePages property, PrintDialog

sorting arrays, 133

control, 233

using For loop with arrays, 128–130

AllowSorting attribute, GridView control, 579

ArrayList

AlternatingRowStyle attribute, GridView

assigning values to, 151

control, 579

defining, 150

ampersand (&)

definition of, 149–150

concatenation of strings, 21, 53, 54–55

deleting items from, 153–156

in Text property of buttons for hot key, 16

limitations of, 158

And operator, 95–97

properties for, 155

And Or Demo project example, 93–97

showing items in, 156–157

angle brackets (<...>), enclosing XML tags, 613

using, 150–153

ANSI SQL, 477

arrays

AnyColor property, ColorDialog control, 230

accessing elements of, 126, 128

Apache Web server, 552

assigning values to, 126, 128

API, Windows, 2

defining, 126–128

apostrophe (‘)

definition of, 125

preceding comments, 21, 43

dynamic, 168–171

three (‘‘‘), for comment blocks, 43

index of, 127–128

Application class, 341–342

initializing, 135–136

application isolation, 33–34

looping through

Application Programming Interface, Windows (Windows

forward, 113–114, 128–130

API), 2

in reverse, 133–134

applications. See also Web Forms applications;

passing as parameters, 130–132

Windows Forms applications

returning from Web Service, 660–664

compared to solutions, 731

reversing order of, 134–135

creating, 10–13

sorting, 133

deployment

.asmx files, 654

ClickOnce deployment, 688–693

.asp files, 553

definition of, 687–688

aspect ratio for images, preserving, 471–473

for private assemblies, 700–701

ASP.NET 2.0

setup application, creating with Windows Installer,

benefits of, 553–554

693–697

development using, 554–555

for shared assemblies, 701–702

files used by, 554

tools for, 702–703

reinstalling, 702

774

Button control

ASP.NET 2.0 Beta Preview, 728–729

ASPNET_RegIIS tool, 702

ASPX. See Active Server Pages; Web Forms

applications

.aspx files, 553 assemblies

creating COM information from, 703 definition of, 383

deploying, 700–702

evidence of origin of an assembly, 741–742 installer classes, executing, 703

metadata of, inspecting, 703 permissions for, 741

registering and unregistering, 392–394, 703 signing, 390–392

strongly named, 389–390 versions of, 392

assignment operators, 47–48 asterisk (*)

multiplication operator, 47 wildcard in Access queries, 480

wildcard in SQL statements, 477, 478

Asterisk icon, MessageBox dialog box, 206

at sign (@), parameters in SQL statements, 498–499 attributes, XML, 614

authentication

for database access, 496 forms authentication

choosing in WAT, 589 configuring in WAT, 586–595 definition of, 586

login controls for, 595–605 roles for, 605–607

windows authentication choosing in WAT, 589 definition of, 585–586

AutoCorrect feature, 272, 278

AutoGenerateColumns attribute, GridView control, 579

B

back-end functionality, 350 backwards loop, 112–113

base classes, .NET Framework, 30–31 base-2 numbers, 67

base-10 numbers, 67

Beginning XML, 2nd Edition, 640

behavior of object, 310, 316 Bezier curves, drawing, 473 bin directory, 700

binary, 67–71

BindingContext class, 516–517

BindingExample project example

adding records, 534–536

binding simple controls, 518–524 deleting records, 543–544 searching in, 531–532

sorting in, 529

updating records, 541–542

BindingNavigator class, 484

BindingSource class, 484 bitmap, 429

bits, 68

blank space, 44

Blue Screen of Death (BSOD), 33 blue wavy underline, 272

.bmp files, 466 BODY element, 560

Boolean data type, 66, 707

BoundField attribute, GridView control, 580

Bounds property, Screen class, 403

braces ({...}), enclosing array values, 135–136 brackets ([...]), enclosing fields in SQL statements, 477 branching, 83. See also decisions

breakpoints

changing properties of, 299–300 clearing, 304

definition of, 293

hit count for, 298–299 setting, 293–294 using, 296–298

Breakpoints window, 295 browser

Object Browser, 397–398

Web browser, 552

BSOD (Blue Screen of Death), 33 buffer overflow attack, 739

build configuration, changing from Build to Release, 303

Build menu, IDE, 8 business tier, 389

Button control

adding to form, 15–16

in Compact Framework, 710 events for, 174–178

Index

775