2013-03-28 31 views
0

我有一个WinForm应用程序,并且我有一个ToolStripSplitButton,它包含三个项目(项目1,项目2,项目3)。通过ToolStripSplitButton中的DropDownItems骑自行车

现在我想要做的就是让用户点击ToolStripSplit按钮,然后将下一个值分配给ToolStripSplit按钮的文本属性。我想出了以下的解决方案,工作正常,但我不知道是否有这样做的更好的办法:

private void toolStripSplitButton_ButtonClick(object sender, EventArgs e) 
{ 
    ToolStripSplitButton tsb = (ToolStripSplitButton)sender; 

    for (int i = 0; i < tsb.DropDownItems.Count; i++) 
    { 
     int ii = i + 1; 
     if (ii >= tsb.DropDownItems.Count) 
     { 
      ii = 0; 
     } 

     if (tsb.Text == tsb.DropDownItems[i].Text) 
     { 
      tsb.Text = tsb.DropDownItems[ii].Text; 
      break; 
     } 
    } 
} 

回答

2

更好的是在旁观者的眼睛。我的版本:

private void toolStripSplitButton1_ButtonClick(object sender, EventArgs e) { 
    ToolStripSplitButton tsb = (ToolStripSplitButton)sender; 
    string text = tsb.DropDownItems[0].Text; 
    bool found = false; 
    for (int i = 0; i < tsb.DropDownItems.Count; i++) { 
    if (found) text = tsb.DropDownItems[i].Text; 
    found = (tsb.Text == tsb.DropDownItems[i].Text); 
    } 
    tsb.Text = text; 
} 
+0

谢谢我会试试看。 –