views:

262

answers:

3

What is the best way to implement a 2D grid of radio buttons so that only one option in each column and one option in each row can be selected?

+1  A: 

A 1D array of 1D arrays of radio buttons. Each row (or column) would use the normal radio button functionality, while each column (or row) would be updated by a loop called whenever an individual radio button is toggled.

Sparr
A: 

Something like this?

using System;
using System.Drawing;
using System.Windows.Forms;

class Program {

    static RadioButton[] bs = new RadioButton[9];

    static void HandleCheckedChanged (object o, EventArgs a) {
        RadioButton b = o as RadioButton;
        if (b.Checked) {
            Console.WriteLine(Array.IndexOf(bs, b));
        }
    }

    static void Main () {
        Form f = new Form();
        int x = 0;
        int y = 0;
        int i = 0;
        int n = bs.Length;
        while (i < n) {
            bs[i] = new RadioButton();
            bs[i].Parent = f;
            bs[i].Location = new Point(x, y);
            bs[i].CheckedChanged += new EventHandler(HandleCheckedChanged);
            if ((i % 3) == 2) {
                x = 0;
                y += bs[i].Height;
            } else {
                x += bs[i].Width;
            }
            i++;
        }
        Application.Run(f);
    }

}

Regards, tamberg

tamberg
This creates a single 2D button group which allows only one button in the entire group to be selected. In my question, I specified that it should be one per row and one per column (that would be a total of three selected out of nine buttons in six groups, for example).
Dennis Williamson
A: 

You could approach this with a custom collection and databind the radio buttons.

Each individual child would have a property for row and a property for column as well as the true/false value flag, which raises an event when changed to true via a click or keypress.

Logic in the collection class would respond to the value change and loop through the other children in the same row and column to notify them that they should be false.

If you don't want to databind, you could also do it with a collection of user controls.

Doug L.