void selectionChanged(Object sender, EventArgs e) { if (IsOpen) { var needClose = false; if (!_tb.Selection.IsEmpty) { needClose = true; } else if (!Fragment.Contains(_tb.Selection.Start)) { if (_tb.Selection.Start.iLine == Fragment.End.iLine && _tb.Selection.Start.iChar == Fragment.End.iChar + 1) { //user press key at end of fragment var c = _tb.Selection.CharBeforeStart; if (!Regex.IsMatch(c.ToString(), SearchPattern)) { needClose = true; } } else { needClose = true; } } if (needClose) { Close(); } } }