Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Guys simply what I want is this.Get a input from a textbox and press OK button and the input stores in the array[0] element.And then user input another number and press OK button and input saves in array[1] and so on.How can I implement this?So far from this code I can input from text box but storing is a problem.
Example= If my first input is 12 and second input is 45,when i see the array its 12 and 1245..how to seperate it? Any ideas please :D Thanks in advance :)

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Matrix
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private const int MAX_ITEMS = 10;
        private int CurrentIndex = 0;
        private double[] numArray = new double[MAX_ITEMS];
        private int index = 0;


        private void button1_Click(object sender, EventArgs e)
        {
            {
               if (this.index < 10)
                {
                    numArray[this.index] = int.Parse(textBox1.Text);
                    this.index++;
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            for(int i=0;i<10;i++){

            listBox1.Items.Add(numArray[i]); //show array in a listbox

        }
    }
    }
}
Posted
Comments
Sergey Alexandrovich Kryukov 1-Mar-13 13:48pm    
This code is impossible gibberish. It's clearly that you have no clue how to write code in principle. I would advise to learn some programming first, and not with UI, but doing simple exercises with console-only applications. Anyway, what is your delimiters between numbers? Remove hard-coded 10 from everywhere, use you explicitly defined constant (why did you define it if you are not using it?!). Remove the parsing in a loop... no, it looks useless... even if I explain, I cannot hope you can understand... Improve your language and programming knowledge, then come back...
—SA
Libertyofstatue 1-Mar-13 13:53pm    
Thank you very much for your honest opinion.I admit i am a newbie and I have a lot to learn.Can you please suggest some good sites to learn C#.And please if you like,give a solution to my problem since this is an urgent matter :( thanks again :)
Sergey Alexandrovich Kryukov 1-Mar-13 15:06pm    
I know only MSDN:
http://msdn.microsoft.com/en-us/vstudio/hh341490.aspx

Thank you for understanding.
—SA
Sergey Alexandrovich Kryukov 1-Mar-13 15:08pm    
Solution? First, you should avoid your list of integers in text box, should design reasonable UI.
But, instead of cycle, you need to use string.Split(....) (depends on what delimiters you use).
And then cycle "foreach" in by the array of strings you got...
—SA
Libertyofstatue 1-Mar-13 20:12pm    
Thank you.. and I will follow your advice :) ...since this is an emergency i needed a quick quide and thanks a lot again

1 solution

Here is your code with a critique which includes some of the comments made by others above. Note that where I've commented out a line of code and replaced it with an alternative suggestion I would normally just edit or delete the original
C#
private const int MAX_ITEMS = 10;          // This is a good idea but you don't use it everywhere you could
    //private int CurrentIndex = 0;           // This is never used so remove it - stay tidy!
    private double[] numArray = new double[MAX_ITEMS];  // You've declared this array as a double but only ever use ints in it
    private int index = 0;

    private void button1_Click(object sender, EventArgs e)
    {
        //{ These extra braces aren't necessary and could cause confusion
            //if (this.index <10) - Avoid "magic" numbers - make it obvious what you are checking e.g.
            if (this.index < numArray.GetUpperBound(0)) // or you could have reused MAX_ITEMS here
            {
                // You should check that the text box actually contains a number rather than trusting the user
                // From your question you appear to enter the numbers one at a time so I haven't put anything
                // here about splitting strings as per SA's comment but you may need to consider that
                double dnum;
                if (double.TryParse(textBox1.Text, out dnum))
                {
                    numArray[this.index++] = dnum;  // Note that the index is incremented immediately after it is used
                    textBox1.Text = "";   // See comments from PhantomUpvoter - this fixes the problem listed in your question
                }
            }
        //} See comment on extra braces above
    }

    private void button2_Click(object sender, EventArgs e)
    {
        //for (int i = 0; i < 10; i++)
        for(int i = 0; i <= numArray.GetUpperBound(0); i++) // As before - no "magic" numbers or reuse MAX_ITEMS
        {
            // You should really only display stuff that has been entered
            if(i < index)
                listBox1.Items.Add(numArray[i]); //show array in a listbox
            // This bit would be easier if you use List<> or one of the other collections
            // Then you could get rid of the high level variable int index
        }
    }
}

In terms of sites to learn C# Sergey gave an excellent suggestion which I'm only repeating here for ease of linking http://msdn.microsoft.com/en-us/vstudio/hh341490.aspx[^]
There are many others but be warned they can be of varying quality... there are sites that come up first in Google searches that aren't necessarily the best.
Good luck and keep trying!
 
Share this answer
 
Comments
lewax00 1-Mar-13 17:33pm    
In button2_Click, the loop should be "for(int i = 0; i < index; i++)", since you ignore all higher values anyways, then you don't need the if statement. Otherwise, looks good.
CHill60 1-Mar-13 19:39pm    
Good spot! Thank You. (hanging head in shame) I'd use Improve solution but it's probably better for the OP to see *me* take critique as well! :-)
Libertyofstatue 1-Mar-13 20:16pm    
Thank you very much Chill60 :). this works fine with the above modification.Thanks for the references to the sites as well.Cheers :)
CHill60 2-Mar-13 19:40pm    
No problem - but I do have to point out that it was Sergey A K and PhantomUpvoter that provided the link and the solution respectively ... I just typed it out in the solution :-)
markqui 20-Jan-15 11:21am    
what if i want to store string in an array from textbox? same structure?

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