如何使用 ICsharpCode.TextEditor 组件自动完成标签?
How to Auto-Complete a tag with ICsharpCode.TextEditor component?
我已将 ICSharpCode.TextEditor.dll 组件添加到我的 C# 项目中的 Visual Studio 2017 参考中。接下来,我想找出一种自动完成标签的方法。我在下面提供了一张 GIF 图片来说明我的意思。
我提供了一个代码,我尝试用组件本身进行调试,但它不起作用。
if (textEditorControl1.Text.Contains("<html>"))
{
textEditorControl1.ActiveTextAreaControl.SelectionManager.SelectedText("</html>");
}
ICsharpCode.TextEditor 组件没有与普通 Textbox.[=14 相同的 属性 =]
if (textBox2.Text.Contains("<html>"))
{
textBox2.SelectedText = "</html>";
}
如果我尝试调试 TextBox 版本,我会无数次收到该标签,直到我收到未处理的 System.WhosebugException.
我找到了您要找的东西 here。唯一的问题是,这是正常 TextBox.
的工作代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace AutoCompleteHTMLTags_CSharp
{
public partial class Simple_Form : Form
{
public Simple_Form()
{
InitializeComponent();
}
public static String EnteredString = "";
public static Boolean Is_LessThanKeyPressed = false;
public static Boolean Is_GreaterThanKeyPressed = false;
public static Boolean Is_AutoCompleteCharacterPressed = false;
public Boolean Is_SpaceBarKeyPressed = false;
public Boolean Is_TagClosedKeyPressed = false;
public String[] tagslist ={
"html",
"head",
"title",
"body",
"h1",
"h2",
"h3",
"h4",
"h5",
"h6",
"b",
"u",
"i",
"sub",
"sup",
"center",
"strike",
"font",
"p",
"style",
"pre",
"marquee",
"ul",
"ol",
"a",
"img",
"table",
"tr",
"th",
"td",
"frameset",
"iframe",
"form",
"input",
"button",
"textarea",
"select",
"div",
"fieldset",
"span",
"strong",
"em",
"big",
"small"
};
public void ProcessAutoCompleteBrackets(String s)
{
int sel = richTextBox1.SelectionStart;
switch (s)
{
case "(":
richTextBox1.Text = richTextBox1.Text.Insert(sel, ")");
richTextBox1.SelectionStart = sel;
Is_AutoCompleteCharacterPressed = true;
break;
case "[":
richTextBox1.Text = richTextBox1.Text.Insert(sel, "]");
richTextBox1.SelectionStart = sel;
Is_AutoCompleteCharacterPressed = true;
break;
case "\"":
Is_AutoCompleteCharacterPressed = true;
richTextBox1.Text = richTextBox1.Text.Insert(sel, "\"");
richTextBox1.SelectionStart = sel;
break;
case "'":
richTextBox1.Text = richTextBox1.Text.Insert(sel, "'");
richTextBox1.SelectionStart = sel;
Is_AutoCompleteCharacterPressed = true;
break;
}
}
private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
String ch = e.KeyChar.ToString();
this.ProcessAutoCompleteBrackets(ch);
if (ch == "<")
{
Is_LessThanKeyPressed = true;
Is_SpaceBarKeyPressed = false;
EnteredString = "";
}
else if (ch == ">")
{
if (!Is_TagClosedKeyPressed)
{
Is_GreaterThanKeyPressed = true;
Is_SpaceBarKeyPressed = false;
int oldsel = richTextBox1.SelectionStart;
for (int i = 0; i < tagslist.Length; i++)
{
if (EnteredString == tagslist[i])
{
richTextBox1.Text = richTextBox1.Text.Insert(oldsel, "</" + tagslist[i] + ">");
richTextBox1.SelectionStart = richTextBox1.SelectionStart + oldsel;
EnteredString = "";
}
}
Is_LessThanKeyPressed = false;
}
else
{
Is_TagClosedKeyPressed = false;
}
}
else
{
if (Is_LessThanKeyPressed)
{
for (char a = 'a'; a <= 'z'; a++)
{
if (a.ToString() == ch)
{
EnteredString += ch;
}
else if (a.ToString().ToUpper() == ch)
{
EnteredString += ch;
}
}
for (int a = 0; a <= 9; a++)
{
if (a.ToString() == ch)
{
EnteredString += ch;
}
}
}
}
// if user itself closes the tag
if (Is_LessThanKeyPressed)
{
if (ch == "/")
{
Is_TagClosedKeyPressed = true;
Is_SpaceBarKeyPressed = true;
EnteredString = "";
}
}
}
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Space:
Is_SpaceBarKeyPressed = true;
if (Is_GreaterThanKeyPressed)
{
Is_GreaterThanKeyPressed = false;
}
Is_LessThanKeyPressed = false;
for (int i = 0; i < tagslist.Length; i++)
{
if(EnteredString==tagslist[i])
{
EnteredString = tagslist[i];
}
}
break;
case Keys.Up:
if (Is_AutoCompleteCharacterPressed == false)
{
EnteredString = "";
Is_AutoCompleteCharacterPressed = false;
}
Is_SpaceBarKeyPressed = false;
break;
case Keys.Down:
if (Is_AutoCompleteCharacterPressed == false)
{
EnteredString = "";
Is_AutoCompleteCharacterPressed = false;
}
Is_SpaceBarKeyPressed = false;
break;
case Keys.Left:
if (Is_AutoCompleteCharacterPressed == false)
{
EnteredString = "";
Is_AutoCompleteCharacterPressed = false;
}
Is_SpaceBarKeyPressed = false;
break;
case Keys.Right:
if (Is_AutoCompleteCharacterPressed == false)
{
EnteredString = "";
Is_AutoCompleteCharacterPressed = false;
}
Is_SpaceBarKeyPressed = false;
break;
case Keys.Enter: EnteredString = "";
Is_SpaceBarKeyPressed = false;
break;
case Keys.Back:
int sel = richTextBox1.SelectionStart;
Point pt = richTextBox1.GetPositionFromCharIndex(sel);
char ch = richTextBox1.GetCharFromPosition(pt);
if (EnteredString.Length > 0)
{
if (ch != '>')
{
EnteredString = EnteredString.Remove(EnteredString.Length - 1);
Is_LessThanKeyPressed = true;
}
}
if (ch == '<')
{
EnteredString = "";
}
break;
}
}
}
}
我已将 ICSharpCode.TextEditor.dll 组件添加到我的 C# 项目中的 Visual Studio 2017 参考中。接下来,我想找出一种自动完成标签的方法。我在下面提供了一张 GIF 图片来说明我的意思。
我提供了一个代码,我尝试用组件本身进行调试,但它不起作用。
if (textEditorControl1.Text.Contains("<html>"))
{
textEditorControl1.ActiveTextAreaControl.SelectionManager.SelectedText("</html>");
}
ICsharpCode.TextEditor 组件没有与普通 Textbox.[=14 相同的 属性 =]
if (textBox2.Text.Contains("<html>"))
{
textBox2.SelectedText = "</html>";
}
如果我尝试调试 TextBox 版本,我会无数次收到该标签,直到我收到未处理的 System.WhosebugException.
我找到了您要找的东西 here。唯一的问题是,这是正常 TextBox.
的工作代码using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace AutoCompleteHTMLTags_CSharp
{
public partial class Simple_Form : Form
{
public Simple_Form()
{
InitializeComponent();
}
public static String EnteredString = "";
public static Boolean Is_LessThanKeyPressed = false;
public static Boolean Is_GreaterThanKeyPressed = false;
public static Boolean Is_AutoCompleteCharacterPressed = false;
public Boolean Is_SpaceBarKeyPressed = false;
public Boolean Is_TagClosedKeyPressed = false;
public String[] tagslist ={
"html",
"head",
"title",
"body",
"h1",
"h2",
"h3",
"h4",
"h5",
"h6",
"b",
"u",
"i",
"sub",
"sup",
"center",
"strike",
"font",
"p",
"style",
"pre",
"marquee",
"ul",
"ol",
"a",
"img",
"table",
"tr",
"th",
"td",
"frameset",
"iframe",
"form",
"input",
"button",
"textarea",
"select",
"div",
"fieldset",
"span",
"strong",
"em",
"big",
"small"
};
public void ProcessAutoCompleteBrackets(String s)
{
int sel = richTextBox1.SelectionStart;
switch (s)
{
case "(":
richTextBox1.Text = richTextBox1.Text.Insert(sel, ")");
richTextBox1.SelectionStart = sel;
Is_AutoCompleteCharacterPressed = true;
break;
case "[":
richTextBox1.Text = richTextBox1.Text.Insert(sel, "]");
richTextBox1.SelectionStart = sel;
Is_AutoCompleteCharacterPressed = true;
break;
case "\"":
Is_AutoCompleteCharacterPressed = true;
richTextBox1.Text = richTextBox1.Text.Insert(sel, "\"");
richTextBox1.SelectionStart = sel;
break;
case "'":
richTextBox1.Text = richTextBox1.Text.Insert(sel, "'");
richTextBox1.SelectionStart = sel;
Is_AutoCompleteCharacterPressed = true;
break;
}
}
private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
String ch = e.KeyChar.ToString();
this.ProcessAutoCompleteBrackets(ch);
if (ch == "<")
{
Is_LessThanKeyPressed = true;
Is_SpaceBarKeyPressed = false;
EnteredString = "";
}
else if (ch == ">")
{
if (!Is_TagClosedKeyPressed)
{
Is_GreaterThanKeyPressed = true;
Is_SpaceBarKeyPressed = false;
int oldsel = richTextBox1.SelectionStart;
for (int i = 0; i < tagslist.Length; i++)
{
if (EnteredString == tagslist[i])
{
richTextBox1.Text = richTextBox1.Text.Insert(oldsel, "</" + tagslist[i] + ">");
richTextBox1.SelectionStart = richTextBox1.SelectionStart + oldsel;
EnteredString = "";
}
}
Is_LessThanKeyPressed = false;
}
else
{
Is_TagClosedKeyPressed = false;
}
}
else
{
if (Is_LessThanKeyPressed)
{
for (char a = 'a'; a <= 'z'; a++)
{
if (a.ToString() == ch)
{
EnteredString += ch;
}
else if (a.ToString().ToUpper() == ch)
{
EnteredString += ch;
}
}
for (int a = 0; a <= 9; a++)
{
if (a.ToString() == ch)
{
EnteredString += ch;
}
}
}
}
// if user itself closes the tag
if (Is_LessThanKeyPressed)
{
if (ch == "/")
{
Is_TagClosedKeyPressed = true;
Is_SpaceBarKeyPressed = true;
EnteredString = "";
}
}
}
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Space:
Is_SpaceBarKeyPressed = true;
if (Is_GreaterThanKeyPressed)
{
Is_GreaterThanKeyPressed = false;
}
Is_LessThanKeyPressed = false;
for (int i = 0; i < tagslist.Length; i++)
{
if(EnteredString==tagslist[i])
{
EnteredString = tagslist[i];
}
}
break;
case Keys.Up:
if (Is_AutoCompleteCharacterPressed == false)
{
EnteredString = "";
Is_AutoCompleteCharacterPressed = false;
}
Is_SpaceBarKeyPressed = false;
break;
case Keys.Down:
if (Is_AutoCompleteCharacterPressed == false)
{
EnteredString = "";
Is_AutoCompleteCharacterPressed = false;
}
Is_SpaceBarKeyPressed = false;
break;
case Keys.Left:
if (Is_AutoCompleteCharacterPressed == false)
{
EnteredString = "";
Is_AutoCompleteCharacterPressed = false;
}
Is_SpaceBarKeyPressed = false;
break;
case Keys.Right:
if (Is_AutoCompleteCharacterPressed == false)
{
EnteredString = "";
Is_AutoCompleteCharacterPressed = false;
}
Is_SpaceBarKeyPressed = false;
break;
case Keys.Enter: EnteredString = "";
Is_SpaceBarKeyPressed = false;
break;
case Keys.Back:
int sel = richTextBox1.SelectionStart;
Point pt = richTextBox1.GetPositionFromCharIndex(sel);
char ch = richTextBox1.GetCharFromPosition(pt);
if (EnteredString.Length > 0)
{
if (ch != '>')
{
EnteredString = EnteredString.Remove(EnteredString.Length - 1);
Is_LessThanKeyPressed = true;
}
}
if (ch == '<')
{
EnteredString = "";
}
break;
}
}
}
}