Jump to content

Paste listBox content to array

- - - - -

  • Please log in to reply
4 replies to this topic

#1
Forex91

Forex91

    Newbie

  • Members
  • Pip
  • 4 posts
Hello guys,
I just started to "work" with C#, so please be clement :D

I think this is a really easy task to the most of you, but I got a problem with this Method(I hope this is called "method"):

listBox1.Items.CopyTo(names, 1);

It don't pastes the content of the listbox into the array "names" and I don't know why.
The debugger just says that no values will be assigned to the field "names", it always has it's default value "zero".
(Thats not the exact message, I'm from germany so I got the german version of Visual C# ;) )

Here is the complete code:

namespace WindowsFormsApplication1

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }


        string name;

        int max, rand;

        string[] names;

        private void bttSub_Click(object sender, EventArgs e)

        {

            name = txtName.Text;

            listBox1.Items.Add(name);

        }


        private void btt_Clear_Click(object sender, EventArgs e)

        {

            listBox1.Items.Clear();

        }


        private void bttRand_Click(object sender, EventArgs e)

        {

            max = listBox1.Items.Count + 1;

            Random random = new Random();

            rand = random.Next(1, max);

            listBox1.Items.CopyTo(names, 1);


            lblRand.Text = Convert.ToString(names[rand]);

        }

    }

}

Sry if there are any misspellings, I do my best to be understandable^^

Greets,
Forex

#2
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 242 posts
You never assign a value to names, so it has nothing to copy it into.
private void bttRand_Click(object sender, EventArgs e) {

    max = listBox1.Items.Count + 1;

    Random random = new Random();

    rand = random.Next(1, max);

    names = new String[rand+1];

    listBox1.Items.CopyTo(names, 1);  // ****

**** C# arrays start at zero, so you are skipping the first element of the array and starting your copy at the second element (it's also why I added 1 to rand in the line above, to account for the skipped element).

Remember, it's CopyTo, not Create :)

#3
Forex91

Forex91

    Newbie

  • Members
  • Pip
  • 4 posts
Hey, thanks for that.
So "CopyTo" can only Copy Data to fields which already exists, and don't create fields to copy it in?

I copied your Code-Excerpt, but I get an error.
Posted Image

I got it ;)

max = listBox1.Items.Count + 1;

            Random random = new Random();

            rand = random.Next(1, max);

            names = new String[max];

            listBox1.Items.CopyTo(names, 1);

Edit:
Got another question:
After clicking on "Submit" I always have to click on the textbox again to type in. Is there a possibilty to avoid this? So that I can click submit and without clikcing on the textbox write the next name?

And how can I do it, that I only have to press the Enter-key on my keyboard to "submit"?

Edited by Forex91, 08 October 2010 - 07:10 AM.


#4
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 242 posts
Yes, you can do both. In your submit handler, add textName.Focus() to set the focus to your textbox.

To set the button to also be the enter key you have to set the forms AcceptButton property to your button. This can be done is the VS designer easily :) There is also a cancel button you can tie to the "esc" key.

#5
Forex91

Forex91

    Newbie

  • Members
  • Pip
  • 4 posts
Thank you ;)
Works fine




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users