char数组的输入键代码是什么?

时间:2017-07-24 12:02:19

标签: c# winforms

我正在尝试制作一个Reddit Formatter工具,只要你有一个只有一个换行符的文本来添加另一个换行符并创建一个新的段落。在StackOverflow中它是相同的,你必须按两次回车键才能开始一个新的段落。它来自:

 Roses are red
 Violets are Blue

 Roses are red

 Violets are Blue

这实际上非常简单,而且我已经设法在下面自己完成这个代码(可能很乱,但它可以正常工作!!)在按下a之后,它暂时将textBox中的'a'字符替换为'e'按钮。

  private void button1_Click(object sender, EventArgs e)
    {
        Char[] textBox1Array = textBox1.Text.ToCharArray();
        for (int i = 0; i < textBox1Array.Length; i++)
        {
            if (textBox1Array[i] == 'a')
            {
                textBox1Array[i] = 'e';
            }
        }
        textBox1.Text = String.Concat(textBox1Array);
    }

真正的问题是:如何使用回车键而不是'a'? HTML代码显然似乎不起作用:

(&#13;)

\r\n

它会抛出另一个错误,因为它不认为它是一个单个字符(字符文字中的字符太多)

4 个答案:

答案 0 :(得分:1)

所有系统上的换行符不一定相同。因此,如果用户在Windows上输入了他的文本,那么除了在Linux下输入文本之外,换行符可能会更少。所以Environment.Newline在这里不起作用。您需要检查多种换行符类型。我建议做以下事情:

private void button1_Click(object sender, EventArgs e)
{
    textBox1.Text = textBox1.Text.Replace("\r\n", "\r")
                                 .Replace("\n\r", "\r")
                                 .Replace("\n", "\r")
                                 .Replace("\r", "\r\n\r\n");        
}

通过这种方式,您将用占位符替换所有(至少我知道的)可能的换行符类型,然后用双行换行符替换该占位符(在本例中为Windows版本)。

答案 1 :(得分:0)

直截了当的回答是:

textBox1Array[i] = '\n';

另一种可能性是使用新行的十进制代码:

textBox1Array[i] = Convert.ToChar(10);

它会自动反馈光标

您实际上可以直接使用该字符串,而无需转换为不灵活的数组。使用反向for循环并使用[ ]运算符访问chars(因为string在内部表示为char数组):

string g = "Roses are red \r\nViolets are Blue";

for (int i = g.Length -1; i >=  0; i--)
{
    if (g[i] == '\n')
    {
        g = g.Insert(i, Environment.NewLine);
        // or as a string you can use your former logic
        g = g.Insert(i, "\r\n");
    }
}

答案 2 :(得分:0)

对我而言,似乎问题是关于什么字符代表输入密钥,所以我会尝试回答。

就我而言,它是'\r'。要确保,请尝试以下代码:

// you need to add textBox1 to try it or just use what's inside the method
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if(e.KeyChar == '\r')
        MessageBox.Show("Test");
}

if行设置断点,运行程序并按Enter,当它在断续线上停止时,在e上添加监视并查看其字段。您将看到e.KeyChar下的字符。

答案 3 :(得分:0)

我的理解是,你想要你的节目 截取一个换行符,它应该开始一个新的 段落(即“一行的结尾,然后一行新行+新行)。

我建议两种方法来实现这一目标, 1 GT;点击一个按钮 2 - ;当用户点击键盘上的“Enter / Return”键时 它应该添加一个新的段落,就像在“MSWord”

中一样

对于这个测试,我放了2个文本框,TextBox1,TextBox2, 一个命令按钮(btnFormat)只适用于TextBox1。

以下是代码:

private void btnFormat_Click(object sender, EventArgs e)
{
    if(String.IsNullOrWhiteSpace(textBox1.Text) == true) return;

    //searches for the new line character
    Int32 i = textBox1.Text.IndexOf("\r\n");
    Int32 j = 0;

    if (i == -1) return; //new line character not found

    String strA = "";
    String strB = "";

    //now pass the value in 'i' to 'j'
    Int32 icnt = 0;

    while(true)
    {
        //j : from where search should begin. Therefore, it is set
        //to a position ahead of last occurence of new line charachter(\r\n)
        //i.e. value in 'i'
        //i : current occurence of new line character

        //scan for the next occurence of new line character from
        //current positon
        i = textBox1.Text.IndexOf("\r\n",j);
        if (i == -1) break;

        //there is a possibility of some space(s) or no character at all
        //between the last position and current position, then in such a 
        //case we will remove that newly found new line characters so that
        //the formatting is uniform

        //all text before new line 
        strA = textBox1.Text.Substring(0, i);
        //all textbox after the new line
        strB = textBox1.Text.Substring(i + 2);

        if (String.IsNullOrWhiteSpace(textBox1.Text.Substring(j, i - j)) == 
        false)
        {
            textBox1.Text = strA + "\r\n\r\n" + strB;
            j = i + 4;
        }
        else
        {
            textBox1.Text = strA + strB;
            //do not change the value of 'j'
        }
    }

    //increment i and now again scan from this position
    //for another new line character        
}

案例2:[输入文本或以前输入的文本时] 当用户点击键盘上的“Enter / Return”键时, 在MSWord中,程序会自动开始一个新段落(即 添加两个换行符(\ r \ n \ r \ n)。

private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == '\r')
    {
        e.KeyChar = '\0';//this will suppress current {Return key}

        Int32 i = textBox2.SelectionStart;//your current cursor position
        String sa = textBox2.Text.Substring(0,i);//text before this pos.
        String sb = textBox2.Text.Substring(i);//text beyond from this pos
        textBox2.Text = sa + ("\r\n\r\n") + sb;
        textBox2.SelectionStart = i + 2;
        //if there is already one or more new line characters, then 
        //this code does not check for that, so it has to be removed
        //manually using 'delete' or 'backspace'
    }
}