It's difficult to say something definit, because a lot of information is missing. But here is my approach:
1. Create type of the tool:
private enum ToolType { Brush, Bucket, Cut }
2. Create inner helper class:
// Inner class that represents a tool in this particular form (control, page) private class UITool { public MyToolType Type { get; set; } public Tool Tool { get; set; } //'Tool' is the parent of Brush, Bucket, Cut public CheckBox ToolCheckBox { get; set; } //'CheckBox' a base class for tspBrush, tspBucket, tspCut /*If tspBrush, tspBucket, tspCut don't belong to the same base class with Checked property, then don't use ToolCheckBox property and use SetToolCollback instead. public Action SetToolCollback { get; set; } */ }
3. Create private property
private List<UITool> _uiTools = new List<UITool>();
4. Init it somewhere
_uiTools.Add(new UITool() { Type = ToolType.Brush, Tool = new Brush(tileLayers), ToolCheckBox = tspBrush });
or if you use SetToolCollback property:
_uiTools.Add(new UITool() { Type = ToolType.Brush, Tool = new Brush(tileLayers), SetToolCollback = () => tspBrush.Checked = true });
5. Create method:
private void SelectTool(ToolType toolType) { var utTool = _uiTools.Select(i => i.Type == toolType).Single(); currentTool = utTool.Tool; UncheckToolstripButtons(); utTool.ToolCheckBox.Checked = true; //If using SetToolCollback //utTool.SetToolCollback(); }
6. Use this method:
private void tspBrush_Click(object sender, EventArgs e) { SelectTool(ToolType.Brush); }
As a variant, you can replace ToolType with the type of 'sender' and use common event handler for all tools like Jan Bolting have suggested.