分享

Use eVB to create display and edit forms for your Pocket PC

 高高兴兴 2006-01-17

Use eVB to create display and edit forms for your Pocket PC.
Written by Carl Davis  [author‘s bio]  [read 19469 times]
Edited by Derek

eVB Ver 3.0   

The Pocket PC is an ideal platform for carrying information around that you need to review often and occasionally edit. This leaves a developer with a dilemma - do I optimize the display for entry or for displaying information. Microsoft solved this problem for the contacts function by having two screens. Unfortunately, the screens are completely different and editing requires the user to scroll through all the fields. I‘ve found that there is an alternative. This article presents a simple technique to create eVB forms that allow users to edit information without jumping to another screen.

The example in Figure 1 shows a hypothetical screen designed to display statistics for a football player. This might be something a fantasy football fan would keep handy to compare his or her players with friends. The important thing to note (besides the fact the player is a very good running back) is that there is a lot of information crammed on the screen and it is laid out to optimize it‘s utility to the viewer. All of the text display elements in this example are Label controls. To allow the user to update the information on this player, I want to allow them to tap and edit without jumping to an entirely different layout or form. Using a few simple steps we can allow the user to tap on any of the fields and edit it.


Figure 1

By manipulating and repositioning controls we can make a text box, combo box, or other control element appear ready for input from the user without leaving the screen. Figure 2 shows how the application responds when the user taps on the player‘s name "Ed V. Barber" (his friends call him edVB). The text box appears with the name highlighted. Once the user taps on a different part of the form or the "OK" button the text box disappears.


Figure 2

To demonstrate how this is accomplished, I‘ll use a simple text box and one of our fields on the player statistics form. However, the routines provided are easily modified to work with combo boxes, check boxes, or other controls.

To follow along create a form with the following controls: a Text Box named txtEntry and a command button named cmdOK with a caption of "OK", a Frame called Frame1, and finally two labels called lblNameDisplay and lblNameHeader which are placed into Frame1. lblNameHeader is the label that say "Name:" and lblNameDisplay will contain our information (edVB‘s name).

Listing 1 on the next page shows the functions we need to handle moving and displaying the list box.

Option Explicit

Const MINENTRYSIZE = 450 ‘ Minimum text box size
Const OKOFFSET = 15 ‘ Spacing between OK and text box

Public lblCurrent As Label
Public fraCurrent As Frame

‘ Disable an existing text box
Private Sub disableTextBox()
‘Remove the entry box from display
txtEntry.Visible = False
cmdOK.Visible = False

‘ make sure the object exists before working it.
If (IsObject(fraCurrent)) Then
fraCurrent.ZOrder
fraCurrent = Null
End If

‘ No longer have valid label
If (IsObject(lblCurrent)) Then
lblCurrent = Null
End If

End Sub

‘ Enables text box, called by Mousedown event on Label
Private Sub enableTextBox(ByRef clbl As Label, ByRef cfra As Frame)
‘ will hold the new positions
Dim myTop As Single
Dim myLeft As Single

‘ remove the previous text box if it‘s still displayed
Call disableTextBox

If (IsObject(clbl) And IsObject(cfra)) Then

Set lblCurrent = clbl
Set fraCurrent = cfra
Else
‘ nothing to affect, return
‘ you may add other error handling or logging
Exit Sub
End If

‘ The text box is located on top of the label
‘ Note: label is also located w/r to the frame
myTop = lblCurrent.Top + fraCurrent.Top
myLeft = lblCurrent.Left + fraCurrent.Left

‘ push the current frame behind the text box
fraCurrent.ZOrder 1

‘ Starting text inforation was label informatino
txtEntry.Text = lblCurrent.Caption

‘ The text box perimeter takes up some of the space available
‘ if this is too small there will be no characters visible
If (lblCurrent.Width > MINENTRYSIZE) Then
txtEntry.Width = lblCurrent.Width
Else
txtEntry.Width = MINENTRYSIZE
End If

‘ Place the text box at our coordinates
txtEntry.Move myLeft, myTop

‘ separate function to place OK button
Call placeTextEntryOK

‘ Make the text box visible
txtEntry.Visible = True
‘ Give the entry box the focus so user can begin
‘ using it immediately. Also set the text as selected
‘ so user can erase with first input stroke.
txtEntry.SetFocus
txtEntry.SelStart = 0
txtEntry.SelLength = Len(txtEntry.Text)

End Sub

‘ Places the OK button in the proper location
Private Sub placeTextEntryOK()
‘ Location to place OK button
Dim myLeft As Single
Dim myTop As Single
‘ size of OK
Dim wid As Single

