Allow only Copy/Paste Context Menu in System.Windows.Forms.WebBrowser Control - menu

The WebBrowser control has a property called "IsWebBrowserContextMenuEnabled" that disables all ability to right-click on a web page and see a context menu. This is very close to what I want (I don't want anyone to be able to right-click and print, hit back, hit properties, view source, etc).
The only problem is this also disables the context menu that appears in TextBoxes for copy/paste, etc.
To make this clearer, this is what I don't want:
This is what I do want:
I would like to disable the main context menu, but allow the one that appears in TextBoxes. Anyone know how I would do that? The WebBrowser.Document.ContextMenuShowing event looks promising, but doesn't seem to properly identify the element the user is right-clicking on, either through the HtmlElementEventArgs parameter's "FromElement" and "ToElement" properties, nor is the sender anything but the HtmlDocument element.
Thanks in advance!

have you considered writing your own context menu in javascript? Just listen to the user right clicking on the body, then show your menu with copy and paste commands (hint: element.style.display = "block|none"). To copy, execute the following code:
CopiedTxt = document.selection.createRange();
CopiedTxt.execCommand("Copy");
And to paste:
CopiedTxt = document.selection.createRange();
CopiedTxt.execCommand("Paste");
Source:
http://www.geekpedia.com/tutorial126_Clipboard-cut-copy-and-paste-with-JavaScript.html
NOTE: This only works in IE (which is fine for your application).
I know its not bulletproof by any means, but here is a code sample that should get you started:
<html>
<head>
<script type = "text/javascript">
var lastForm = null;
window.onload = function(){
var menu = document.getElementById("ContextMenu");
var cpy = document.getElementById("CopyBtn");
var pst = document.getElementById("PasteBtn");
document.body.onmouseup = function(){
if (event.button == 2)
{
menu.style.left = event.clientX + "px";
menu.style.top = event.clientY + "px";
menu.style.display = "block";
return true;
}
menu.style.display = "none";
};
cpy.onclick = function(){
copy = document.selection.createRange();
copy.execCommand("Copy");
return false;
};
pst.onclick = function(){
if (lastForm)
{
copy = lastForm.createTextRange();
copy.execCommand("Paste");
}
return false;
};
};
</script>
</head>
<body oncontextmenu = "return false;">
<div id = "ContextMenu" style = "display : none; background: #fff; border: 1px solid #aaa; position: absolute;
width : 75px;">
Copy
Paste
</div>
sadgjghdskjghksghkds
<input type = "text" onfocus = "lastForm = this;" />
</body>
</html>

//Start:
function cutomizedcontextmenu(e)
{
var target = window.event ? window.event.srcElement : e ? e.target : null;
if( navigator.userAgent.toLowerCase().indexOf("msie") != -1 )
{
if (target.type != "text" && target.type != "textarea" && target.type != "password")
{
alert(message);
return false;
}
return true;
}
else if( navigator.product == "Gecko" )
{
alert(message);
return false;
}
}
document.oncontextmenu = cutomizedcontextmenu;
//End:
I hope this will help you Anderson Imes

A quick look at the MSDN documentation shows that none of the mouse events (click, button down/up etc) are supported to be used in your program. I'm afraid its either or: Either disable conetxt menus, or allow them.
If you disable them, the user can still copy & paste using keyboard shortcuts (Ctrl-C, Ctrl-V). Maybe that gives you the functionality you need.

We ended up using a combination of both of the above comments. Closer to the second, which is why I gave him credit.
There is a way to replace the context menu on both the client-side web code as well as through winforms, which is the approach we took. I really didn't want to rewrite the context menu, but this seems to have given us the right mix of control.

Related

how to make it in one line after onclick

I have a div in my html:
.row(id="div_amount")
.col-sm-2
label(for="amount") Amount:
.col-sm-1
input(type="text", name="amount")
which is perfect as I expected. It displays in one line for both elements.
I have a radio button which have an onclick event for toggle the display of this div section.
function div_toggle() {
div_amount = document.getElementById('div_amount');
if (radio_value === 0) {
div_amount.style.display = "block";
} else {
div_amount.style.display = "none";
}
}
However, after click on the radio button, it always displays as two lines.
How could I fix it to always display in one line?
Please take a look at https://stackoverflow.com/help/mcve and try to include a reproducible example.
In bootstrap4, rows are displayed as flex, i.e., "display: flex;". So in your JavaScript if you change it to display as block, the columns within the row would be broken.
You can set its display to flex, like
if (radio_value === 0) {
div_amount.style.display = "flex";
} else {
div_amount.style.display = "none";
}
although setting HTML element styles in JavaScript is considered as bad practice IMO.

Paste as plain text Contenteditable div & textarea (word/excel...)

I would like the to paste text in a contenteditable div, but reacting as a textarea.
Note that I want to keep the formatting as I would paste it in my textarea (from word, excel...).
So.
1) Paste text in contenteditable div
2) I get the text from clipboard
3) I push my value from clipboard to my textarea, (dont know how??)
4) Get value from my textarea and place it in my contenteditable div
Any suggestions?
I'm CKEditor's core developer and by coincidence for last 4 months I was working on clipboard support and related stuff :) Unfortunately I won't be able to describe you the entire way how pasting is handled, because the tales of the impl are too tricky for me even after writing impl by myself :D
However, here're some hints that may help you:
Don't write wysiwyg editor - use one that exists. It's going to consume all your time and still your editor will be buggy. We and guys from other... two main editors (guess why only three exist) are working on this for years and we still have full bugs lists ;).
If you really need to write your own editor check out http://dev.ckeditor.com/browser/CKEditor/trunk/_source/plugins/clipboard/plugin.js - it's the old impl, before I rewrote it, but it works everywhere where it's possible. The code is horrible... but it may help you.
You won't be possible to handle all browsers by just one event paste. To handle all ways of pasting we're using both - beforepaste and paste.
There's number (huge number :D) of browsers' quirks that you'll need to handle. I can't to describe you them, because even after few weeks I don't remember all of them. However, small excerpt from our docs may be useful for you:
Paste command (used by non-native paste - e.g. from our toolbar)
* fire 'paste' on editable ('beforepaste' for IE)
* !canceled && execCommand 'paste'
* !success && fire 'pasteDialog' on editor
Paste from native context menu & menubar
(Fx & Webkits are handled in 'paste' default listner.
Opera cannot be handled at all because it doesn't fire any events
Special treatment is needed for IE, for which is this part of doc)
* listen 'onpaste'
* cancel native event
* fire 'beforePaste' on editor
* !canceled && getClipboardDataByPastebin
* execIECommand( 'paste' ) -> this fires another 'paste' event, so cancel it
* fire 'paste' on editor
* !canceled && fire 'afterPaste' on editor
The rest of the trick - On IEs we listen for both paste events, on the rest only for paste. We need to prevent some events on IE, because since we're listening for both sometimes this may cause doubled handling. This is the trickiest part I guess.
Note that I want to keep the formatting as I would paste it in my textarea (from word, excel...).
Which parts of formatting do you want to keep? Textarea will keep only basic ones - blocks formatting.
See http://dev.ckeditor.com/browser/CKEditor/trunk/_source/plugins/wysiwygarea/plugin.js#L120 up to line 123 - this is the last part of the task - inserting content into selection.
Current solution works perfect in IE/SAF/FF
But still i need a fix for "non" keyboard events, when pasting with mouse click...
Current solution for keyboard "paste" events:
$(document).ready(function() {
bind_paste_textarea();
});
function bind_paste_textarea(){
var activeOnPaste = null;
$("#mypastediv").keydown(function(e){
var code = e.which || e.keyCode;
if((code == 86)){
activeOnPaste = $(this);
$("#mytextarea").val("").focus();
}
});
$("#mytextarea").keyup(function(){
if(activeOnPaste != null){
$(activeOnPaste).focus();
activeOnPaste = null;
}
});
}
<h2>DIV</h2>
<div id="mypastediv" contenteditable="true" style="width: 400px; height: 400px; border: 1px solid orange;">
</div>
<h2>TEXTAREA</h2>
<textarea id="mytextarea" style="width: 400px; height: 400px; border: 1px solid red;"></textarea>
I have achieved this using rangy library to save and restore selections.
I also perform some other work using the library in the same functions, which I have stripped out of this example, so this is not optimal code.
HTML
<div><div id="editor"contenteditable="true" type="text"></div><div>
Javascript
var inputArea = $element.find('#editor');
var debounceInterval = 200;
function highlightExcessCharacters() {
// Bookmark selection so we can restore it later
var sel = rangy.getSelection();
var savedSel = sel.saveCharacterRanges(editor);
// Strip HTML
// Prevent images etc being pasted into textbox
inputArea.text(inputArea[0].innerText);
// Restore the selection
sel.restoreCharacterRanges(editor, savedSel);
}
// Event to handle checking of text changes
var handleEditorChangeEvent = (function () {
var timer;
// Function to run after timer passed
function debouncer() {
if (timer) {
timer = null;
}
highlightExcessCharacters();
}
return function () {
if (timer) {
$timeout.cancel(timer);
}
// Pass the text area we want monitored for exess characters into debouncer here
timer = $timeout(debouncer, debounceInterval);
};
})();
function listen(target, eventName, listener) {
if (target.addEventListener) {
target.addEventListener(eventName, listener, false);
} else if (target.attachEvent) {
target.attachEvent("on" + eventName, listener);
}
}
// Start up library which allows saving of text selections
// This is useful for when you are doing anything that might destroy the original selection
rangy.init();
var editor = inputArea[0];
// Set up debounced event handlers
var editEvents = ["input", "keydown", "keypress", "keyup", "cut", "copy", "paste"];
for (var i = 0, eventName; eventName = editEvents[i++];) {
listen(editor, eventName, handleEditorChangeEvent);
}

