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 5945337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:40:02+00:00 2026-05-22T16:40:02+00:00

I have code that creates a button for each object in a list. When

  • 0

I have code that creates a button for each object in a list. When each object is created it is given a name that corresponds to the row and column (i.e. name = row1col2). Each button is generated dynamically, added to the grid and then the row/column are set. At a later time I need to gather the “selected” button data so I can perform actions on the data they represent. When I attempt to get the control data from the buttons everything is fine, except for the grid row/column data. It remains the same for all of the selected rows and columns in a grid.

Creating buttons:

for (int i = 1; i < row.StackCount+1; i++)
{
    //create button for the column
    stackButton = new Button();
    stackButton.Height = ((newRow.Height - 2));
    stackButton.Width = ((newRow.Width / row.StackCount) - 2);
    stackButton.Background = new SolidColorBrush(Colors.White);

    //add the button border
    stackButton.BorderBrush = new SolidColorBrush(Colors.Black);
    stackButton.BorderThickness = new Thickness(1);
    stackButton.Style = Application.Current.Resources["flatButton"] as Style;

    //add the button name
    stackButton.Name = "Row" + row.LineNumber + "Col" + (i - 1).ToString();

    //add the event handler to the button
    stackButton.Click += new RoutedEventHandler(stackButton_Click);

    //add a new column
    newRow.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(newRow.Width, GridUnitType.Star) });

    //put the button into the grid
    newRow.Children.Add(stackButton); 
    Grid.SetRow(stackButton, 0);
    Grid.SetColumn(stackButton, i-1);
}

Getting the Button data back

g = (Grid)b.Child;
foreach (Button currentButton in g.Children)
{
    if (((SolidColorBrush)currentButton.Background).Color == Colors.Gray)
    {
        //create a stack object
        buttonData.StartDate = DateTime.Now;
        buttonData.LotNumber = LotDisplay.Text;
        buttonData.RoomID = SilverGlobals.CurrentRoom.RoomID;

        buttonData.RoomCol = Grid.GetColumn(currentButton);
        buttonData.RoomRow = Grid.GetRow(currentButton);

        buttonData.TrayCount = int.Parse(currentButton.Content.ToString());
        buttonData.Status = 0;

        //add stack object to list of stack objects
        stacks.Add(buttonData);
    }
}

I know this must be something small I am missing. Anyone got any ideas?

  • 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-22T16:40:03+00:00Added an answer on May 22, 2026 at 4:40 pm

    Although the comment in your second section of code says:

    //create a stack object
    

    you don’t actually create a new stack object so it is simply overwriting the single instance of buttonData on each iteration. The values for row and column that you see at the end are the last iteration’s values.

    The net effect is that stacks is a collection of all the same instance of an object instead of a collection of separate instances.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that dynamically creates a new button through JavaScript that when
So I'm running through a list of things and have code that creates an
I have the following code that creates a serverside object of the xmlhttp class.
I have some code that creates a new site in SharePoint. Upon browsing to
I have written code that automatically creates CSS sprites based on the IMG tags
I have the following code that creates two objects (ProfileManager and EmployerManager) where the
I have some javascript code that creates an img tag with a mouseover callback,
I have some Javascript code that creates 2 arrays: One for Product Category and
I have a bit of Javascript code that creates a save friendly version of
I have some code on my PHP powered site that creates a random hash

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.