public partial class Form1 : Form
    {
        int row, col;
        int num = 0;

        public Form1()
        {
             InitializeComponent();
             
        }

 private void generatebutton_Click(object sender, EventArgs e)
        {
            int  rows = Convert.ToInt32(rowBox.Text);
            int cols = Convert.ToInt32(colBox.Text);
          
            for (int i = 2; i <= rows+1; i++)
            {
                for (int j = 4; j <= cols+3; j++)
                {
                    Button button = new Button();
                    button.Location = new Point(j * 100, i * 100);
                     
                    this.Controls.Add(button);
                    num++;     
                    button.Text = Convert.ToString(num);
                    button.Name = Convert.ToString("button" + num);
                    WireUp(button, "Click", "Clickbutton");
                    
                }
                    
 
            }  
        }

        void WireUp(object o, string eventname, string methodname)
        {
            EventInfo ei = o.GetType().GetEvent(eventname);

            MethodInfo mi = this.GetType().GetMethod(methodname, BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);

            Delegate del = Delegate.CreateDelegate(ei.EventHandlerType, this, mi);

            ei.AddEventHandler(o, del);

        }

       
        void Clickbutton(object sender, System.EventArgs e)
        {
            textBox1.Text = Convert.ToString(num);
           
            
        }

now i have to print corresponding number on clicking button

Edited 6 Years Ago by __avd: Added [code] tags.

This might work for your needs... you'll need to add "using System.RegularExpressions;" at the top of your code and this goes into your button click:

Button btn = (Button)(sender);
string btnName = btn.Name.ToString;
string[] btnSplit = Regex.Split(btnName, "button");
string btnNum = btnSplit[0];

btnNum will be the number portion of the button name of the button that called the click event.

Hope this helps :) Please remember to mark solved if your issue is resolved.

Its not even letting me include System.RegularExpression.
Can you provide a alternative.

Its not even letting me include System.RegularExpression.

That's because I'm an idiot and it's supposed to be:

using System.Text.RegularExpressions;

Note to self: double check some stuff before posting on-the-fly :twisted:

Thanks a lot for your help.
Your sol did work
Thanks again....!!

I am new to c#
So can u please make me understand the meaning of code that you provided.
Waiting for your reply

Lusiphur is using a Regex to remove the "button" text from the button name.
Since your buttons have the number in the button text then you can use this instead.

When ever an event fires it passes the object that raised the event in the sender parameter. However, to use it, you need to cast back to the original class type. That is what the first line of Lusiphur's code is doing. Once you have the object you can use any of its properties.
E.g. to get the button text

Button btn = (Button)sender;
textBox1.Text = btn.Text;

Or

textBox1.Text = ((Button)sender).Text;

Another way using the button Name (as Lusiphur did) would be:

Button btn = (Button)sender;
textBox1.Text = btn.Name.Substring(6);

Borrowing Lusiphur's closing line;)
Hope this helps and please remember to mark solved if your issue is resolved.

Edited 6 Years Ago by nick.crane: n/a

Another way using the button Name (as Lusiphur did) would be:

Button btn = (Button)sender;
textBox1.Text = btn.Name.Substring(6);

Borrowing Lusiphur's closing line;)
Hope this helps and please remember to mark solved if your issue is resolved.

Hehe ya, I was tossed up whether to use the regex method (assuming that all buttons contained the string "button" and removing that from the name to get the number) or to use the substring method (assuming all buttons contained the string button so starting at the end of the string and running to the end to get the number).

My only thought here is, doesn't .SubString() require the starting point and the length of the substring? in which case you need to do a bit more math to determine the length from the end of the word button to the end of the name and include that as a 2nd variable (ie: btn.Name.Substring(6,2) for a 2 digit button number).

Great example though :)

My only thought here is, doesn't .SubString() require the starting point and the length of the substring?

No. When only the start is given substring returns all the text from given start index to end of string. A bit link the old VB right$:D

Edited 6 Years Ago by nick.crane: n/a

No. When only the start is given substring returns all the text from given start index to end of string. A bit link the old VB right$:D

Dude!!! Do you have any idea how much time that little tidbit of information would have saved me in a couple of past projects of mine? lol

This question has already been answered. Start a new discussion instead.