SharePoint Change OK button text to Submit in newitem.aspx of lists

I have a problem with the sharepoint lists. I need to change the OK button to display as Submit. Anyone has any ideas how to do that?
Thanks,
Jason
1) In your URL box, after NewForm.aspx (or EditForm.aspx,) add this text: ?toolpaneview=2
Your url should look like "http://mysite.com/mylist/NewForm.aspx?toolpaneview=2"
2) Hit enter. The page will open in Shared editing mode. Choose "Add a Web Part" anywhere on the page.
3) Add a Content Editor Web Part. In the Text Source of the Content Editor Web Part, paste the following code:
<script type="text/javascript">
function changeOKButtons()
{
var inputs = document.getElementsByTagName("input");
for(i = 0; i<inputs.length; i++)
{
if(inputs[i].type == "button" && inputs[i].value == "OK")
inputs[i].value = "Submit";
}
}
_spBodyOnLoadFunctionNames.push("changeOKButtons");
</script>
If you can, use the jQuery equivalent of zincorp's code:
function changeButton()
{
$("input").each(function() {
if ($(this).attr("value") === "ButtonName") {
$(this).attr("value", "NewButtonName");
}
});
}
And if you have jQuery 1.6 or greater, use "prop" instead of "attr".

Is it possible to 'freeze panes' in SharePoint lists?

