0

I need to show a grid on a Form, that displays as 32 by 32 cells, each cell can be clicked to do some operation(like the windows calendar app). I'v considered buttons and panels, but that'll be 1024 controls. any better way?

Brian Holsen
  • 350
  • 3
  • 10
  • 1
    wpf or winforms? An efficient way would be to have one panel listen to the click event and then using the position of the click to determine what should happen next. – sknt Nov 10 '16 at 10:36
  • wpf or winform, whichever has a way to solve this. I need those cells to act separately, for example,.show different colors – Brian Holsen Nov 10 '16 at 10:41
  • You can use a `TableLayoutPanel` or a `DataGridView`. See [this example](http://stackoverflow.com/questions/33968993/how-to-create-a-magic-square-using-windows-forms) or [this one](http://stackoverflow.com/questions/38127389/can-datagridview-be-used-to-accomplish-the-following-table-like-ui-in-winforms) What's the requirement for cells? What would they contain? – Reza Aghaei Nov 10 '16 at 11:01
  • they just show colors(black or white), like the minesweeper game – Brian Holsen Nov 10 '16 at 11:06
  • 1
    @BrianHolsen you should use a DataGridView. One Control and you got it. – Sebi Nov 10 '16 at 11:17

1 Answers1

0

Here's a way to create a responsive grid with buttons in WPF:

The responsiveness is created with GridLength(1, GridUnitType.Star)

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        for (int x = 0; x < 32; x++)
        {
            buttonGrid.ColumnDefinitions.Add(new ColumnDefinition
            {
                Width = new GridLength(1, GridUnitType.Star)
            });
            buttonGrid.RowDefinitions.Add(new RowDefinition
            {
                Height = new GridLength(1, GridUnitType.Star)
            });

            for (int y = 0; y < 32; y++)
            {
                var bt = new Button();
                bt.Content = x + "" + y;
                Grid.SetRow(bt, x);
                Grid.SetColumn(bt, y);
                buttonGrid.Children.Add(bt);
                bt.Click += Bt_Click;
            }
        }
    }

    private void Bt_Click(object sender, RoutedEventArgs e)
    {
        Button bt = (Button)sender;
        bt.Background = Brushes.Black;
    }
}

MainWindow.xaml

<Grid Name="buttonGrid"></Grid>

Be aware that resizing is going to be quite slow.

sknt
  • 963
  • 6
  • 16