如何将数组中的内容打印到文本框?

问题描述:

我最近开始使用C#,我正在努力开发一个我一直想尝试的小项目。



我正在尝试做什么是从 txtName (文本框), numAmount (NumericUpDown)和 radA / radB (Radiobuttons)中获取数据并将它们存储在一个数组中,以便我可以将它们打印到 txtEntries (多行文本框)中,然后撤消最后添加的行..



示例:



姓名:[0]

金额:[1]

选择:[2]



[btnAdd] - > make数组包含[0] [1] [2]并将其打印到txtEntries(文本框)

[btnUndo] - >删除最后添加的行[0] [1] [2]



这是现在的截图:http://s23.postimg.org/da2yi766j/image.jpg



这是我到目前为止的代码:



I recently started with C# and I'm trying to work on a small project I've always wanted to try.

What I'm trying to do is grab the data from txtName (Textbox), numAmount (NumericUpDown) and radA/radB (Radiobuttons) and store them in an array so I can print them into txtEntries (Multiline Textbox) and afterwards undo the last added line..

Example:

Name: [0]
Amount: [1]
Choice: [2]

[btnAdd] --> make array that holds [0][1][2] and prints it into txtEntries (textbox)
[btnUndo] --> remove the last added line that holds [0][1][2]

Here's a screenshot of what it looks like now: http://s23.postimg.org/da2yi766j/image.jpg.

Here's the code I've got so far:

private void btnAdd_Click(object sender, EventArgs e)
        {
            string name = txtName.Text;
            string amount = numAmount.Text;
            string radioA = radA.Text;
            string radioB = radB.Text;
            string entries = txtEntries.Text;

            // Save input to array (when chosen A)
            string[] participantA = new string[3];
            participantA[0] = name;
            participantA[1] = amount;
            participantA[2] = radioA;

            // Save input to array (when chosen B)
            string[] participantB = new string[3];
            participantB[0] = name;
            participantB[1] = amount;
            participantB[2] = radioB;

            // Check which bet is made (A or B)
            if (radA.Checked == true)
            {
                txtEntries.Text = txtEntries.Text + participantA + Environment.NewLine;
             /* ParticipantA should show me everything in the Array but it doesn't.. */
                txtName.Text = string.Empty;
                numAmount.Text = "1";
                txtName.Focus();
            }
            else if (radB.Checked == true)
            { 
                txtEntries.Text = txtEntries.Text + name + " - " + amount + " - " + radioB + Environment.NewLine;
             /* Instead of putting the arrayname I entered the items manually.. */
                txtName.Text = string.Empty;
                numAmount.Text = "1";
                txtName.Focus();
            }            
        }





我知道这看起来很复杂,但由于我是初学者,我正在寻找一种简单的方法来制作一个撤消按钮,它可以消除最后添加的行。我很确定这需要用数组完成(我认为参差不齐的数组)但是我不知道如何开始..



I know this seems complicated, but since I'm a beginner in this, I'm looking for an easy way to make an undo button that'll whipe away the last added line. I'm pretty sure this needs to be done with arrays (and I think jagged arrays) but I'm not sure how to start..

txtEntries.Text = txtEntries.Text + participantA + Environment.NewLine;



...此行无效,因为您正在尝试连接字符串数组(participantA)直接进入字符串属性(txtEntries.Text)。它们是根本不同的对象,所以你不能这样做。



你必须直接访问participantA的每个元素,所以它看起来像这样: />


... This line isn't working because you are trying to concatenate a string array (participantA) directly into a string property (txtEntries.Text). They are fundamentally different objects, so you can't do this.

You have to access each element of participantA directly, so it would look something like this:

txtEntries.Text = txtEntries.Text + participantA[0] + " - " + participantA[1]+ " - " + participantA[2] + Environment.NewLine;





对于撤消按钮,由于您要为每个partipant添加一个新行,您可以搜索最后一个换行并在此之前采取一切措施。所以在你的撤销功能中,把它放在:



As for your undo button, since you are adding each partipant with a new line, you can search for the last newline and take everything before that. So in your undo function, put this:

string oldText = txtEntries.Text;
txtEntries.Text = oldText.Substring(0, oldText.LastIndexOf('\n')); 





Environment.NewLine实际上给你\\\\ n(这是一个回车和一个换行),这是为什么我使用LastIndexOf搜索最后一个换行符。我使用substring来获取索引0和最后一行换行之间的所有内容。



希望这会有所帮助!



Environment.NewLine actually gives you \r\n (which is a carriage return and a new line feed), which is why I use LastIndexOf to search for the last line feed. I used substring to take everything between index 0 and the last line feed.

Hope this helps!