I'm writing a .NET forms control to edit HTML using MSHTML. I am creating some custom elements and want to make them effectively read-only. I thought I could go about this by focusing on the entire element any time focus entered anywhere in that element but the HtmlElement.Focus() doesn't select the entire element and I don't seem to be able to capture entry of the cursor.
我正在编写一个.NET表单控件来使用MSHTML编辑HTML。我正在创建一些自定义元素,并希望将它们有效地设置为只读。我认为我可以通过专注于整个元素来关注这一点,任何时候焦点输入到该元素的任何地方,但HtmlElement.Focus()不会选择整个元素,我似乎无法捕获光标的条目。
Another option would be to raise an event whenever the text of the element is changed (on KeyDown I expect) but I can't get that event to fire, either. Any ideas about why my expectations about event behavior is wrong or alternate suggestions for implementation?
另一种选择是每当元素的文本被更改时(在我期望的KeyDown上)引发一个事件,但我也无法触发该事件。关于为什么我对事件行为的期望是错误的或者实施的替代建议的任何想法?
2 个解决方案
#1
1
I found that setting the attribute:
我发现设置属性:
contentEditable=false
Resulted in the desired behavior.
导致了所需的行为。
#2
0
In case you are trying to apply readonly behavior to an User Input control, you can try using @readonly attribute of that control. Otherwise you could also add event listeners for appropriate UI events (keydown, mousedown) and prevent their default behavior (return false, or event.returnValue = false). As for custom events dispatch, you can indeed do that. Use event name that is known to IE. And another hint could be: register an onchange event handler and revert value of control to the value of defaultValue (property of any input controls).
如果您尝试将只读行为应用于用户输入控件,则可以尝试使用该控件的@readonly属性。否则,您还可以为适当的UI事件(keydown,mousedown)添加事件侦听器,并防止其默认行为(返回false或event.returnValue = false)。至于自定义事件调度,您确实可以这样做。使用IE已知的事件名称。另一个提示可能是:注册onchange事件处理程序并将控制值恢复为defaultValue的值(任何输入控件的属性)。
Hope some of ideas will help.
希望一些想法会有所帮助。
#1
1
I found that setting the attribute:
我发现设置属性:
contentEditable=false
Resulted in the desired behavior.
导致了所需的行为。
#2
0
In case you are trying to apply readonly behavior to an User Input control, you can try using @readonly attribute of that control. Otherwise you could also add event listeners for appropriate UI events (keydown, mousedown) and prevent their default behavior (return false, or event.returnValue = false). As for custom events dispatch, you can indeed do that. Use event name that is known to IE. And another hint could be: register an onchange event handler and revert value of control to the value of defaultValue (property of any input controls).
如果您尝试将只读行为应用于用户输入控件,则可以尝试使用该控件的@readonly属性。否则,您还可以为适当的UI事件(keydown,mousedown)添加事件侦听器,并防止其默认行为(返回false或event.returnValue = false)。至于自定义事件调度,您确实可以这样做。使用IE已知的事件名称。另一个提示可能是:注册onchange事件处理程序并将控制值恢复为defaultValue的值(任何输入控件的属性)。
Hope some of ideas will help.
希望一些想法会有所帮助。