gpt4 book ai didi

c# - C# 中的自动 HTML 标签补全

转载 作者:行者123 更新时间:2023-12-03 23:47:09 26 4
gpt4 key购买 nike

我一直在尝试在 Winforms Richtextbox 中实现自动 HTML 标记功能。我是 C# 的初学者,我需要一个引用,所以我遇到了 this article on CodeProject .使用此给定代码:

    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;
}
}
}
}

问题:

实现工作正常,唯一的问题是当你输入 <html> 时(或任何其他关键字),完成变为 </html> , 但它向下滚动到 TextBox 的末尾 :

Demo of Auto tags

有谁知道如何解决这个问题?

最佳答案

ScrollToCaret()方法,RichTextBox 将控件的内容滚动到当前插入符号位置。

关于c# - C# 中的自动 HTML 标签补全,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61997383/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com