3

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.

denied66
  • 644
  • 7
  • 18
  • Curly braces aren't required in switch case statements, after case. – yogi Jul 28 '12 at 06:45
  • Indeed, I was always wondering why they were looking odd (positioning wise). But that doesn't trim it down too much sadly, still better than it was before, thanks. – denied66 Jul 28 '12 at 06:47
  • In my opinion it would be better not to rely on control names. You should handle the appropriate event (f.e. `BtnDelete.Click` or `BtnCopy.Click`) and call the correct method from there. The code would become easier to understand and maintain and would be less error-prone. That's more important than the number of lines. – Tim Schmelter Jul 28 '12 at 06:55
  • In theory yea, I guess I can put the events in a partial class and work with that. But first things first sadly. The UI class is so clustered at the moment which makes finding things a bit annoying. So the ideal for this case is to trim down everything I can (while of course keeping readability) then after that is done I can move to creating new classes. Thanks for sharing your idea though. – denied66 Jul 28 '12 at 07:00

1 Answers1

3

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;
}
codetiger
  • 2,650
  • 20
  • 37
  • Luckily the names already have the function names, worst case scenario I can use tags I assume. Thanks for the example, it should help out a lot since the majority of the buttons have to do with those 6 we grouped up above. – denied66 Jul 28 '12 at 06:55