如何在TextBox中以某种方式显示字符串数组?

时间:2012-02-28 21:22:14

标签: c# winforms textbox formatting

我正在进行机器翻译的毕业设计,将任何语言翻译成英语。

我的软件接受源语言(SL)中的字符串,然后在每个源语言单词下面显示根据其概率排序的所有含义......看起来像这样

Word1      Word2     Word3
hit        bell      man
multiply             leg

问题是我必须显示第一个单词的含义然后我必须回到第一行来显示第二个单词的含义,依此类推......在相同的TextBox中! /强>

c#中有没有办法可以回到第一行并在现有单词旁边写一下?

1 个答案:

答案 0 :(得分:1)

您可以按TextBox.SelectionStartTextBox.SelectionLength属性控制光标位置(和选择)。

示例是否要将光标移动到第3个字符集SelectionStart = 2SelectionLength = 0之前。

所以,作为 - 假设Windows窗体应用程序 - 解决您的问题

public class TextBoxEx : TextBox
{
    public TextBoxEx()
    { }

    public void GoTo(int line, int column)
    {
        if (line < 1 || column < 1 || this.Lines.Length < line)
            return;

        this.SelectionStart = this.GetFirstCharIndexFromLine(line - 1) + column - 1;
        this.SelectionLength = 0;
    }

    public int CurrentColumn
    {
        get { return this.SelectionStart - this.GetFirstCharIndexOfCurrentLine() + 1; }
    }

    public int CurrentLine
    {
        get { return this.GetLineFromCharIndex(this.SelectionStart) + 1; }
    }
}

OR

只需将此类添加到您的项目中,

public static class Extentions
{
    public static void GoTo ( this TextBox Key , int Line , int Character )
    {
        if ( Line < 1 || Character < 1 || Key . Lines . Length < Line )
            return;

        Key . SelectionStart = Key . GetFirstCharIndexFromLine ( Line - 1 ) + Character - 1;
        Key . SelectionLength = 0;
        Key . Focus ( );
    }
} 

将此类添加到项目后,您可以轻松地通过

导航TextBox
TextBox . GoTo ( 1 , 1 ); // Navigate to the 1st line and the 1st character :)

希望得到这个帮助。