Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well, there are a couple of things to note.</p> <p>First of all, <code>HideSelection</code> doesn't hide or unhide the selection. It specifies, whether the selection will be hidden (or not) when the control loses focus. So it's pointless to change it when the <code>TextBox</code> becomes focus'ed.</p> <p>What you're doing in <code>LostFocus</code> is default, btw. Why there is an exception in <code>GotFocus</code> is burried within Windows API, I guess. Maybe some undefined state when <code>HideSelection</code> checks inside a focus changed event whether the TextBox has focus or not or tries to hide the selection which is not shown. Edit: It's not the Windows API on the first hand, but the framework. It tries to "recreate the handle" in the setter of HideSelection <em>if it is changed</em> (don't know why yet - would have to analyse sources) and seems to fail (don't know why either). Edit2: Finally there's some problem in Win32 DestroyWindow - which leads to skipping the creation of the new window. Maybe because the old one is "in use" in the focus change events?</p> <p>Interestingly, as soon as the exception occurs (for me), the <code>LostFocus</code> event is fired, immediately followed by <code>GotFocus</code> which throws another exception a.s.o. thus blocking the GUI. Both assignments to <code>HideSelection</code> throw the exception.</p> <p>Also, when you click the <code>TextBox</code>, any selection is deselected automatically. This, however, is not the <em>reason</em> for the problem, because the exception is thrown if you change focus by pressing Tab (whose normal behaviour is to restore focus). But it may be related (state problems).</p> <p>If you actually want to restore the selection, you could do it like this:</p> <pre><code>int selStart; int selLen; void textBox1_LostFocus(object sender, EventArgs e) { selStart = textBox1.SelectionStart; selLen = textBox1.SelectionLength; } void textBox1_GotFocus(object sender, EventArgs e) { BeginInvoke((Action)(() =&gt; { textBox1.SelectionStart = selStart; textBox1.SelectionLength = selLen; })); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload