Intro -
I am an electrical major. Having said that, my school has a pilot program this year that I was randomly selected for, that includes introduction to C# in the program. I am not very familiar with programming at all but I must admit, I have made decent progress in this class using the tutorial textbook.

Problem -
I have been assigned to create a program, that generates the prime numbers 2 - 102 (100 numbers). We have been instructed to use the The Sieve of Eratosthenes. In addition, there must be 100 buttons placed on a form, that will be labled 2, 3, 4, ... 102. When the "Run" button is clicked, it will begin to find primes. When the prime number generator finds a prime, the button with the same label is to be disabled (grayed out). This is the issue I am having the most problem with. I don't even know where to start, somehow you must be able to associate the output of the generator to the button names? Can someone help/point me in the right direction? I know that this involves using arrays but im so lost as to how to do it.... any help is MUCHOOO appreciated. Thanks!

assuming that ur able to do The Sieve of Eratosthenes,
....to hide/grey-out a button this is what you do:
button1.hide();
that will hide the button.....but in order to see if the button has a certain prime number you will have MANY if statments...(approx 102). Each one will do the following...:

if(prim_number.ToString()==button1.text)
{
      button1.hide();
}

if(prim_number.ToString()==button2.text)
{
      button2.hide();
}
//..........

if(prim_number.ToString()==button102.text)
{
      button102.hide();
}

Where prim_number is the (u've guessed it),,,,,prime number!

wouldn't the above sample involve hard coding? I have numbers 2 thru 102 listed, which are not all prime obviously. If you look at http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes , the visual example they have there is almost exactly what I'm trying to do, except instead of X's, im disabling buttons.

is it possible to take for example: the first prime number "2", and have it disable every other button? I have my buttons named (starting at b2) b2, b3, b4, etc.

Or...
if the prime is 11 then b+"11".hide(); [trying to get b11.hide(); ]
can i do that somehow?

Yea i read that... and yes, the if way i posted above is a kinda retarded way of programming

well, what you could do is put the buttons you want to hide that are multiples of 2 inside an if function that says if(prim_num==button2.text).....
but even then you will only be able to do the program from 2-102...not frm 2-n.

but this is wrong::::

if the prime is 11 then b+"11".hide(); [trying to get b11.hide(); ]

because by doing this u are saying that the variable b is a string, and your adding the string "11" to the variable b (which is a string) thus returning a string and not a Button, so that won't work....

read this....http://thedailywtf.com/Articles/Soft_Coding.aspx.....it'll help...helped me!

When you click a button, It should cast an event and when the event it handled (going into your method) you can convert the "object sender" defined in the method's parameter to a "Button". Then this way, you can get the Text property of the button and parse it to be an integer.

After what you can do your mathematical operations to find the prime numbers in the array and hide them.

A good way of doing this is either by iterating over the array of button and finding the right text property with an if and then do hide it and break your for, or simply with using LINQ (vs2008 and later versions).

Hope this helps

Perhaps do it this way:
Make a standard forms application and fill in this code:

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public const int MaxLen = 10;
        public const int ButtonSide = 40;
        // Declare empty 2D button array of 100 buttons
        private Button[,] MyButtons = new Button[MaxLen, MaxLen];

        public Form1()
        {
            InitializeComponent();
            this.Size = new Size(450, 450); //increase size of form a bit to see all the buttons         
            for (int row = 0; row < MaxLen; row++)
            {
                for (int col = 0; col < MaxLen; col++)
                {
                    MyButtons[row, col] = new Button(); //make a button in the array
                    MyButtons[row, col].Height = ButtonSide;
                    MyButtons[row, col].Width = ButtonSide;
                    MyButtons[row, col].Location = new Point(row * ButtonSide + 10, col * ButtonSide + 10);
                    MyButtons[row, col].Name = "b" + row.ToString() + col.ToString();
                    int ButtonNumber = col * MaxLen + row + 2;
                    MyButtons[row, col].Text = ButtonNumber.ToString(); //put text on button
                    this.Controls.Add(MyButtons[row, col]); // add button to form
                }
                
            }
            
        }

    }
}

Now all you have to do is to hide the buttons with a text that is not a prime number. Hope it helps.

Hi,

If you create your buttons with a standard naming convention for can use the Controls.Find function. E.g.

Create Buttons

for (int i = 1; i <= 10; i++)
            {
                Button b = new Button();
                b.Enabled = true;
                b.Left = 5 + ((i - 1) * 100);
                b.Top = 5;
                b.Text = i.ToString();

                b.Name = "btn" + i.ToString();

                this.Controls.Add(b);
            }

This will add 10 buttons to the form with the name 'btn<n>'.

Then your disable button function finds the button from a given number, thus:

private void DisableButton(int num)
        {
            Button b = (Button)this.Controls.Find("btn" + num.ToString(), false)[0];
            b.Enabled = false;
        }

Hope this helps.

got it working. Decided to give the buttons some color too. Thanks guys.

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 - 
namespace Prime_Number_Generator
{
    // Form name & class
    public partial class primesForm : Form
    {
        //Form name - primesForm (Prime number generator)
        public primesForm()
        {
            InitializeComponent();
        }
        //Exit Program Button
        private void exitButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //Run button - starts prime number generator loop
        private void runButton_Click(object sender, EventArgs e)
        {
            int max = 102;

            var maxSqRt = Math.Sqrt(max);
            var eliminated = new System.Collections.BitArray(max + 1);
            // Two is a given prime so we label it as such;
            b2.BackColor = System.Drawing.Color.Red;
            b2.ForeColor = System.Drawing.Color.White;
            // Because two is prime, loop starts at 3
            for (int i = 3; i <= max; i += 2)
            {
                if (!eliminated[i])
                {
                    if (i < maxSqRt)
                    {

                        for (int j = i * i; j <= max; j += 2 * i)
                            eliminated[j] = true;
                    }

                    string vals = "b" + i;
                    //Decorate the buttons associated with prime numbers                    
                    this.Controls.Find(vals, false)[0].Enabled = true;
                    this.Controls.Find(vals, false)[0].BackColor = System.Drawing.Color.Red;
                    this.Controls.Find(vals, false)[0].ForeColor = System.Drawing.Color.White;
                    

                }


            }
        }
    }
}
{
InitializeComponent();
}

I cut and pasted the code that Jimingle posted, as it looked interesting, but got an error
"The name "InitializeComponent" does not exist in the current content".
Am I missing a function?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.