如何从另一个方法调用方法?

时间:2013-03-06 11:30:30

标签: c# icsharpcode

我正在编写代码编辑器,我想将字符串 line 调用到另一个返回void的方法中的keyargs事件中。

当我输入enter键时输出应该出现,然后ComboBox中的selected-list应该附加到RichTextBox中保存的文本。

现在要实现这一点,我想问你,如何称呼这种方法:

void Parse()
    {
        String inputLanguage =

          "using System;\n" + "\n" +
          "public class Stuff : Form { \n" +
          "  public static void Main(String args) {\n" +
          "\n" + "\n" +
        "  }\n" +
        "}\n";

        // Foreach line in input,
        // identify key words and format them when adding to the rich text box.
        Regex r = new Regex("\\n");
        String[] lines = r.Split(inputLanguage);
        foreach (string l in lines)
        {
            ParseLine(l);
        }
    }
void ParseLine(string line)
{
    Regex r = new Regex("([ \\t{}();])");
    String[] tokens = r.Split(line);

    foreach (string token in tokens)
    {

        // Set the token's default color and font.
        rtb.SelectionColor = Color.Black;
        rtb.SelectionFont = new Font("Courier New", 10, FontStyle.Regular);

        // Check for a comment.
        if (token == "//" || token.StartsWith("//"))
        {
            // Find the start of the comment and then extract the whole comment.
            int index = line.IndexOf("//");

            rtb.SelectedText = comment;
            break;
        }

        // Check whether the token is a keyword. 
        var keywordsDef = new KeyWord();
        String[] keywords = keywordsDef.keywords;

        for (int i = 0; i < keywords.Length; i++)
        {
            if (keywords[i] == token)
            {
                // Apply alternative color and font to highlight keyword.
                HighlighType.keywordsType(rtb);
                break;
            }
        }
        rtb.SelectedText = token;
    }
    rtb.SelectedText = "\n";
}

来自这个:

void lb_KeyDown(object sender, KeyEventArgs e)
    {

        if (e.KeyCode == Keys.Escape)
        {
            lb.Visible = false;
            lb.Items.Clear();
        }

        if (e.KeyCode == Keys.Enter)
        {
            //ParseLine(string line);
            Parse();

            string comment = line.Substring(index, line.Length - index);

            rtb.SelectedText = comment + " " + lb.SelectedIndex.ToString();
        }
    }

我真的需要帮助。非常感谢提前!

2 个答案:

答案 0 :(得分:1)

您传递的参数错误。调用方法时无法传递类型。注释行应为

ParseLine(line);

必须在line之上的某处声明变量ParseLine。它包含的内容取决于您,但可能您想要设置

string line = lb.Text;

所以你的代码可以这样读:

void lb_KeyDown(object sender, KeyEventArgs e)
{

    if (e.KeyCode == Keys.Escape)
    {
        lb.Visible = false;
        lb.Items.Clear();
    }

    if (e.KeyCode == Keys.Enter)
    {
        string line = lb.Text;
        ParseLine(line);
        //Parse();

        string comment = line.Substring(index, line.Length - index);
        rtb.SelectionColor = Color.Green;
        rtb.SelectionFont = new Font("Courier New", 10, FontStyle.Italic);
        rtb.SelectedText = comment + " " + lb.SelectedIndex.ToString();
    }
}

答案 1 :(得分:0)

调用函数不是问题,但是您需要某种方法在您使用的任何编辑器中检索当前行。一旦你检索到它,就可以在它上面调用ParseLine,但是在你拥有它之前你没有任何工作要做。

相关问题