Question: I'm absolute beginner when it comes to *mono for android*.

I've used following code snippet to create 50 buttons programmatically:

```
for(int i=0;i<50;i++)
            {
            //code to calculate x and y position
                btn=new Button(this);
                //btn.SetBackgroundColor(Android.Resource.Color.);
                btn.SetTextSize(Android.Util.ComplexUnitType.Sp,8);
                btn.Text="Scrip "+i+"\n"+"CMP "+i+"\n"+"%Chg "+i;
                lp = new RelativeLayout.LayoutParams(new ViewGroup.MarginLayoutParams((width+30)/5, (height-10)/10));
                btn.LayoutParameters=lp;
                lp.SetMargins(leftMargin,topMargin, 0, 0);
                main.AddView(btn);
            }

            String str="";
            btn.Click += (sender, e) => 
            {
                str=btn.Text;
                Toast.MakeText(this, "Selected="+str,ToastLength.Short).Show();
                Console.WriteLine("Selected="+str);
            };

```

But **one big problem** with this code is at the end of the loop, `btn` object has reference of the last button **only**.

So if any button other than last button is pressed, `button click` event is not fired.
How to resolve it?
Ideally, it should return the text of the clicked button.

Also, referring to below screenshot, the default button style doesn't look good here. So I want to change it to exact rectangle and not rounded-rectangel(the default one).
Any idea on this?

***As I'm very new to this, any help will be appreciated !!***

EDIT
----

Asa result of your help, I'm able to create and refer all buttons appropriately.

***But how to set their style as exact rectangle??***

![screen](https://i.imgur.com/EHUVq.png)

Answer: 
Move `btn.Click += (sender, e)` subscription inside for loop.

Even better - create one named method instead of creating many anonymous. E.g. Button\_Click and subscribe to it:

```
btn = new Button(this);
btn.Click = Button_Click;

```

Inside that method you can cast sender object to Button and know which button was clicked.

UPDATE: here is complete code

```
const int rowsCount = 10;
const int columnsCount = 5;
int buttonsCount = rowsCount * columnsCount;

for (int i = 0; i < buttonsCount; i++)            
    AddButton();

```

I prefer not to use magic numbers in code :)

```
private void AddButton()
{
    Button button = new Button(this);
    button.Click += Button_Click;
    // add text and other properties
    main.AddView(button);
}

private void Button_Click(object sender, EventArgs e)
{
    Button button = (sender as Button);
    // use clicked button e.g. Console.WriteLine("Selected = {0}", button.Text);
}

```
Your for loop operates on a button and creates a new instance of it every time, but your click event is only added once (it is outside your for loop). Therefore it will only be added to the last instance of Button that is put into btn (the last button from the for loop). 

You need to either create each button separately (putting them in a List say) and then outside the for loop you will have a reference to each that you can add the click event to each button individually. Or (**the much better way**) add the click event inside the for loop, so that each button you create adheres to it. Bear in mind that since the event will then be reached by an arbitrary button(one of your 50) that it will be best to use the sender parameter to determine its text value. 
i.e.:

```
        btn.Click += (sender, e) => 
        {
            Button b = sender as Button;
            if ( b == null ) return;
            String str;
            str=b.Text;
            Toast.MakeText(this, "Selected="+str,ToastLength.Short).Show();
            Console.WriteLine("Selected="+str);
        };

```