Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I need something like "OnPaste event" for TextBox 1

Status
Not open for further replies.

alfer

Programmer
Jun 12, 2003
41
0
0
PL
Hi

I would like to write a special handling of paste operation for a TextBox. (Only some characters are allowed in this TextBox).
I have written code proving pasted text when the user presses CTRL+INS or CTRL+V. [highlight]But I have no idea how to catch "paste operation" from context menu (right mouse click on the TextBox).[/highlight]
An Event like "OnPaste" would be perfect, but I cannot find such an event :(

Thanks for any help
 
use the text changed event do to all the validation on the textbox.

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
Yes this is a good idea but new problem appears:
When a text is pasted I would like
A) to allow to past it or
B) to deny.

When I allow - I left the text as it is.
When I deny - I want to return to the previous text.

Have I to declare an additional String oldText? May by do you know another method?

[tt]
private void txt1_TextChanged(object sender, System.EventArgs e)
{
if( IsEverythingOK(txt1.Text) )
{
//characters are ok - remember this new value
oldTxt=txt1.Text;
}
else
{
//one or more characters are disallowed
//return to the old value
txt1.Text=oldTxt;
}
}
[/tt]
 
yes, you need to declare an additional string variable that holds the last correct value.

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
#region constants
private const int WM_COPY = 0x301;
private const int WM_CUT = 0x300;
private const int WM_PASTE = 0x302;
private const int WM_CLEAR = 0x303;
private const int WM_UNDO = 0x304;
private const int EM_UNDO = 0xC7;
private const int EM_CANUNDO = 0xC6;
#endregion

/// <summary>
/// Capture command keys
/// </summary>
/// <param name="msg"></param>
/// <param name="keyData"></param>
/// <returns></returns>
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
switch(keyData)
{
case Keys.Enter:
if(this._TabOnEnter)
return base.ProcessCmdKey(ref msg, Keys.Tab);
return true;
case Keys.Control | Keys.V:
case Keys.Shift | Keys.Insert:
PostMessage(this.Handle, WM_PASTE, 0, 0);
return true;
case Keys.Control | Keys.C:
case Keys.Control | Keys.Insert:
PostMessage(this.Handle, WM_COPY, 0, 0);
return true;
case Keys.Control | Keys.X:
case Keys.Shift | Keys.Delete:
PostMessage(this.Handle, WM_CUT, 0, 0);
return true;
case Keys.Control | Keys.Delete:
PostMessage(this.Handle, WM_CLEAR, 0, 0);
return true;
case Keys.Control | Keys.Z:
PostMessage(this.Handle, WM_UNDO, 0, 0);
return true;
default:
return base.ProcessCmdKey (ref msg, keyData);
}
}
/// <summary>
///
/// </summary>
/// <param name="m"></param>
protected override void WndProc(ref Message m)
{
switch(m.Msg)
{
case WM_COPY:
if(this._ClipMode == ClipModeConstants.mskExcludeLiterals)
Clipboard.SetDataObject(RemoveFormatCharacters(base.SelectedText).Replace(this._PromptChar, ""));
else
Clipboard.SetDataObject(base.SelectedText.Replace(this._PromptChar, " "));
break;
case WM_PASTE:
case WM_CLEAR:
base.WndProc(ref m);
break;
case WM_CUT:
Clipboard.SetDataObject(base.SelectedText);
this.InsertChararacter(1, "");
break;
//TODO: ova ne ke bide loso da se napravi
case WM_UNDO:
case EM_UNDO:
m.Result = new IntPtr(1);
break;
case EM_CANUNDO:
m.Result = new IntPtr(0);
break;
default:
base.WndProc (ref m);
break;
}
}

I've paste complete working code. You must comment some lines where my special properties are included. I have one problem with this code and if you have a opinion make a post. I don't now how make code part for handling WM_UNDO, EM_UNDO and EM_CANUNDO, to work.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top