locked
Adding names to the newly created buttons in a for-loop RRS feed

  • Question

  • Hi, I am relatively new to WPF and im using Visual studio 2010.  I have created a grid which draws the number of rows and columns based on the users selection.  and for each row and each col added, I have added a new button to each, so for example if the grid has 2 rows and 3 cols, this will display a total of 6 buttons... However, I want to give a certain name to each button added.  Here is my code so far:

    for

     

     

    (int i = 0; i < row; i++)

    {

    diagramGroup.RowDefinitions.Add(

     

    new RowDefinition());  // creates the new rows

    }

     

     

    for (int j = 0; j < col; j++)

    {

    diagramGroup.ColumnDefinitions.Add(

     

    new ColumnDefinition()); //Creates the new columns

     

     

        for (int i = 0; i < row; i++)

        {

     

     

        Button bt = new Button();

        bt.Name =

     

    "A" + i;   //Currently is adding names like, 'A0', 'A1', 'A2'...

        bt.Height = 40;

        bt.Width = 60;

        bt.Margin =

     

    new Thickness(120, 20, 20, 20);

        bt.Content =

     

    "A" + i;  //Currently is adding names like, 'A0', 'A1', 'A2'...

        bt.Click +=

     

    new RoutedEventHandler(bt_Click);

        diagramGroup.Children.Add(bt);

        bt.SetValue(

     

    Grid.RowProperty, +i);

        bt.SetValue(

     

    Grid.ColumnProperty, +j);

        }

    }

    Here is an example of how i'd like my buttons Content to appear on the screen:    (Col 0),(Col 1),(Col,2)...

                                                                              (row 0)  'A1'    'B1'     'C1'   etc...

                                                                             (row 1)   'A2'    'B2'    'C2'   etc...

    Is it possible to change this code slightly to allow for thebutton naming to be added like this?

    to hope this isnt too confusing,

    Thanks, Emack

     

     

    Saturday, February 26, 2011 1:12 AM

Answers

  • Instead of using:

     

        "A" + i;

     

    You can use:

    (char)('A' + j) + i.ToString();
    


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by Emack84 Saturday, February 26, 2011 12:16 PM
    Saturday, February 26, 2011 2:29 AM

All replies

  • Instead of using:

     

        "A" + i;

     

    You can use:

    (char)('A' + j) + i.ToString();
    


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by Emack84 Saturday, February 26, 2011 12:16 PM
    Saturday, February 26, 2011 2:29 AM
  • Thanks so much, It worked a treat.

    I made one change to iliminate the number '0' as I wanted my names to begin with "A 1"  instead of A0.  So i just added simply:

    (char)('A' + j) + (i+1).ToString();
    

    Appreciated,

    Emack

    Saturday, February 26, 2011 12:19 PM