locked
Setting buttons in a grid using a for loop RRS feed

  • Question

  • Hi I want have have set up a system wer the the user will select the type of procedure they want which determines the no. of rows and cols that will be displayed on the grid.  Aswell as this, new buttons are created, again depending on the users selection, but I want the buttons to be set in a more complex position rather than in a striaght row or column.  For every 2 cols and 3 rows created, I want to display 4 buttons like this;    

               col1   col2                                   col1  col2   col3   col4                                                                                                                                      row1              B1                         row1             B1              B2                                                                                                                                         row2    A1      C1                         row2    A1     C1    A2      C2                                                                                                                                         row3              D1    if it is 4 cols/3 rows:           D1               D2    As you can see, for every 2 new cols added, it adds new buttons A (i) - D (i).  

    The code i have so far i thing is pretty messy and I cant figure out how to manage this procedure above:  here is my code

    for (int j = 0; j < col; j++)
    {   //Adding 2 cols per each set of buttons - A1 - D1
    diagramGroup.ColumnDefinitions.Add(new ColumnDefinition());
    diagramGroup.ColumnDefinitions.Add(new ColumnDefinition());

     for (int r = 0; r < row; r++)  //Allowing there to perhaps be 6 rows.  ....   but for 3 rows i need 4 buttons... it only creating 3
     {
     Button bt = new Button();
     bt.Name = (char)('A' + r) + (r + 1).ToString();
     bt.Height = 40;
     bt.Width = 40;
     bt.Margin = new Thickness(120, 20, 20, 20);
     bt.Content = (char)('A' + r) + (r+1).ToString();  //incrementing the letter the no. in the content
     bt.Click += new RoutedEventHandler(bt_Click);
     diagramGroup.Children.Add(bt);
                               
     bt.SetValue(Grid.RowProperty, 1);   //This is were i am trying to position them, but they are going straight across 1 row;  result:   A1  B1  C1
     bt.SetValue(Grid.ColumnProperty, +r);
     }
                               
    }

    Im am not sure if i am on the right track with what i have so far, but hopefully someone will know a better way to try this.

    Appreciated,  Emack

    Monday, February 28, 2011 11:40 AM