‘ set the width, we need it to compare
wid = cmdOK.Width

‘ calculate placing on right hand side with a small buffer
myTop = txtEntry.Top + fraCurrent.Top
myLeft = txtEntry.Left + fraCurrent.Left + txtEntry.Width + OKOFFSET

‘ insure that the OK button will be visible and still
‘ is within current frame‘s display
If (myLeft + wid > fraCurrent.Width) Then
‘ If not place on left hand side of text box
myLeft = txtEntry.Left + fraCurrent.Left - wid - OKOFFSET
End If

‘ Move the OK button and make it visible
cmdOK.Move myLeft, myTop
cmdOK.Visible = True

End Sub

The first thing you‘ll notice is that the enableTextBox() function requires both Label and Frame references. It‘s obvious we‘ll need to keep track of which Label was selected so we can update it as the user enters new text. However, the frame requires some explanation. For this technique to work, the txtEntry text box and "OK" button must appear on top of everything on the form. This is accomplished using the Zorder property of the controls. Instead of changing every control‘s Zorder property that might be underneath the txtEntry and cmdOK, everything is placed in a Frame. Since the Frame‘s Zorder attribute affects every control it contains, this provides a much more convenient method of managing getting our text box to come out on top.

The listing also shows how we determine where to place the control by using the coordinates of the Label and it‘s position in the frame. This assumes the txtEntry and cmdOK Command Button are not part of the frame and reside on the main form. I recommend this since it allows us to use a single set of controls to support a large number of frames in the application. I have an application where there are 9 frames where on is made visible based on the task the user is performing. Reusing entry fields saved significant time.

The enableTextBox() routine also makes the width of txtEntry the same as the label it will lay on top of. However, I‘ve added a minimum size requirement. Text Boxes have borders that take up space or you might use a larger font for the input characters. Thus for narrow sets of text, like the Q1 score on edVB‘s statistics sheet, we might be able to see only one character! A minimum length insures we‘ll always have a couple of characters visible in the Text Box.

Once txtEntry is moved to the new position and we‘ve copied the contents from the label‘s caption, the routine sets the selected text to everything in the field. This is important because it allow the user to quickly change the entire contents of the Text Box without first selecting it. We also call a routine placeTextEntryOK that determines where to place the OK button on the form. This routine checks if there is enough room for the cmdOK button on the right-hand side of the screen. This allows us to place our labels anywhere and as close to the edges as we desire without make allowances for a button that will appear later.

The disableTextBox procedure is provided to remove the Text Box and OK button from the display area and return the Frame to its prior Zorder state. As you add more controls that use this technique, you should have each "enable" routine call all the other control‘s "disable" routines (i.e. your enableComboBox() should call disableTextBox() prior to setting up it‘s controls)

The events on txtEntry and cmdOK (shown in Listing 2) are very simple. We keep the caption updated as changes are made and remove the controls when either the text box loses focus or OK is selected.

Listing 2

Private Sub txtEntry_Change()
lblCurrent.Caption = txtEntry.Text
End Sub

Private Sub txtEntry_LostFocus()
If (IsObject(lblCurrent)) Then
lblCurrent.Caption = txtEntry.Text
End If
Call disableTextBox
End Sub

Now that we have all the supporting routines setup, it is a very simple matter to enable one of labels for editing. Listing 3 shows two of our labels used for the name of our player. One is the descriptor "Name:" and the other holds the actual content (edVB‘s name).

Listing 3

Private Sub lblNameDisplay_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Call enableTextBox(lblNameDisplay, Frame1)
End Sub

Private Sub lblNameHeading_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Call enableTextBox(lblNameDisplay, Frame1)
End Sub

By attaching the action of enabling the entry field and button to both the descriptive label and contents we give the user flexibility. Some users will naturally select the label and others will tap on the content, we can handle both.

Now with everything in position, when the user taps on the player‘s name, a text box will appear in the correct position. The technique described provides a simple to implement mechanism that is easily enhanced to include other types of controls and other command buttons. I‘ve personally added buttons to increment and decrement, toggling between two values, and combo boxes with a great deal of success.

Carl Davis
Carl is currently Vice President of Technology at Click Commerce, an industry-leading provider of private exchange software for global manufacturers. He has over ten years of experience developing and architecting software for a rich variety of systems-from complex embedded machine vision algorithms in manufacturing to Enterprise scale knowledge management systems to B2B E-Commerce Solutions. Carl‘s current interests are in small footprint platforms including eVB and Java on both the PocketPC and Palm computing platforms and plans on releasing a series of PocketPC applications from his own company (www.) for gamers. (as soon as the user guides are complete!)

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多