2015-01-07 4 views
3

내 상황에 맞는 메뉴에서 ToolStripLabel의 텍스트를 변경하면 메뉴 항목의 텍스트를 변경할 때 컨텍스트 메뉴의 크기가 자동으로 조정되지 않습니다.
이 다음과 같이 보이는 :ContextMenuStrip이 제대로 크기가 조정되지 않습니다.

enter image description here

가 어떻게 상황에 맞는 메뉴가 제대로 크기를 조정 할 수 있습니까?
실제 메뉴 항목의 텍스트를 변경할 수는 있지만 더러운 해결책으로 보입니다.


시험 형태 :(사용 마우스 왼쪽 버튼, 왼쪽 및 오른쪽) 해당 메뉴의 Text 속성을 할당 할 때

using System.Drawing; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1: Form 
    { 
     private ToolStripLabel menuLabel; 

     private void CreateNewContextMenu() 
     { 
      ContextMenuStrip = new ContextMenuStrip(); 

      // label 
      menuLabel = new ToolStripLabel("hello"); 
      menuLabel.ForeColor = Color.Blue; 
      ContextMenuStrip.Items.Add(menuLabel); 

      // items 
      ContextMenuStrip.Items.Add("Test"); 
      ContextMenuStrip.Items.Add("Cut"); 
      ContextMenuStrip.Items.Add("&Copy"); 
      ContextMenuStrip.Items.Add("&Paste"); 
      ContextMenuStrip.Items.Add("&Delete"); 
     } 

     protected override void OnMouseClick(MouseEventArgs e) 
     { 
      CreateNewContextMenu(); 
      menuLabel.Text = "hello world hello world hello world"; 
      Point p = PointToScreen(Point.Empty); 

      // left 
      if (e.X < ClientSize.Width/2) 
       ContextMenuStrip.Show(p.X + 8, p.Y + 8); 
      // right 
      else 
      { 
       ContextMenuStrip.Items[1].Text = menuLabel.Text; 
       ContextMenuStrip.Show(p.X + ClientSize.Width - 8, p.Y + 8); 
      } 

      base.OnMouseClick(e); 
     } 
    } 
} 

답변

4

예, ContextMenuStrip을 레이아웃을 다시 계산하지 않습니다 목. 틀림없이 그것은 느리게 그것을해야하지만 그것은 보르켄 보인다. 도움이 필요합니다. 한 줄짜리입니다.

menuLabel.Text = "hello world hello world hello world"; 
    ContextMenuStrip.PerformLayout(); 
+1

고마워요! 그것은 깨끗한 해결책입니다. 실제로 ** ToolStripLabel이 아닌 일반 메뉴 항목 ('ToolStripItem')에 대한 레이아웃을 ** 다시 계산합니다 **. – Bitterblue