110,534
社区成员
发帖
与我相关
我的任务
分享
string[] snippets = { "if(^)\n{\n}", "if(^)\n{\n}\nelse\n{\n}", "for(^;;)\n{\n}", "while(^)\n{\n}", "do${\n^}while();", "switch(^)\n{\n\tcase : break;\n}" };
private void BuildAutocompleteMenu()
{
var items = new List<AutocompleteItem>();
foreach (var item in snippets)
items.Add(new SnippetAutocompleteItem(item) { ImageIndex = 1 });
//设置为自动完成源
autocompleteMenu1.SetAutocompleteItems(items);
}
internal class EmailSnippet : AutocompleteItem
{
public EmailSnippet(string email): base(email)
{
ImageIndex = 0;
ToolTipTitle = "Insert email:";
ToolTipText = email;
}
public override CompareResult Compare(string fragmentText)
{
if (fragmentText == Text)
return CompareResult.VisibleAndSelected;
if (fragmentText.Contains("@"))
return CompareResult.Visible;
return CompareResult.Hidden;
}
}
autocompleteMenu1.SetAutocompleteItems(new DynamicCollection(tb));
....
internal class DynamicCollection : IEnumerable<AutocompleteItem>
{
public IEnumerator<AutocompleteItem> GetEnumerator()
{
return BuildList().GetEnumerator();
}
private IEnumerable<AutocompleteItem> BuildList()
{
//找到文本中所有单词
var words = new Dictionary<string, string>();
foreach (Match m in Regex.Matches(tb.Text, @"\b\w+\b"))
words[m.Value] = m.Value;
//返回自动完成项
foreach(var word in words.Keys)
yield return new AutocompleteItem(word);
}
}
public interface ITextBoxWrapper
{
Control TargetControl { get; }
string Text { get; }
string SelectedText { get; set; }
int SelectionLength { get; set; }
int SelectionStart { get; set; }
Point GetPositionFromCharIndex(int pos);
event EventHandler LostFocus;
event ScrollEventHandler Scroll;
event KeyEventHandler KeyDown;
event MouseEventHandler MouseDown;
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//把myControl1附加到autocompleteMenu1
autocompleteMenu1.TargetControlWrapper = new MyControlWrapper(myControl1);
}
}
internal class MyControlWrapper : ITextBoxWrapper
{
private MyControl tb;
public MyControlWrapper(MyControl tb)
{
this.tb = tb;
}
//在这里实现ITextBoxWrapper
//(略)
}
using System;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
namespace AutocompleteMenuNS
{
/// <summary>
/// Wrapper over the control like TextBox.
/// </summary>
public class TextBoxWrapper : ITextBoxWrapper
{
private Control target;
private PropertyInfo selectionStart;
private PropertyInfo selectionLength;
private PropertyInfo selectedText;
private MethodInfo getPositionFromCharIndex;
private event ScrollEventHandler RTBScroll;
private TextBoxWrapper(Control targetControl)
{
this.target = targetControl;
Init();
}
protected virtual void Init()
{
var t = target.GetType();
selectedText = t.GetProperty("SelectedText");
selectionLength = t.GetProperty("SelectionLength");
selectionStart = t.GetProperty("SelectionStart");
getPositionFromCharIndex = t.GetMethod("GetPositionFromCharIndex") ?? t.GetMethod("PositionToPoint");
if (target is RichTextBox)
(target as RichTextBox).VScroll += new EventHandler(TextBoxWrapper_VScroll);
}
void TextBoxWrapper_VScroll(object sender, EventArgs e)
{
if (RTBScroll != null)
RTBScroll(sender, new ScrollEventArgs(ScrollEventType.EndScroll, 0, 1));
}
public static TextBoxWrapper Create(Control targetControl)
{
var result = new TextBoxWrapper(targetControl);
if (result.selectedText == null || result.selectionLength == null || result.selectionStart == null ||
result.getPositionFromCharIndex == null)
return null;
return result;
}
public virtual string Text
{
get { return target.Text; }
set { target.Text = value; }
}
public virtual string SelectedText
{
get { return (string)selectedText.GetValue(target, null); }
set { selectedText.SetValue(target, value, null); }
}
public virtual int SelectionLength
{
get { return (int)selectionLength.GetValue(target, null); }
set { selectionLength.SetValue(target, value, null); }
}
public virtual int SelectionStart
{
get { return (int)selectionStart.GetValue(target, null); }
set { selectionStart.SetValue(target, value, null); }
}
public virtual Point GetPositionFromCharIndex(int pos)
{
return (Point)getPositionFromCharIndex.Invoke(target, new object[] { pos });
}
public virtual Form FindForm()
{
return target.FindForm();
}
public virtual event EventHandler LostFocus
{
add { target.LostFocus += value; }
remove { target.LostFocus -= value; }
}
public virtual event ScrollEventHandler Scroll
{
add
{
if (target is RichTextBox)
RTBScroll += value;
else
if (target is ScrollableControl) (target as ScrollableControl).Scroll += value;
}
remove
{
if (target is RichTextBox)
RTBScroll -= value;
else
if (target is ScrollableControl) (target as ScrollableControl).Scroll -= value;
}
}
public virtual event KeyEventHandler KeyDown
{
add { target.KeyDown += value; }
remove { target.KeyDown -= value; }
}
public virtual event MouseEventHandler MouseDown
{
add { target.MouseDown += value; }
remove { target.MouseDown -= value; }
}
public virtual Control TargetControl
{
get { return target; }
}
}
}
Public Shared Function Create(ByVal targetControl As Control) As TextBoxWrapper
Dim result = New TextBoxWrapper(targetControl)
'If result.SelectedText Is Nothing OrElse result.SelectionLength = 0 OrElse result.SelectionStart = 0 _
'OrElse result.GetPositionFromCharIndex Is Nothing Then
If result.SelectedText Is Nothing OrElse result.SelectionLength = 0 OrElse result.SelectionStart = 0 Then
Return Nothing
End If
Return result
End Function