Christopher
Christopher

Reputation: 788

Clickable Grid in C# Winform

I want to generate a grid that operates more or less like http://www.favicon-generator.org/editor/

But I want to gather the height x width from the user (in tiles), then generate a grid of that size. The tiles should all be white or 'clear' to start, and when they click on a specific tile it will change it to black. Clicking on a black tile will change it back to white.

I have created the winform to gather the height x width and save it as a user setting. But I'm really struggling to find the best way to create a grid that functions how I want.

I originally went with creating lots of buttons, but that just became too crazy. What are some ways you would try to create this?

Upvotes: 0

Views: 1580

Answers (1)

Jeffrey Wieder
Jeffrey Wieder

Reputation: 2376

Use a DataGridView, Add a column for each width count and a row for each height count.

Then register the cell click event and change that cell's background color.

    dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick);

    void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        dataGridView1[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.White;
    }

Upvotes: 2

Related Questions