I need to keep my columns header, so people can see what data is displayed there, but I can not find if it is possible, or how to do it. Also will be nice to add a navigation bar to the list. Any suggestions?
Thanks,
I had similar challenge recently, and worked out the following code. The code works for IE, but there's a problem with the width of the freeze header in Chrome.
Anyway, hope it helps.
<script type="text/javascript">
$(document).ready(function(){
// Replace 'NameOfList' with the name of the SharePoint list
var $header = $("table[summary^='NameOfList']:first > tbody > tr.ms-viewheadertr");
var headertop = $header.offset().top;
// Replace 'NameOfColumn' with the name of the column that you would like to freeze
var $fzCol= $("tr.ms-viewheadertr th:contains('NameOfColumn')");
// IE has iFrame, Chrome doesn't have, so the 'n-th' count of the column is different in IE than in Chrome
if( $fzCol.siblings().eq(0).children().eq(0).prop("tagName") == "IFRAME"){ var shift = 0} else { var shift = 1};
var nfzCol=$fzCol.index()+shift;
var $mcol=$("table[summary^='NameOfList'] > tbody > tr:not(.ms-viewheadertr) > td:nth-child("+nfzCol+")");
var colleft=$mcol.eq(0).offset().left;
$(window).scroll(function(){
var windowtop = $('body').scrollTop();
if( windowtop > headertop ){
$header.css({"position":"absolute", "top":windowtop});
} else {
$header.css({"position":"static", "top":"0px"});
}
var windowleft = $('body').scrollLeft();
if (windowleft > colleft ){
$mcol.css({"position":"relative", "left": windowleft-colleft});
} else {
$mcol.css({"position":"static", "left":"0px"});
}
});
}

Disable Specific Keys in IE 6

I need to disable specific keys (Ctrl and Backspace) in Internet Explorer 6. Is there a registry hack to do this. It has to be IE6. Thanks.
Long Edit:
#apandit: Whoops. I need to more specific about the backspace thing. When I say disable backspace, I mean disable the ability for Backspace to mimic the Back browser button. In IE, pressing Backspace when the focus is not in a text entry field is equivalent to pressing Back (browsing to the previous page).
As for the Ctrl key. There are some pages which have links which create new IE windows. I have the popup blocker turned on, which block this. But, Ctrl clicking result in the new window being launched.
This is for a kiosk application, which is currently a web based application. Clients do not have the funds at this time to make their site kiosk friendly. Things like URL filtering and disabling the URL entry field is already done.
Thanks.
For what purpose do you need this? Because disabling the backspace would be hell for typing urls or emails, etc.
We could recommend other workarounds if we knew the problem better.
EDIT 1:
This website seems to have some information as to how it's done. I can't verify it currently, but I'll look into it:
http://www.ozzu.com/programming-forum/disable-key-and-back-t44867.html
Edit 2:
This website has some key codes:
http://www.advscheduler.com/docs/manual/type_sendkeys.html
It seems BACKSPACE is 08.
EDIT 3:
Found some more code for blocking, check this out:
<script type="text/javascript">var sType = "keypress";</script>
<!--[if IE]>
<script type="text/javascript">sType = "keydown";</script>
<![endif]-->
<script type="text/javascript">
fIntercept = function(e) {
// alert(e.keyCode);
e = e || event.e;
if (e.keyCode == 116) {
// When F5 is pressed
fCancel(e);
} else if (e.ctrlKey && (e.keyCode == 0 || e.keyCode == 82)) {
// When ctrl is pressed with R
fCancel(e);
}
};
fCancel = function(e) {
if (e.preventDefault) {
e.stopPropagation();
e.preventDefault();
} else {
e.keyCode = 0;
e.returnValue = false;
e.cancelBubble = true;
}
return false;
};
fAddEvent = function(obj, type, fn) {
if (obj.addEventListener) {
obj.addEventListener(type, fn, false);
} else {
obj['e'+type+fn] = fn;
obj[type+fn] = function() {
obj['e'+type+fn](window.event);
}
obj.attachEvent('on'+type, obj[type+fn]);
}
};
fAddEvent(document, sType, fIntercept);
</script>
Ok, now you should have all you need to do it. To disable backspace, the keycode is 08. You can probably just use the code I posted with slight modifications only... :\
Try it out and see if it's what you needed. (I hope you know how to use Javascript.)
You can't do it from a web page. One of the main purposes of a web browser is to protect users from the internet. They define a very specific set of things that web sites can do, and disabling buttons isn't in the list.
On the other hand, if you're a network admin and just want to mess with your users, you might be able to do it via some desktop software. But I wouldn't hold my breath.
I'm using this jQuery solution (tested on ie6 and firefox 3.6):
$(document).keydown(function(e) {
var tag = e.target.tagName;
var ro = e.target.readOnly;
var type = e.target.type;
var tags = {
INPUT : '',
TEXTAREA : ''
};
if (e.keyCode == 8) {// backspace
if (!(tag in tags && !ro && /text/.test(type))) {
e.stopPropagation();
e.preventDefault();
}
}
});
hope it helps someone

Resources