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);
}

```
it's because you are setting the button click event outside the for loop. put it on the inside so it gets assigned to every button, not just the last one.