Undo changes in a TextBox control

The TextBox control supports the capability to undo changes, but there is no property or method that exposes this feature. You can achieve the same goal with a a set of messages.

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal _    hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _    lParam As Any) As LongConst EM_CANUNDO = &HC6Const EM_UNDO = &HC7Const EM_EMPTYUNDOBUFFER = &HCD

The EM_CANUNDO message returns a non-zero value if the contents of the TextBox control can undone. The EM_UNDO message actually undoes all changes:. The following code demonstrates how you can use these messages in a typical Edit menu:

Private Sub mnuEdit_Click()    If TypeOf ActiveControl Is TextBox Then        ' check whether changes to the current         ' TextBox control can be undone        mnuEditUndo.Enabled = SendMessage(ActiveControl.hWnd, EM_CANUNDO, 0, _            ByVal 0&)    Else        ' in all other cases, disable this menu command        mnuEditUndo.Enabled = False    End IfEnd SubPrivate Sub mnuEditUndo_Click()    ' Undo the most recent edit operation on the active control     ' (no need to use TypeOf...Is to check the control's type)    SendMessage ActiveControl.hWnd, EM_UNDO, 0, ByVal 0&End Sub

Finally, the EM_EMPTYUNDOBUFFER clears the internal undo buffer, so that the user can’t undo changes.

SendMessage ActiveControl.hWnd, EM_EMPTYUNDOBUFFER, 0, ByVal 0&

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

technology leadership

Why the World Needs More Technology Leadership

As a fact, technology has touched every single aspect of our lives. And there are some technology giants in today’s world which have been frequently opined to have a strong influence on recent overall technological influence. Moreover, those tech giants have popular technology leaders leading the companies toward achieving greatness.

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job