Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

C# Paste Question

  • 18-04-2006 6:11pm
    #1
    Moderators, Education Moderators, Technology & Internet Moderators Posts: 35,125 Mod ✭✭✭✭


    Hi all

    I'm just wondering is there any way to make a textbox so that the user can't paste data into it.


Comments

  • Registered Users, Registered Users 2 Posts: 44,200 ✭✭✭✭Basq


    You mean they specifically can't paste so they can still type into it?...

    .. Like for a password or validation key.


  • Closed Accounts Posts: 261 ✭✭bishopLEN


    Hi all

    I'm just wondering is there any way to make a textbox so that the user can't paste data into it.
    Hi,
    For the Ctrl + V scenario you can use this
              protected override bool ProcessCmdKey(ref Message msg,     Keys keyData)
              {
                   if(keyData == (Keys.Control | Keys.V))
                        return true;
                   else
                        return base.ProcessCmdKey(ref msg, keyData);
              } 
    

    You can set the ContextMenu property of the TextBox to a dummy, empty ContextMenu instance.


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    you can put in a KeyEvent handler to preprocess all keypresses and if the user presses "Ctrl + V" you can choose to make that combination do nothing. But i wouldn't be too sure on how to stop them rightclicking and pasting. I'm sure there'd be a similar way to stop that from happening.


  • Moderators, Education Moderators, Technology & Internet Moderators Posts: 35,125 Mod ✭✭✭✭AlmightyCushion


    Yeah I still want them to be able to type data into the text box.

    Using the keydown eventhandler was something I was planning on doing but like you said it doesn't disable for the right click pasting option.

    I was hoping there was just some thing like a property you could enable or disable.


  • Registered Users, Registered Users 2 Posts: 44,200 ✭✭✭✭Basq


    I was hoping there was just some thing like a property you could enable or disable.
    Let me guess.. you're a student! :D

    I was amazed when i discovered you actually had to write code for form design.

    But bishopLEN's solution above with the context menu will work.


  • Advertisement
  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    http://www.dotnet247.com/247reference/msgs/30/150798.aspx

    That should answer all your questions.


  • Moderators, Education Moderators, Technology & Internet Moderators Posts: 35,125 Mod ✭✭✭✭AlmightyCushion


    basquille wrote:
    Let me guess.. you're a student! :D

    I was amazed when i discovered you actually had to write code for form design.

    Yeah I'm a student but we don't have to write code for form design(although we are encouraged to look at it and understand what it's doing). What would be the point of that when you have the designer there to do all that for you.


  • Registered Users, Registered Users 2 Posts: 44,200 ✭✭✭✭Basq


    No, i think you missed my point!

    What i'm saying is there are some elements of form design where you can't just check a property of a control.

    Most items can be simply dragged-and-dropped using the designer of course.


  • Moderators, Education Moderators, Technology & Internet Moderators Posts: 35,125 Mod ✭✭✭✭AlmightyCushion


    Alright I have no idea how to do anything with the context menu. I can't change it in the properties window. It has a value of none that can't be changed. Any insight on this please.


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    Alright I have no idea how to do anything with the context menu. I can't change it in the properties window. It has a value of none that can't be changed. Any insight on this please.
    You looked at my link?


  • Advertisement
  • Moderators, Education Moderators, Technology & Internet Moderators Posts: 35,125 Mod ✭✭✭✭AlmightyCushion


    basquille wrote:
    No, i think you missed my point!

    What i'm saying is there are some elements of form design where you can't just check a property of a control.

    Most items can be simply dragged-and-dropped using the designer of course.

    Sorry completely misunderstood what you were trying to say there. I thought you were saying that students were being forced to code all the form design stuff which depending on the complexity of the form design could be pretty horrible.


  • Moderators, Education Moderators, Technology & Internet Moderators Posts: 35,125 Mod ✭✭✭✭AlmightyCushion


    You looked at my link?

    Yeah I checked the link. The problem is I already have the text boxes created and I want to disable the ability to paste on alot of them. So I don't want to have to recreate them again (too time consuming) so I figured I'd see what other options I had and if nothing of much use turned up then I'd go down that route.

    One question though that code shows you how to create a control with the paste ability enabled. How would I go about creating it with it disabled.

    I was thinking this:

    case WM_PASTE:
    {
    if(/*CantPaste*/) //If you dont want to allow paste
    {}
    break;
    }


  • Registered Users, Registered Users 2 Posts: 44,200 ✭✭✭✭Basq


    Sorry.. off-topic but i see you're from Sligo: you studying computing in the IT?


  • Moderators, Education Moderators, Technology & Internet Moderators Posts: 35,125 Mod ✭✭✭✭AlmightyCushion


    basquille wrote:
    Sorry.. off-topic but i see you're from Sligo: you studying computing in the IT?

    Yep. Second year software development.

    What about yourself?


  • Registered Users, Registered Users 2 Posts: 44,200 ✭✭✭✭Basq


    Yep. Second year software development.

    What about yourself?
    Just finishing my 4th year now.. how you finding it?


  • Moderators, Education Moderators, Technology & Internet Moderators Posts: 35,125 Mod ✭✭✭✭AlmightyCushion


    basquille wrote:
    Just finishing my 4th year now.. how you finding it?

    Ah it's alright. It was a bit hard adjusting to second year. First year was such a piece of piss, I coulda done it in my sleep. Then having to actually use my brain for second year was tough. I wish first year wasn't so easy, it just made me lazy.

    Btw all this is in aide of powells project. The good auld WAPL system that the i.t has been doing since like the late seventies. Think they'd change it every so often. How did you find it when you did it? If you can remember that far back.


  • Registered Users, Registered Users 2 Posts: 44,200 ✭✭✭✭Basq


    Ah yes... the WAPL system. Can remember it but not too well - remember there was 5 of us in a group. One programming for a debtor, one for creditor etc. And myself and a mate got Finance (i think that's what it was) and it's the hardest section. We had to program ours in VB - as we didn't start learning C# until 3rd year. It's not too bad though in hindsight - it due up next week?

    Believe me.. it's nothing compared to the project next year and the one the year after.

    I'm just in the process of doing my my technical manual (minimum 10,000 word technical and user manuals) - not at all easy!


  • Moderators, Education Moderators, Technology & Internet Moderators Posts: 35,125 Mod ✭✭✭✭AlmightyCushion


    Yeah due up nect week and I still havn't got those stupid diagrams done yet either. I got finance too. One problem though I'm on my own so that has been fun. What's Powell like for marking the thing not too harsh I hope.

    10,000 word technical manual! I think that's considered a form of torture in most parts of the world. I still don't know what I'm gonna do for my project next year. I was thinking of doing a till system. I thought it would be cool and since I work in Tesco's I know exactly what it needs to do and how to make it good. I thought it would be kinda easy but the more I think about it the harder the thing seems to get.


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    if you use the link to create the custom textbox (called CustomTextBox i suppose) you could do a quick find/replace to replace TextBox with CustomTextBox and automagically all your textboxes would then be CustomTextBoxes which are identical to standard textboxes, except you can disable pasting into them.


  • Moderators, Education Moderators, Technology & Internet Moderators Posts: 35,125 Mod ✭✭✭✭AlmightyCushion


    if you use the link to create the custom textbox (called CustomTextBox i suppose) you could do a quick find/replace to replace TextBox with CustomTextBox and automagically all your textboxes would then be CustomTextBoxes which are identical to standard textboxes, except you can disable pasting into them.

    But how do I disable the pasting into them.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 640 ✭✭✭Kernel32


    basquille wrote:
    Believe me.. it's nothing compared to the project next year and the one the year after.
    And your final year projects pale in comparison to real life development.

    Here's what I would do..

    1. Create a control extender that adds a property to the textbox, AllowPaste true/false. Default to true http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbconExtenderObjects.asp

    2. The code in the extender would create an event end point(s) for the textbox key events you need to capture and you can kill the ctrl V there.

    3. Create an instance of a ContextMenu in the extender, add what you want for menu items, capture the events if needed. Set the ContextMenu property of the textbox to the new ContextMenu instance.

    4. Have a cup of tea.

    Don't put the code on the form, that's terrible design and would not be tolerated in any properly run software shop.


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    But how do I disable the pasting into them.
    By following the link, extending the control, and then if you get stuck on a specific part of the rather detailed instructions in the link ask here. I won't tell supply you the finished code :P


  • Moderators, Education Moderators, Technology & Internet Moderators Posts: 35,125 Mod ✭✭✭✭AlmightyCushion


    By following the link, extending the control, and then if you get stuck on a specific part of the rather detailed instructions in the link ask here. I won't tell supply you the finished code :P

    1. I never once asked you to give me the code and even if I did it wouldn't be much good to me as I have to show off my program to my lecturer and explain what it is doing and how it is doing it(and I'm pretty sure he'd be interested in how I managed to stop users pasting in to the text box.

    2. I was stuck on a specific part of the rather detailed instructions, the part that disables the ability to paste because as far as I can see it doesn't show that in the code in the link you provided. The only way I could see would be to send a different ref to the WndProc method so that it would go to the default case in the switch statement and therefore skip the WM_PASTE case.

    3. I suggested a way to modify this code so that it would disable the ability to paste and I was looking for feedback to see if that would work.
    How would I go about creating it with it disabled.

    I was thinking this:

    case WM_PASTE:
    {
    if(/*CantPaste*/) //If you dont want to allow paste
    {}
    break;
    }


    4. I am not disabling pasteing into the text box because the lecturer asked for it (Infact he told me not to bother with it), I am doing it to further my knowledge so when I do have to do something along the same lines I will know how to do it. If I just copy and pasted the code in to the program with no understanding of it then where would be the benefit.


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    If you want to disable pasting, all you have to do is (i assume, i havn't done this) is change:
    case WM_PASTE:
            {
                if(/*CanPaste*/) //If you want to allow paste
             {
                    base.WndProc(ref m);
                }
                break;
            }
    

    to
    case WM_PASTE:
            {
                if(false) //now the paste operation should never ever be processed
             {
                    base.WndProc(ref m);
                }
                break;
            }
    
    or possibly you might have to do some trick like m = NULL, or m.remove() to remove that from the message queue. I assume putting in false would do what you want.

    Basically, what the switch statement is doing (i think) is processing EVERY message that gets passed. If the message happens to be of type "PASTE", then you have the option of stopping it being processed (by stopping "base.WndProc(ref m);" from being called for the PASTE case).


  • Moderators, Education Moderators, Technology & Internet Moderators Posts: 35,125 Mod ✭✭✭✭AlmightyCushion


    This piece of code doesn't work anyways. I think it's because the condition for the if statement is a comment. How could it work and even if it did it would prove no point. The whole if block is not needed to paste.
    case WM_PASTE:
            {
                if(/*CanPaste*/) //If you want to allow paste
             {
                    base.WndProc(ref m);
                }
                break;
            }
    

    This probably won't work either.
    case WM_PASTE:
            {
                if(false) //now the paste operation should never ever be processed
             {
                    base.WndProc(ref m);
                }
                break;
            }
    


    This is what I did and it works perfectly.
    case WM_PASTE:
            case WM_PASTE:
           {
    	break;
           }
    

    Basically once you click on paste in the context menu it sends it to this case, it enters and does nothing then leaves.

    The only problem I have is that the paste option is still in the context menu. It doesn't do anything but it is still there. It's only a small problem I know but meh it's still pissing me off.


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    case WM_PASTE:
            {
                if(false) //now the paste operation should never ever be processed
             {
                    base.WndProc(ref m);
                }
                break;
            }
    
    is identical to
    case WM_PASTE:
            {
                break;
            }
    

    if(false) can never evaluate to true, so it will always just run the break; operation. Ideally what you'd do is have a bool called "IsPasteAllowed" (or whatever) and you'd run the code as:
    case WM_PASTE:
            {
                if(IsPasteAllowed == true) 
             {
                    base.WndProc(ref m);
                }
                break;
            }
    
    That way you can dynamically disable/enable pasting as you see fit.

    As for the context menu still containing the paste operation... you shoule be able to use similar techniques to disable the context menu if you want too... you already figured out how to disable paste, and its given you the message code for the ContextMenu, so just disable it!


  • Moderators, Education Moderators, Technology & Internet Moderators Posts: 35,125 Mod ✭✭✭✭AlmightyCushion


    Yeah I was thinking about disabling the context menu completely but I would prefer to have it there with the paste option disabled (I'm a picky little so and so amn't I).


  • Registered Users, Registered Users 2 Posts: 151 ✭✭sailorfoley


    Ok,

    there are 2 ways to Paste into a text box. Using a menu via the mouse or via Ctrl + V with the keyboard.

    To stop with the mouse:

    implement a custom context menu that does not define a paste operation and assign that to the textbox. This will override the default windows one.

    To stop with the keyboard:

    implement the keypress event of the text box. Set handled from the KeyPressEventArgs to be true. What this means is that the event message is not processed after this, therefore no text appears in the text box.

    Hope this helps as i think its the cleanest and easiest way to do it.


Advertisement