tags:

views:

100

answers:

6

Hello;-)

I just designed a simple "For Loop" using window form application. I would like that this will be only clickable once & that it will not repeat the same information if I click the button. How could I do that? thanks Here is my code:

        int count;

        for (count = 0; count < 5; count = count + 1)
        {
            listBox1.Items.Add("This is count: " + count);
        }
        const string textEnd = "Done!!!";
        {
            listBox1.Items.Add(textEnd);
        }

==== Additional Info === I've made it this way now. This will only click once, but the button is still enable. I think this is ok:

        int count;

        for (count = 0; count < 5; count++)
        {
            string newItem = "This is count: " + count; 
            if (listBox1.Items.IndexOf(newItem) < 0) 
            { 
                listBox1.Items.Add(newItem); 
            }
         }

        const string textEnd = "Done!!!";
        if (listBox1.Items.IndexOf(textEnd) <0)
        {
            listBox1.Items.Add(textEnd);
        }
+4  A: 
button1.Enabled = false;
Mehrdad Afshari
this is also a great idea. Thanks Mehrdad;-)
tintincute
A: 

On the click event, add button1.Enabled = false

And maybe, after the loop, you may want to add button1.Enabled = true to reenable the button. :)

Ian
hi Ian where shall I put the button1.Enabled=false? would that be on the first line of my code? how about the button1.Enabled=true? after the loop would be after the block of const string textEnd? is that right?
tintincute
+1  A: 

I'm assuming you don't want the same items added to the list multiple times?

Instead of

{
    listBox1.Items.Add("This is count: " + count);
}

You need something like

{
    string newItem = "This is count: " + count;
    if(listBox1.Items.IndexOf(newItem) < 0)
    {
        listBox1.Items.Add(newItem);
    }
}
Binary Worrier
Hi Binary what does this line means:if(listBox1.Items.IndexOf(newItem)<0) ?
tintincute
It's to check if the Item (String) is already in the list...
Peter
ok thanks. I've also seen that when I wrote string textEnd, it gives me suggestions that I can also make it constant. Is there any difference?If I just write string textEnd with const string textEnd?Just encounter this "const" I know I can't change it but what's the purpose?
tintincute
"const": it's suggesting that you put that literal string into a constant variable, this is good practise to get all the "magic numbers" you use into variables so you're not using the same literal value many times in your code. However in this instance, at your stage, you don't want to worry about it, you have larger fish to fry, so go write more code ! ! !
Binary Worrier
A: 

You can just use a simple 'flag' to determine whether the loop has already run.

Create a global variable eg bool HasRun = false;

Then check the state of the flag before you execute the loop eg if HasRun == true

Set HasRun to true when you first run the loop.

Finally you could also disble the button on first run eg button1.Enabled = false;

geoff
hello geoff, hmm sorry I don't understand what "flag" means here. Maybe I haven't learned it yet. Just learned C#;-)
tintincute
A: 

If it's not too many items, you could always clear the listbox before adding the stuff.

listbox1.Items.Clear();
...your adding code...

But the probably best solution is just to disable the button, like Ian wrote.

kaze
where should I add this listBox1.Items.Clear()?
tintincute
As the first line in the method that is populating the listbox, as in your example, before the "int count;" line. I'm assuming that you are trying to avoid a lot of duplicates in the listbox.
kaze
yes kaze I'm avoiding a duplicate.
tintincute
A: 

Best one is what Ian Said however you can also hide the button completely by running either of the folowing commands

button.Hide();

Or

button.Visable = false;
Shahmir Javaid