Measurement Studio for VB6

cancel
Showing results for 
Search instead for 
Did you mean: 

cwNumEdit values not taking affect

I'm having a problem with the cwNumEdit control and having a default command button. If I have a form with one cwNumEdit control (say cwNumEdit1) and a command button (say cmdOK) with the default value set to true I want it so that when I hit the enter key to have a messagebox that pops up and gives me the value of cwNumEdit1. This isn't working for me in all instances.

Say the cwNumEdit1.Value field is equal to 5. I hit the increment or decrement arrows and hit enter, it works fine. If I type in the number and click ok, it works fine. However, if I type in the number and hit enter, I get the message box giving me the original value that was in cwNumEdit1 before I started typing.

i.e. First cwNumEdit1.Value = 5. I click on cwNumEdit1 and change the value to 9 and hit enter. I get a message box that pops up as 5.

It appears this happening because the cwNumEdit control was written to register the new value in the control only when either the inc/dec buttons are hit, when focus is lost, or when the enter key is struck. The last part is where the problem comes in as if a button is set to default and the enter key is struck.

Proof of this seems to appear as if one puts in 2 message boxes in a row in their cmdOK function. On the second message box the change does appear.

Here's the code example.

Private Sub cmdOK_Click()
MsgBox CWNumEdit1.Value
' MsgBox CWNumEdit1.Value ' uncomment to see the real value register
End Sub

Set the cmdOK parameter default to true, run the form and type in a number on the cwNumEdit1 control then just hit enter (don't cause the button to lose focus) and you should be able to repeat the problem.

We've come up w/a software hack to solve this problem, but it adds more lines of code that need not be there. If there's an easy solution to this (besides making the cmdOK button a non-default b/c then users have to use the mouse or tab all over the place), please, let me know as I'd appreciate the gained knowledge.
0 Kudos
Message 1 of 3
(6,015 Views)
This seems to be an affect of VB's interaction with activex control. One easy workaround for this would be to programmatically set focus to the cwnumedit control using SetFocus before you read the cwnumedit. Something like the following

CWNumEdit1.SetFocus
MsgBox CWNumEdit1.Value


Hope this helps

Bilal Durrani
NI
Bilal Durrani
NI
0 Kudos
Message 2 of 3
(6,015 Views)
One way that you could work around this is to send a WM_KILLFOCUS message to the edit box of the numeric edit control before getting the value. I don't know if this is any better than the workaround that you came up, but one advantage of this is that you could encapsulate everything in a single function and then just call that function instead of accessing the property, so the only extra lines of code is for the function. Here are the steps to do it:

First, download olelib.tlb and unzip it. Then go to Project->References in your VB project, click Browse, then browse to the olelib.tlb file that was unzipped from the zip file downloaded above.

Now add the following code to your
project:

Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" ( _
ByVal hWnd1 As Long, _
ByVal hWnd2 As Long, _
ByVal lpsz1 As String, _
ByVal lpsz2 As String _
) As Long

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Long _
) As Long

Private Const WM_KILLFOCUS = &H8

Private Function GetNumEditValue(numEdit As CWNumEdit) As Double
Dim window As IOleInPlaceObject
Set window = numEdit.Object
Debug.Assert Not window Is Nothing

Dim numEditWnd As Long
numEditWnd = window.GetWindow
Debug.Assert Not numEditWnd = 0

Dim editWnd As Long
editWnd = FindWindowEx(numEditWnd, 0, "edit", "")
Debug.Assert Not editWnd = 0

SendMessage editWnd, WM_KILLFOCUS, 0, 0
GetNumEditValue = CDbl(numEdit.Value)
End Function

Then replace this code:

MsgBox CWNumE
dit1.Value

With this code:

MsgBox GetNumEditValue(CWNumEdit1)

You should see that GetNumEditValue will always return the current value. Hope this helps.

- Elton
Message 3 of 3
(6,015 Views)