Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7682775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:38:46+00:00 2026-05-31T18:38:46+00:00

so this code here dynamically adds buttons to my wpf windows application. I cant

  • 0

so this code here dynamically adds buttons to my wpf windows application. I cant think of the way in which I can call buttons which actually runat server because they are randomly added.

namespace DynamicButtons
{
    public partial class Window1
    {
        public Window1()
        {
            this.InitializeComponent();

            populateButtons();
        }

        public void populateButtons()
        {
            int xPos;
            int yPos;

            Random ranNum = new Random();

            for (int i = 0; i < 4; i++)
            {
                Button foo = new Button();
                Style buttonStyle = Window.Resources["CurvedButton"] as Style;

                int sizeValue = ranNum.Next(100);

                foo.Width = sizeValue;
                foo.Height = sizeValue;

                xPos = ranNum.Next(200);
                yPos = ranNum.Next(300);

                foo.HorizontalAlignment = HorizontalAlignment.Left;
                foo.VerticalAlignment = VerticalAlignment.Top;
                foo.Margin = new Thickness(xPos, yPos, 0, 0);

                foo.Style = buttonStyle;
                foo.Name = "button" + i;

                foo.Click += new RoutedEventHandler(buttonClick);

                LayoutRoot.Children.Add(foo);
           }
        }

        private void buttonClick(object sender, EventArgs e)
        {
            Button clicked = (Button) sender;
            // something like certainWindowsButton((i)<-this has to be based on above code) = clicked.Name(); 
            MessageBox.Show("Button's name is: " + clicked.Name);
        }
    }
}

Every time the application runs the buttons are randomly assigned, what I want is a way in which I can interact with them from the code behind.

So

foo.Name = "button" + i; 

means when or whichever button is clicked a number is assigned to it but how can I interact with that button in the code?

    private void buttonClick(object sender, EventArgs e)
    {
        Button clicked = (Button) sender;
        // something like certainWindowsButton((i)<-this has to be based on above code) = clicked.Name(); 
        MessageBox.Show("Button's name is: " + clicked.Name);
    }
}

I hope that makes sense.

To give you a sense of whats happening at design level:

enter image description here

Each of these grey squares are buttons each button is randomly assigned its designated name and number and each time the application runs these buttons will be reassigned a different number.

I need a way inwhich when a number is assigned to it I can then fire an event for that button clicked.

So randomly a button has been assigned number 1, take this number 1 and then assign it to

private void buttonClick(object sender, EventArgs e) 

if
{
    button1_Click = clicked.Name(strip away "button" and make sure the (variableNumber leftover matches button(1)_Click);
}
else 
    button2

etc etc

Which then fires my button event:

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("yay each randomly assigned button now correlates with a real button event");
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T18:38:47+00:00Added an answer on May 31, 2026 at 6:38 pm

    Is this more what you’re looking for? This takes each buttonNClick handler and creates a button for it. This way you don’t even need the “i”.

    partial class Window1 {
        void button3Click(object sender, RoutedEventArgs e) {
            //Whatever you want here
        }
        void button2Click(object sender, RoutedEventArgs e) {
            //Whatever you want here
        }
        void button1Click(object sender, RoutedEventArgs e) {
            //Whatever you want here
        }
    
        public Window1() {
            this.InitializeComponent();
            populateButtons();
        }
    
        public void populateButtons() {
            int xPos;
            int yPos;
    
            Random ranNum = new Random();
            foreach (var routedEventHandler in new RoutedEventHandler[] { button1Click, button2Click, button3Click }) {
                Button foo = new Button();
    
                int sizeValue = ranNum.Next(100);
    
                foo.Width = sizeValue;
                foo.Height = sizeValue;
    
                xPos = ranNum.Next(200);
                yPos = ranNum.Next(300);
    
                foo.HorizontalAlignment = HorizontalAlignment.Left;
                foo.VerticalAlignment = VerticalAlignment.Top;
                foo.Margin = new Thickness(xPos, yPos, 0, 0);
    
                foo.Click += routedEventHandler;
    
                LayoutRoot.Children.Add(foo);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code here, which is intended to allow any type of arguments:
I've got this code here: SqlCommand CodeStatus = new SqlCommand(SQL, DB); DB.Open(); Reader =
I have this code here: var infiltrationResult; while(thisOption) { var trNode = document.createElement('tr'); var
I have this code here, {foreach from=$cart.cartItems item=item name=cart} <div id=cart2Produkt> <p>{if $item.Product.ID} <a
I found this code here class Usable; class Usable_lock { friend class Usable; private:
Im using Doctrine and i dont quite understand this code here: $this->hasColumn('id', 'integer', 8,
Hey I have this code right here: http://pastie.org/534470 And on line 109 I get
I found this code from here: http://www.cssportal.com/form-elements/text-box.htm But the problem is you can still
Something must be wrong with this code right here: + (UIImage*)captureView:(UIView *)theView { UIGraphicsBeginImageContext(theView.frame.size);
Got this line of code here but its not working. private void Button_Click(object sender,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.