Click here to Skip to main content
15,888,803 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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:

C#
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..
Posted
Comments
Sergey Alexandrovich Kryukov 31-Jul-13 18:42pm    
Bad idea in general. You are trying to put inherently heterogeneous data in array of strings. It's way to bad to work with strings representing data instead of data itself. For example, the data of the NumericUpDown is integer, radio button is selection of element from a set, so it can be enumeration, etc.

If you explained the ultimate goals of the project and other detail, you could get a chance to receive a better advice, but as you formulated it, is simply doesn't make much sense...
—SA

1 solution

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

... 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:
C#
txtEntries.Text = txtEntries.Text + participantA[0] + " - " + participantA[1]+ " - " + participantA[2] + Environment.NewLine;


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:
C#
string oldText = txtEntries.Text;
txtEntries.Text = oldText.Substring(0, oldText.LastIndexOf('\n')); 


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!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900