Answers

  • This is the best i can do: 

    int col = clas;
                    int btCount = stim;

                    if (col <= 3)
                    {
                        for (int j = 0; j < col; j++)
                        {
                            diagramGroup.ColumnDefinitions.Add(new ColumnDefinition());

                            Grid notSerial = new Grid();
                            notSerial.ShowGridLines = true;
                            notSerial.RowDefinitions.Add(new RowDefinition());
                            notSerial.RowDefinitions.Add(new RowDefinition());
                            notSerial.ColumnDefinitions.Add(new ColumnDefinition());
                            notSerial.ColumnDefinitions.Add(new ColumnDefinition());
                            if (btCount >= 4)
                            {
                                notSerial.RowDefinitions.Add(new RowDefinition());
                            }
                            diagramGroup.Children.Add(notSerial);
                            notSerial.SetValue(Grid.RowProperty, 0);
                            notSerial.SetValue(Grid.ColumnProperty, j);

                            Image myArrow = new Image();   //Create Arrow image
                            BitmapImage bi3 = new BitmapImage();
                            bi3.BeginInit();
                            bi3.UriSource = new Uri("images/riteArrow.gif", UriKind.Relative);
                            bi3.EndInit();
                            myArrow.MaxHeight = 50;
                            myArrow.MinWidth = 70;
                            myArrow.Source = bi3;
                            notSerial.Children.Add(myArrow);  //Add Arrow to Grid
                            myArrow.SetValue(Grid.RowSpanProperty, 2);
                            myArrow.SetValue(Grid.ColumnSpanProperty, 2);


                            Button bt1 = new Button();
                            bt1.Name = "A" + (j + 1);
                            bt1.Height = 40;
                            bt1.Width = 60;
                            bt1.Margin = new Thickness(10);
                            bt1.Content = "A" + (j + 1);
                            bt1.Click += new RoutedEventHandler(bt_Click);
                            notSerial.Children.Add(bt1);
                            bt1.SetValue(Grid.RowProperty, 1);
                            bt1.SetValue(Grid.ColumnProperty, 0);

                            Button bt2 = new Button();
                            bt2.Name = "B" + (j + 1);
                            bt2.Height = 40;
                            bt2.Width = 60;
                            bt2.Margin = new Thickness(10);
                            bt2.Content = "B" + (j + 1);
                            bt2.Click += new RoutedEventHandler(bt_Click);
                            notSerial.Children.Add(bt2);
                            bt2.SetValue(Grid.RowProperty, 0);
                            bt2.SetValue(Grid.ColumnProperty, 1);

                            Button bt3 = new Button();
                            bt3.Name = "C" + (j + 1);
                            bt3.Height = 40;
                            bt3.Width = 60;
                            bt3.Margin = new Thickness(10);
                            bt3.Content = "C" + (j + 1);
                            bt3.Click += new RoutedEventHandler(bt_Click);
                            notSerial.Children.Add(bt3);
                            bt3.SetValue(Grid.RowProperty, 1);
                            bt3.SetValue(Grid.ColumnProperty, 1);
                            if ((btCount == 4) || (btCount == 5))
                            {
                                Button bt4 = new Button();
                                bt4.Name = "D" + (j + 1);
                                bt4.Height = 40;
                                bt4.Width = 60;
                                bt4.Margin = new Thickness(10);
                                bt4.Content = "D" + (j + 1);
                                bt4.Click += new RoutedEventHandler(bt_Click);
                                notSerial.Children.Add(bt4);
                                bt4.SetValue(Grid.RowProperty, 2);
                                bt4.SetValue(Grid.ColumnProperty, 1);

                                if (btCount == 5)
                                {
                                    Button bt5 = new Button();
                                    bt5.Name = "E" + (j + 1);
                                    bt5.Height = 40;
                                    bt5.Width = 60;
                                    bt5.Margin = new Thickness(10);
                                    bt5.Content = "E" + (j + 1);
                                    bt5.Click += new RoutedEventHandler(bt_Click);
                                    notSerial.Children.Add(bt5);
                                    bt5.SetValue(Grid.RowProperty, 2);
                                    bt5.SetValue(Grid.ColumnProperty, 0);
                                }
                            }

    • Marked as answer by Emack84 Thursday, March 17, 2011 4:02 PM
    Thursday, March 17, 2011 4:01 PM

All replies

  • Hi Emack,

    I'd like to give you one suggestion. If you want to have an answer on your question, than please describe your question more clearly :)

     

    p.s. lots of text, we are so lazy to read it :)))))))

    Regards,

    Julian :)

    • Proposed as answer by Sheldon _Xiao Tuesday, March 8, 2011 12:10 PM
    Monday, February 28, 2011 12:49 PM
  • Hi there,

    I second Julian's statement - your question doesn't become clear. Furthermore, the setup of your grid - as you drew it - doesn't help much. FWIW, I've posted a sample for a sort-of dynamic grid in this thread . The code there is VB but should be pretty easy to convert.


    Cheers,
    Olaf
    http://blogs.intuidev.com
    • Proposed as answer by Sheldon _Xiao Wednesday, March 2, 2011 12:01 PM
    • Marked as answer by Sheldon _Xiao Tuesday, March 8, 2011 12:10 PM
    • Unmarked as answer by Sheldon _Xiao Wednesday, March 9, 2011 2:27 AM
    Monday, February 28, 2011 2:06 PM
  • Hi Emack,

    If your issue persists, please let us know.


    Sheldon _Xiao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, March 2, 2011 12:00 PM
  • Hi, yes i still have the problem.  I was not working on it for 1 week there, but im back at it again.  Apologies for the too much writing... that was me trying to explain it well as it is quite complex.

    Basically if I start with just 2 cols and 3 rows and can position the 4 buttons in  positions 1,0  0,1   1,1    +   2,1  inside my forloop which creates the cols + rows, then perhaps I can work on positioning them when I add more cols.

    (positioning of the second set of buttons;    1,2     0,3    1,3   2,3)

    Im not great at explaining things :-(  

    I have to get it, so if i dnt get help here i shall reply with the answer when i do get it.

    Thanks,

    Emack

    Tuesday, March 8, 2011 9:59 PM
  • Hi Emack84,

    I am sorry that I have not understood your situation clearly, could you share pictrues with me, and let me know what you want to achieve.

     

    Best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, March 10, 2011 7:50 AM
  • Hi Sheldon,   I am not sure how to send a picture with you on this or shall i email it to your email address?

    The program has 'Classes' and 'Stimuli'.    Stimuli will be buttons, e.g. if user wants 3 stimuli, the program must generate 3 buttons.   'Classes' contain the 'stimuli'   e.g.  3 stimuli will display in each class.    if the user wants 2 classes then this will be 2 classes with 3 buttons in each classes , which equals 6 buttons all together.

    My code Above, can add the proper amount of rows/cols needed and can create the correct number of buttons needed.   but i do not want them positioned in a straight line.

    If i can share with you the picture, then you can see how I would like my buttons placed on the Grid? please let me know how I can do this.

    Many thanks,

    Emack

    Thursday, March 10, 2011 5:55 PM
  • Hi Emack84,

    My email: v-sjinan AT microsoft.com.


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, March 10, 2011 5:58 PM
  • Perhaps if there is a way I can count the total of new buttons that were created, and if i can retrieve each individual button name..   Then i could set  up a if statement after the forloop to hard code the postion of each button.??

    e.g.   if (btCount == '3')

    {   

    A1.SetValue(

     

    Grid.RowProperty, 1);

    A1.SetValue(

     

    Grid.ColumnProperty, 0);

    B1.SetValue(Grid.RowProperty, 0);

    B1.SetValue(Grid.ColumnProperty, 1); .... etc...

    }

    Although it is not very tidy.

    Thanks

    Saturday, March 12, 2011 12:22 PM
  • This is the best i can do: 

    int col = clas;
                    int btCount = stim;

                    if (col <= 3)
                    {
                        for (int j = 0; j < col; j++)
                        {
                            diagramGroup.ColumnDefinitions.Add(new ColumnDefinition());

                            Grid notSerial = new Grid();
                            notSerial.ShowGridLines = true;
                            notSerial.RowDefinitions.Add(new RowDefinition());
                            notSerial.RowDefinitions.Add(new RowDefinition());
                            notSerial.ColumnDefinitions.Add(new ColumnDefinition());
                            notSerial.ColumnDefinitions.Add(new ColumnDefinition());
                            if (btCount >= 4)
                            {
                                notSerial.RowDefinitions.Add(new RowDefinition());
                            }
                            diagramGroup.Children.Add(notSerial);
                            notSerial.SetValue(Grid.RowProperty, 0);
                            notSerial.SetValue(Grid.ColumnProperty, j);

                            Image myArrow = new Image();   //Create Arrow image
                            BitmapImage bi3 = new BitmapImage();
                            bi3.BeginInit();
                            bi3.UriSource = new Uri("images/riteArrow.gif", UriKind.Relative);
                            bi3.EndInit();
                            myArrow.MaxHeight = 50;
                            myArrow.MinWidth = 70;
                            myArrow.Source = bi3;
                            notSerial.Children.Add(myArrow);  //Add Arrow to Grid
                            myArrow.SetValue(Grid.RowSpanProperty, 2);
                            myArrow.SetValue(Grid.ColumnSpanProperty, 2);


                            Button bt1 = new Button();
                            bt1.Name = "A" + (j + 1);
                            bt1.Height = 40;
                            bt1.Width = 60;
                            bt1.Margin = new Thickness(10);
                            bt1.Content = "A" + (j + 1);
                            bt1.Click += new RoutedEventHandler(bt_Click);
                            notSerial.Children.Add(bt1);
                            bt1.SetValue(Grid.RowProperty, 1);
                            bt1.SetValue(Grid.ColumnProperty, 0);

                            Button bt2 = new Button();
                            bt2.Name = "B" + (j + 1);
                            bt2.Height = 40;
                            bt2.Width = 60;
                            bt2.Margin = new Thickness(10);
                            bt2.Content = "B" + (j + 1);
                            bt2.Click += new RoutedEventHandler(bt_Click);
                            notSerial.Children.Add(bt2);
                            bt2.SetValue(Grid.RowProperty, 0);
                            bt2.SetValue(Grid.ColumnProperty, 1);

                            Button bt3 = new Button();
                            bt3.Name = "C" + (j + 1);
                            bt3.Height = 40;
                            bt3.Width = 60;
                            bt3.Margin = new Thickness(10);
                            bt3.Content = "C" + (j + 1);
                            bt3.Click += new RoutedEventHandler(bt_Click);
                            notSerial.Children.Add(bt3);
                            bt3.SetValue(Grid.RowProperty, 1);
                            bt3.SetValue(Grid.ColumnProperty, 1);
                            if ((btCount == 4) || (btCount == 5))
                            {
                                Button bt4 = new Button();
                                bt4.Name = "D" + (j + 1);
                                bt4.Height = 40;
                                bt4.Width = 60;
                                bt4.Margin = new Thickness(10);
                                bt4.Content = "D" + (j + 1);
                                bt4.Click += new RoutedEventHandler(bt_Click);
                                notSerial.Children.Add(bt4);
                                bt4.SetValue(Grid.RowProperty, 2);
                                bt4.SetValue(Grid.ColumnProperty, 1);

                                if (btCount == 5)
                                {
                                    Button bt5 = new Button();
                                    bt5.Name = "E" + (j + 1);
                                    bt5.Height = 40;
                                    bt5.Width = 60;
                                    bt5.Margin = new Thickness(10);
                                    bt5.Content = "E" + (j + 1);
                                    bt5.Click += new RoutedEventHandler(bt_Click);
                                    notSerial.Children.Add(bt5);
                                    bt5.SetValue(Grid.RowProperty, 2);
                                    bt5.SetValue(Grid.ColumnProperty, 0);
                                }
                            }

    • Marked as answer by Emack84 Thursday, March 17, 2011 4:02 PM
    Thursday, March 17, 2011 4:01 PM