本帖最后由 小白龙 于 2023-4-9 08:50 编辑
回复 12# Five66
问了一下gpt
这段代码中有以下几个问题:
在定义语言样式字段 _theStyler 时,应该在方法或构造函数中进行初始化,而不是在类体外进行初始化。
在定义语言样式字段 _theStyler 时,缺少逗号。
在 EasySci 方法中使用 _theStyler 时,应该使用 Nakano._theStyler,因为 _theStyler 是 Nakano 类的字段。
在调用 Dispose() 方法之前,应该先隐藏窗口并将其设置为不可见。
下面是修改后的代码:- using System;
- using System.Collections;
- using System.Windows.Forms;
- using EasyScintilla.Stylers;
-
- namespace ExampleProject
- {
- public partial class Form1 : Form
- {
- private readonly EasyScintilla.SimpleEditor editor;//编辑器声明
- private readonly Button theButton;//按钮声明
- public string ThertStr; //返回字符串声明
-
- /// <summary>
- /// Required designer variable.
- /// </summary>
- private readonly System.ComponentModel.IContainer components = null;
-
- /// <summary>
- /// Clean up any resources being used.
- ///重定义Form1释放函数
- /// </summary>
- /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
- protected override void Dispose(bool disposing)
- {
- if (disposing && (components != null))
- {
- components.Dispose();
- }
- base.Dispose(disposing);
- }
-
- //定义Form1关闭事件函数,用于关闭时储存编辑器文本
- private void Form1_Closing(object sender, EventArgs e)
- {
- ThertStr = editor.Text;
- }
-
- //Form1构造函数
- public Form1()
- {
- InitializeComponent();//调用初始化函数
- editor = new EasyScintilla.SimpleEditor();//创建编辑器
- theButton = new Button();//创建按钮
- this.SuspendLayout();
- //
- // editor设置
- //
- editor.Anchor = ((AnchorStyles) (((AnchorStyles.Top | AnchorStyles.Bottom)
- | AnchorStyles.Left)
- | AnchorStyles.Right));
- editor.IndentationGuides = ScintillaNET.IndentView.LookBoth;
- editor.Location = new System.Drawing.Point(12, 12);
- editor.Name = "editor";
- editor.Size = new System.Drawing.Size(854, 603);
- editor.Styler = null;
- editor.TabIndex = 0;
- //
- // 按钮设置
- //
- theButton.Size=new System.Drawing.Size(100, 21);
- theButton.Location=new System.Drawing.Point(12, 621);
- theButton.Name="button1";
- theButton.Text="OK";
- this.Controls.Add(theButton);//添加按钮到Form1
- this.Controls.Add(editor);//添加编辑器到Form1
- this.ResumeLayout(false);
- }
- }
- //========================================
- public class Nakano {
- //语言样式字段
- private static readonly Hashtable _theStyler=new Hashtable() {
- {"C#", new CSharpStyler()},
- {"C# (Dark)", new DarkCSharpStyler()},
- {"Windows Batch", new BatchStyler()},
- {"HTML", new HtmlStyler()},
- {"JSON", new JsonStyler()},
- {"PowerShell", new PowerShellStyler()},
- {"Python", new PythonStyler()},
- {"Ruby", new RubyStyler()},
- {"T-SQL", new SqlStyler()},
- {"Teradata Parallel Transporter", new TptStyler()}
- };
-
- //语言样式属性
- public static Hashtable TheStyler => _theStyler;
-
- public static string EasySci(string code, string codeType)
- {
- var theForm1 = new Form1();
- var theStr = "";
- if (_theStyler.ContainsKey(codeType))
- {
- theForm1.editor.Styler = (IStyler) _theStyler[codeType];
- }
-
- theForm1.editor.Text = code;
-
- theForm1.ShowDialog();
-
- theStr = theForm1.ThertStr;
-
- theForm1.Hide();
- theForm1.Visible = false;
- theForm1.Dispose();
-
- return theStr;
- }
- }
- //========================================
- }
复制代码
|