Codejock Forums Homepage
Forum Home Forum Home > Codejock Products > ActiveX COM > Command Bars
  New Posts New Posts RSS Feed - Proper Validation of xtpControlEdit (VB6)
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Proper Validation of xtpControlEdit (VB6)

 Post Reply Post Reply
Author
Message
Milkman View Drop Down
Newbie
Newbie


Joined: 16 September 2005
Status: Offline
Points: 11
Post Options Post Options   Thanks (0) Thanks(0)   Quote Milkman Quote  Post ReplyReply Direct Link To This Post Topic: Proper Validation of xtpControlEdit (VB6)
    Posted: 16 September 2005 at 11:06pm
Most of my applications require that any text entered into an edit box be checked for consistency.  For instance, if the user enters xyz in an ID# edit box, you wouldn't want your program to use a letter value when it should use a number value.  The typical desired result would be to discard the incorrectly entered value and restore the previous contents of the edit box.

Unfortunately, the method in which to do this isn't exactly spelled out in the substandard documentation.  Adding insult to injury, there isn't even an editbox worked into ANY of the command bar samples.  Below is the required code to implement proper validation of an xtpControlEdit within a command bar.  It uses the Tag (they call it the "Paramater") property of the edit control to store the previous contents of the edit box.

You'll first need to create an ID number for the editbox.  Create this global value in a seperate module where you store your constants.

Public Const ID_EDIT = 205

You then need to construct your command bar.

Private Sub cbToolbar()

'Declare Temporary Variables
Dim Control As CommandBarControl
Dim Toolbar As CommandBar
Dim EditControl As CommandBarEdit

'Set Toolbar properties
Set Toolbar = CommandBars.Add("Test Command Bar", xtpBarBottom)

'Create Toolbar
With Toolbar.Controls
     Set EditControl = .Add(xtpControlEdit, ID_EDIT, "")
            EditControl.Text = "Test"
            EditControl.Parameter = EditControl.Text
End With

End Sub


After the control exists you'll need to put some additional code in the "ControlSelected" (catchs when you press the Enter key) and "Update" (catch the case where you change the text and then just leave the text box without pressing the enter key) Events.

Private Sub CommandBars_ControlSelected(ByVal Control As XtremeCommandBars.ICommandBarControl)

'Process Selected Events
If Control Is Nothing Then Exit Sub
Select Case Control.Id
    Case ID_EDIT:
        If Control.Parameter <> Control.Text Then EditBox_Validate Control
End Select

End Sub


Private Sub CommandBars_Update(ByVal Control As XtremeCommandBars.ICommandBarControl)

'Process Update Requests
If Control Is Nothing Then Exit Sub
Select Case Control.Id
    Case ID_EDIT:
        If (Control.HasFocus = False) And (Control.Text <> Control.Parameter) Then EditBox_Validate Control
End Select

End Sub


You're probably wondering why the "If Control Is Nothing Then Exit Sub" line at the beginning of each event handler.  Well it would appear that these event handlers are sometimes called without a control variable passed to it.  Very strange.  The example programs choose to use the "On Error Resume Next" command to juse ignore any errors.  I don't like this approach because it turns off error checking for the rest of the code in the event handler.  A bad thing when you're trying to debug your program.

Finally you'll need to add the validation routine.  Use something like this.

Private Sub EditBox_Validate(ByRef EditBox As CommandBarEdit)
'This sub will validate the edit box text

'Check the text in the EditBox.Text property

'If bad then replace replace .Text with .Parameter
'Maybe you want to raise an error

'If text is good then let it be and update the .Parameter for the next time the
'validate routine is called

'Validation Successful
EditBox.Parameter = EditBox.Text
End Sub


If you want your program to programmatically update the text in the editbox; make sure to change BOTH the .Text and the .Parameter properties to avoid having the validate routine execute needlessly.

Now if I can only figure out how to centre text in the thing, I'll be set.





Edited by Milkman
Back to Top
apuhjee View Drop Down
Senior Member
Senior Member
Avatar

Joined: 02 September 2005
Location: United States
Status: Offline
Points: 130
Post Options Post Options   Thanks (0) Thanks(0)   Quote apuhjee Quote  Post ReplyReply Direct Link To This Post Posted: 07 May 2006 at 10:52am
Excellent post - thanks Milkman, you saved a few hairs on my head!

Cheers ~ jp
I like mathematics because it is not human and has nothing particular
to do with this planet or with the whole accidental universe — because,
like Spinoza's God, it won't love us in return.
Back to Top
 Post Reply Post Reply
  Share Topic   

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.04
Copyright ©2001-2021 Web Wiz Ltd.

This page was generated in 0.047 seconds.