Vb.net 2008: Textbox Undo method issue?

R

Rob W

Greetings,

I have code to insert current date/time into a textbox (see below)

txtEditor.SelectedText = Format(Now, "HH:mm dd/MM/yyyy") 'Insert date in the
format of "HH:mm dd/MM/yyyy")

However this transaction cannot be undone as seen with the following debug
message (returns false):-
MsgBox(txtEditor.CanUndo)

I attempted to insert date with the following code:-
txtEditor.AppendText(Format(Now, "HH:mm dd/MM/yyyy"))

Again this transaction cannot be undone with
txtEditor.undo().

Can anyone suggest how I can undo the insertion of the date into the text
box?

Thanks
Rob
 
A

Andrew Morton

Rob said:
I have code to insert current date/time into a textbox (see below)

txtEditor.SelectedText = Format(Now, "HH:mm dd/MM/yyyy") 'Insert date
in the format of "HH:mm dd/MM/yyyy")

However this transaction cannot be undone as seen with the following
debug message (returns false):-
MsgBox(txtEditor.CanUndo)

Don't you have to do txtEditor.Cut() to put the overwritten text into the
clipboard before you can do an undo?

Andrew
 
A

Andrew Morton

Andrew said:
Don't you have to do txtEditor.Cut() to put the overwritten text into
the clipboard before you can do an undo?

Errr.... no. I just checked.

What you should be doing is using txtEditor.Paste(whatever) to do an
undo-able paste onto the selection.

(Well, OK, you /could/ do the Cut() yourself, but there's no need to.)

Andrew
 
O

OmegaSquared

Hello, Rob,

I think that you can only undo changes that the user has made (i.e. changes
made through code cannot be undone).

If you need to undo such a change, perhaps you can store the original Text
value someplace prior to changing it, and then restore it from there when
required. If you are not using the TextBox's Tag property for anything else,
you could store the value there. Actually, I suppose that you could keep a
stack in the Tag property and store multiple previous values on the stack.

Cheers,
Randy
 
A

Andrew Morton

OmegaSquared said:
I think that you can only undo changes that the user has made (i.e.
changes made through code cannot be undone).

My testing says that is not the case. What apparently should be done is to
use the Cut/Copy/Paste methods provided which automatically interact with
the clipboard to provide the .Undo() functionality. The OP was directly
writing over the selection without using any of those methods, hence
bypassing the built-in undo capability.

Andrew
 
O

OmegaSquared

Hello, Andrew,

Well, I admit that my testing is all done in dotNet 2005. Sadly, I haven't
yet had the privilege of working with 2008. And so circumstances in that
version may indeed be different. Using the TextBox's Paste method in v2005
does set the CanUndo property to True, just as in v2008 as your testing
reveals. In v2005, the TextBox's CanUndo property is also set to True if the
user manually modifies the contents of the TextBox, but not if the contents
are modified by setting the TextBox's Text property in code. Are you saying
that this behaviour is different in v2008?

Re: "...What apparently should be done is to use the Cut/Copy/Paste
methods..."

I haven't seen any indication that the OP wishes to use (or alter) the
contents of the clipboard. He only seems to wish to modify the contents of
the TextBox's Text property.

Personally, I wouldn't want any of my applications to have the side effects
associated with altering the clipboard contents in order to set the value of
a Text box or for any other "hidden" purpose. The clipboard contents should
only be changed as a result of the user's purposeful interaction. I suppose
that the OP might be able to get around this by saving the current clipboard
contents (and formats), setting the clipboard contents to the text he wishes
to paste, pasting the clipboard into the TextBox, and then restoring the
clipboard with its original contents (and formats). This seems a bit
convoluted just to use the TextBox's Undo method (which, in v2005 is really
rather limited anyway).

Cheers,
Randy
 
R

Rob W

Thanks, I will explore that.

Andrew Morton said:
Errr.... no. I just checked.

What you should be doing is using txtEditor.Paste(whatever) to do an
undo-able paste onto the selection.

(Well, OK, you /could/ do the Cut() yourself, but there's no need to.)

Andrew
 
R

Rob W

If you manually type something in the textbox the CanUndo property is set to
true as is when you do a Cut/Copy/Paste in V2008.

I have used the paste function so the action can be undone.

Thanks all
 
C

Cor Ligthert[MVP]

Well, I admit that my testing is all done in dotNet 2005. Sadly, I
haven't
yet had the privilege of working with 2008.
This seems a bit
convoluted just to use the TextBox's Undo method (which, in v2005 is
really
rather limited anyway).
Why this is the same in version 2002,2003, 2005 and 2008, can it not be that
you don't understand it completely?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top