Visual Basic and Visual Basic .NET for Scientists and ... Visual Basic and Visual Basic .NET for

download Visual Basic and Visual Basic .NET for Scientists and ... Visual Basic and Visual Basic .NET for

of 23

  • date post

  • Category


  • view

  • download


Embed Size (px)

Transcript of Visual Basic and Visual Basic .NET for Scientists and ... Visual Basic and Visual Basic .NET for

  • Visual Basic andVisual Basic .NETfor Scientistsand Engineers


    550fm.qxp 2/4/02 2:03 PM Page i

  • Visual Basic and Visual Basic .NET for Scientists and Engineers

    Copyright 2002 by Christopher Frenz

    All rights reserved. No part of this work may be reproduced or transmitted in any form or by anymeans, electronic or mechanical, including photocopying, recording, or by any informationstorage or retrieval system, without the prior written permission of the copyright owner and thepublisher.

    ISBN (pbk): 1-893115-55-0

    Printed and bound in the United States of America 12345678910

    Trademarked names may appear in this book. Rather than use a trademark symbol with everyoccurrence of a trademarked name, we use the names only in an editorial fashion and to thebenefit of the trademark owner, with no intention of infringement of the trademark.

    Technical Reviewer: MN, MCSD

    Editorial Directors: Dan Appleman, Peter Blackburn, Gary Cornell, Jason Gilmore, Karen Watterson

    Managing Editor: Grace Wong

    Project Managers: Alexa Stuart, Erin Mulligan

    Copy Editors: Jennifer Lind, Nicole LeClerc, Ami Knox

    Production Editor: Kari Brooks

    Compositor: Impressions Book and Journal Services, Inc.

    Indexer: Carol Burbo

    Cover Designer: Tom Debolski

    Marketing Manager: Stephanie Rodriquez

    Distributed to the book trade in the United States by Springer-Verlag New York, Inc.,175 FifthAvenue, New York, NY, 10010 and outside the United States by Springer-Verlag GmbH & Co. KG,Tiergartenstr. 17, 69112 Heidelberg, Germany.

    In the United States, phone 1-800-SPRINGER, email, or visit

    Outside the United States, fax +49 6221 345229, email, or visit

    For information on translations, please contact Apress directly at 901 Grayson Street, Suite 204,Berkeley, CA 94710.

    Phone 510-549-5938, fax: 510-549-5939, email, or visit

    The information in this book is distributed on an as is basis, without warranty. Although everyprecaution has been taken in the preparation of this work, neither the author nor Apress shallhave any liability to any person or entity with respect to any loss or damage caused or alleged tobe caused directly or indirectly by the information contained in this work.

    The source code for this book is available to readers at in the Downloadssection.

    550fm.qxp 2/4/02 2:03 PM Page ii


    Programming Your OwnSpreadsheets

    IN THIS CHAPTER, YOULL LEARN how to code the spreadsheet-type interfaces that arecommon to so many scientific applications. These interfaces are extremely usefulwhen dealing with matrix math or other scenarios that require a large amount ofdata. To develop such an advanced interface, this chapter introduces a new con-trol along with some user events you havent yet encountered. The examplesrequire you to use almost everything youve seen so far, so make sure that youhave the basics covered.

    The MSFlexGrid Control

    The MSFlexGrid control, when its displayed on a form, appears like any spread-sheet grid. It consists of a number of intersecting rows and columns, and thepoints where the rows and columns intersect are known as cells. If you add such a control to your form and click Run, youll see that it behaves almost likea spreadsheet programs worksheet. For example, you can move between cellswith the arrow keys and mouse. The only real difference between this control andan actual spreadsheet is that you cant enter (type) data directly into a grid cell.Thus, this chapter will concentrate on coding a way to allow direct user dataentry. But first, take some time to examine the important properties and eventsassociated with this control. The MSFlexGrid control isnt a part of the standardVB toolbox. In VB 6.0, you must go to the Project menu and select theComponents option. You then select the MSFlexGrid control from the Components list. In VB .NET you have to go to the Tools menu and select theCustomize Toolbox option. You next select the MSFlexGrid control from the COMcontrols list. The control icon then appears in the toolbox, where you can dragand drop it onto a form as with any other control.

    MSFlexGrid Properties

    The following sections outline the properties for the MSFlexGrid control.


    550ch09.qxp 1/14/02 3:13 PM Page 199

  • The AllowBigSelection Property

    This property determines whether or not a whole row or column will be selectedif the user clicks a row or column header. If its set to the default of True, suchwhole row/column selections are allowed. If the property is set to False, only single cell selections are allowed.

    The AllowUserResizing Property

    This property works pretty much as youd expect from its name. Its settings deter-mine if neither rows nor columns can be resized (flexResizeNone) by the user, ifjust columns can be resized (flexResizeColumns), if just rows can be resized(flexResizeRows), or if both rows and columns can be resized (flexResizeBoth).

    The Cols and Rows Properties

    These properties are integer values, which determine the grids number ofcolumns and rows. The default for each property is 2, but you can easily adjustthis through code (i.e., MSFlexGrid1.Cols = Number of Columns) or through theProperties window.

    The Col and Row Properties

    Be careful not to confuse these two properties with the Cols and Rows propertiesbecause they have a different function. These properties dont set the number ofrows and columns, but rather store the numbers that represent the row and col-umn containing the active cell. Also keep in mind that this property startscounting columns (left to right) and rows (top to bottom) from 0 and not 1. Thus,if the Cols property is set to 5, youll find that Col property can equal any integerbetween 0 and 4, depending on which column contains the active cell. Pleasenote that the Col and Row properties are available only at runtime.

    The ColWidth and RowHeight Properties

    You only can manipulate these properties at runtime. Nevertheless, they can beuseful because they let you set a specified rows height as well as a specified col-umns width. Youll often find yourself needing to do this if default row or columndimensions are too large for the data to be displayed. The height and width ismeasured in twips.


    Chapter 9

    550ch09.qxp 1/14/02 3:13 PM Page 200

  • The ColAlignment Property

    This propertys value determines whether or not the data displayed in a particu-lar column is centered or justified. Setting it equal to 0 results in left justificationwhile setting it to 1 right justifies the data. If the property is set equal to 2, theinformation displayed will be centered. This property isnt available at designtime (except indirectly through the FormatString property).

    The CellLeft, CellWidth, CellTop, and CellHeight Properties

    Just like with the control-level Left, Width, Top, and Height properties (nowreferred to as Location.X, Size.Width, Location.Y, and Size.Height in VB .NET),these properties return the size and position of the current cell in twips. The onlydifference is that the cell properties refer to the frame that encloses the grid andnot the form. These properties arent available at design time and are read-only at runtime.

    The FixedCols and FixedRows Properties

    These properties control the grids number of fixed rows and columns. The fixedrows and columns always appear at the grids top and left sides (non-scrollable)and in gray instead of white. These types of cells generally provide some type ofheader for the rows and columns.

    The FixedAlignment Property

    This property works exactly like the ColAlignment property. The only difference isthat it controls the alignment in fixed cells and not normal cells.

    The GridLines Property

    This property controls whether or not grid lines are within your grid control. Thedefault displays the grid lines, which generally makes it much easier for the enduser to distinguish the different cells.


    Programming Your Own Spreadsheets

    550ch09.qxp 1/14/02 3:13 PM Page 201

  • The Sort Property

    This property allows you to sort the contents of selected columns according toselected criteria. For example, the user could choose to sort in ascending ordescending orders, or not sort the data at all. This property isnt available atdesign time and is write-only at runtime. When set at the code level, the sortingtakes place immediately after the line of code executes.

    The Text Property

    Youre very familiar with the Text property by now, but keep in mind that propertystores only the text of the active cell, and not the text in any other cell. Thus, this property continually changes as you switch from cell to cell. Reading the Textproperty returns the current cells contents as defined by the Row and Col prop-erties. Writing to the Text property sets the contents of the current cell orselection (a range of cells) depending on the FillStyle property.

    MSFlexGrid Events

    MSFlexGrid controls respond to many of the events youve already seen, such asthe Click and DoubleClick events. However, several useful events are unique tothis control, and are covered in this section.

    The EnterCell Event

    This event occurs when the program user clicks on or keys into a new cell that isdifferent from the cell that is currently selected. The EnterCell event works likethe GotFocus event, only it deals with single cells instead of a control.

    The LeaveCell Event

    This event is the opposite of the EnterCell event: it occurs when the focus isshifted away from th