有没有办法对 stripMenuItems 的类似处理程序进行分组

本文关键字:程序 处理 stripMenuItems 有没有 | 更新日期: 2023-09-27 17:56:38

我有一个简单的位图编辑器,用于小的单色位图。我有一个菜单,可以选择笔和强盗的粗细。问题是我不喜欢下面的代码。有没有办法让它更紧凑?

    private void toolStripMenuItem4_Click(object sender, EventArgs e)
    {
        CurrentPanel.PenThickness = 3;
    }
    private void toolStripMenuItem3_Click(object sender, EventArgs e)
    {
        CurrentPanel.PenThickness = 2;
    }
    private void toolStripMenuItem2_Click(object sender, EventArgs e)
    {
        CurrentPanel.PenThickness = 1;
    }
    private void toolStripMenuItem5_Click(object sender, EventArgs e)
    {
        CurrentPanel.PenThickness = 4;
    }
    private void toolStripMenuItem6_Click(object sender, EventArgs e)
    {
        CurrentPanel.PenThickness = 5;
    }
    private void toolStripMenuItem7_Click(object sender, EventArgs e)
    {
        CurrentPanel.PenThickness = 6;
    }
    private void toolStripMenuItem8_Click(object sender, EventArgs e)
    {
        CurrentPanel.PenThickness = 7;
    }
    private void toolStripMenuItem9_Click(object sender, EventArgs e)
    {
        CurrentPanel.PenThickness = 8;
    }
    private void toolStripButton3_Click_1(object sender, EventArgs e)
    {
        CurrentPanel.EditMode = EditMode.Clear;
    }
    private void toolStripDropDownButton2_Click(object sender, EventArgs e)
    {
        CurrentPanel.EditMode = EditMode.FreeHand;
    }
    private void toolStripSplitButton1_ButtonClick(object sender, EventArgs e)
    {
        CurrentPanel.EditMode = EditMode.Clear;
    }
    private void toolStripMenuItem10_Click(object sender, EventArgs e)
    {
        CurrentPanel.RobberThickness = 1;
    }
    private void toolStripMenuItem11_Click(object sender, EventArgs e)
    {
        CurrentPanel.RobberThickness = 2;
    }
    private void toolStripMenuItem12_Click(object sender, EventArgs e)
    {
        CurrentPanel.RobberThickness = 3;
    }
    private void toolStripMenuItem13_Click(object sender, EventArgs e)
    {
        CurrentPanel.RobberThickness = 4;
    }
    private void toolStripMenuItem14_Click(object sender, EventArgs e)
    {
        CurrentPanel.RobberThickness = 5;
    }
    private void toolStripMenuItem15_Click(object sender, EventArgs e)
    {
        CurrentPanel.RobberThickness = 6;
    }
    private void toolStripMenuItem16_Click(object sender, EventArgs e)
    {
        CurrentPanel.RobberThickness = 7;
    }
    private void toolStripMenuItem17_Click(object sender, EventArgs e)
    {
        CurrentPanel.RobberThickness = 8;
    }
    private void toolStripMenuItem18_Click(object sender, EventArgs e)
    {
        CurrentPanel.RobberThickness = 16;
    }
    private void toolStripMenuItem19_Click(object sender, EventArgs e)
    {
        CurrentPanel.RobberThickness = 32;
    }
    private void toolStripMenuItem21_Click(object sender, EventArgs e)
    {
        CurrentPanel.PenThickness = 32;
    }
    private void toolStripMenuItem20_Click(object sender, EventArgs e)
    {
        CurrentPanel.PenThickness = 16;
    }

有没有办法对 stripMenuItems 的类似处理程序进行分组

private void toolStripMenuItemCommon_Click(object sender, EventArgs e)
{
     ToolStripMenuItem item = (ToolStripMenuItem) sender;
     int thickness = (int)item.Tag;
     CurrentPanel.PenThickness = thickness ;
}

当然,您需要初始化每个ToolStripMenuItem Tag,并将toolStripMenuItemCommon_Click设置为Click事件的处理程序。您可以在for周期中执行此操作,例如:

for(int i = 1; i < 8; i++)
{
    ToolStripMenuItem item = new ToolStripMenuItem();
    item.Text = "Set thickness: " + i;
    item.Click += toolStripMenuItemCommon_Click;
    item.Tag = i;
    // add item to strip container
}

是的,有:

private void SetPenThickness(int thickness)
{
    CurrentPanel.PenThickness = thickness;
}
toolStripMenuItem1.Click += (s,e) => SetPenThickness(1);
toolStripMenuItem2.Click += (s,e) => SetPenThickness(2);
toolStripMenuItem3.Click += (s,e) => SetPenThickness(3);
toolStripMenuItem4.Click += (s,e) => SetPenThickness(4);
// ...

试试这个:

private void toolStripMenuItem_Click(object sender, EventArgs e)    
{        
     ToolStripMenuItem toolStripMenuItem = (ToolStripMenuItem) sender;
     switch (toolStripMenuItem.ID)
     {
         case "toolStripMenuItem4":
         {
              CurrentPanel.PenThickness = 3;    
              break;
         }
         ..........
         ..........
     }
}