Reputation: 644
I've been trying to make the project I'm working a bit more readable since the UI class looks like a mess. The new problem I'm facing is with the StatusStrip, MenuStrip and ToolStrip controls.
Since every button needs to do a different thing this is being controlled with a switch statement at the moment, here's an example:
switch (e.ClickedItem.Text.ToLower())
{
case "find":
{
Find find = new Find(customTextBox1);
find.Show();
break;
}
case "undo":
{
customTextBox1.Undo();
break;
}
case "redo":
{
customTextBox1.Redo();
break;
}
case "cut":
{
customTextBox1.Cut();
break;
}
case "copy":
{
customTextBox1.Copy();
break;
}
case "paste":
{
customTextBox1.Paste();
break;
}
case "delete":
{
customTextBox1.SelectedText = "";
break;
}
case "refresh":
{
RefreshData();
break;
}
case "select all":
{
customTextBox1.SelectAll();
break;
}
}
The above code is just for one item , so imagine having 20 of them with 5-10 subitems each.
I've already cleared up the case methods as you can see and now most of them are one-liners but still with that amount of subitems it just feels like it should be a better way doing this. So ideally I'm looking for a new/better way of handling this issue.
Thanks in advance.
Upvotes: 3
Views: 485
Reputation: 2779
You can use the following code, however you've to make sure, the string is exactly same as function name
switch (e.ClickedItem.Text.ToLower())
{
case "find":
Find find = new Find(customTextBox1);
find.Show();
break;
case "undo": case "redo": case "cut": case "copy": case "paste": case "select all":
Type thisType = customTextBox1.GetType();
MethodInfo theMethod = thisType.GetMethod(e.ClickedItem.Text.ToLower());
theMethod.Invoke(customTextBox1, userParameters);
break;
case "delete":
customTextBox1.SelectedText = "";
break;
case "refresh":
RefreshData();
break;
}
Upvotes: 3