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