2016-08-19 2 views
0

각 셀에 Panel이 포함 된 프로그래밍 방식으로 만들어진 TableLayoutPanel이 있습니다. 각 패널에는 맞춤 라벨이 있습니다. (레이블의 Enabled 속성은 false로 설정되어 있는데 차이가 있는지는 확실하지 않습니다.) 사용자가 마우스를 가리킬 때마다 Label 텍스트를 표시하고 싶습니다.TableLayoutPanel에서 셀 위로 마우스를 가져 가면 텍스트 표시 - C#

내가 읽은 것으로부터 툴팁이 좋은 방법이지만, 제대로 작동하지 못했습니다.

TableLayoutPanel은 간단히 이름이 "tlp"이며 쉽게 액세스 할 수 있도록 양식의 구성원입니다 (이름이 "toolTip"인 ToolTip에서도 마찬가지 임).

지금은 단지 모든 종류의 텍스트를 가져 오려고합니다. 레이블을 사용하면 여기에 문자열을 대치하겠습니다.

private void hoverOverSpace(object sender, EventArgs e) 
{ 
    int row = tlp.GetRow((Panel)sender); 
    int col = tlp.GetColumn((Panel)sender); 

    toolTip.Show("Does this work?", tlp.GetControlFromPosition(col, row).Controls[0]); 
    //toolTip.Show("Does this work?", tlp.GetControlFromPosition(col, row)); 
} 

내 도구 설명을 표시하지 못했습니다. 내가 잘못한 일을하고 있습니까/내가 성취하려는 일을하기위한 더 좋은 방법이 있습니까?

편집 : 나는 각 패널에 툴팁을 추가하려고 시도했지만 여전히 아무것도

// Add Panels to TableLayoutPanel 
for (int i = 0; i < rows; i++) 
{ 
    for (int j = 0; j < cols; j++) 
    { 
     // Create new Panel 
     Panel space = new Panel() 
     { 
      Size = new Size(45, 45), 
      Dock = DockStyle.Fill, 
      Margin = new Padding(0) 
     }; 

     space.MouseClick += new MouseEventHandler(clickOnSpace); 

     CustomLabel info = new CustomLabel(false, 0, Color.White);  // Create new CustomLabel 
     space.Controls.Add(info); // Add CustomLabel to Panel 
     tlp.Controls.Add(space, j, i);  // Add Panel to TableLayoutPanel 

     toolTip = new ToolTip(); 
     toolTip.SetToolTip(space, info.Text); 
    } 
} 
+0

*는 "'Label's ''Enabled' 속성은 FALSE' '로 설정되고, 그 차이를 만드는 경우 확실하지 "* - 변화를 가져옵니다. 비활성화 된 컨트롤에 대한 툴팁은 표시되지 않습니다. 그러나 [해결 방법] (http://stackoverflow.com/q/7887817/1997232)을 사용할 수 있습니다. – Sinatr

+0

대신 이벤트 핸들러없이 패널의 툴팁을 설정해보십시오. 패널 위로 마우스를 움직이면 (아직 레이블 위를 지나지 않았다면) 볼 수 있습니다. – Sinatr

+0

작동하지 않는 것 같습니다 (편집 된 코드 참조). – NickV987

답변

1

이 답변이에 대한 답변에 제시된 코드를 기반으로 발생하지 않습니다 : tablelayoutPanel get cell location from mouse over, by: Aland Li Microsoft CSS.

#region GetPosition 
    // Modified from answer to: tablelayoutPanel get cell location from mouse over 
    // By: Aland Li Microsoft CSS 
    // https://social.msdn.microsoft.com/Forums/windows/en-US/9bb6f42e-046d-42a0-8c83-febb1dcf98a7/tablelayoutpanel-get-cell-location-from-mouse-over?forum=winforms 

//The method to get the position of the cell under the mouse. 
private TableLayoutPanelCellPosition GetCellPosition(TableLayoutPanel panel, Point p) 
{ 

    //Cell position 
    TableLayoutPanelCellPosition pos = new TableLayoutPanelCellPosition(0, 0); 
    //Panel size. 
    Size size = panel.Size; 
    //average cell size. 
    SizeF cellAutoSize = new SizeF(size.Width/panel.ColumnCount, size.Height/panel.RowCount); 

    //Get the cell row. 
    //y coordinate 
    float y = 0; 
    for (int i = 0; i < panel.RowCount; i++) 
    { 
     //Calculate the summary of the row heights. 
     SizeType type = panel.RowStyles[i].SizeType; 
     float height = panel.RowStyles[i].Height; 
     switch (type) 
     { 
      case SizeType.Absolute: 
       y += height; 
       break; 
      case SizeType.Percent: 
       y += height/100 * size.Height; 
       break; 
      case SizeType.AutoSize: 
       y += cellAutoSize.Height; 
       break; 
     } 
     //Check the mouse position to decide if the cell is in current row. 
     if ((int)y > p.Y) 
     { 
      pos.Row = i; 
      break; 
     } 
    } 

    //Get the cell column. 
    //x coordinate 
    float x = 0; 
    for (int i = 0; i < panel.ColumnCount; i++) 
    { 
     //Calculate the summary of the row widths. 
     SizeType type = panel.ColumnStyles[i].SizeType; 
     float width = panel.ColumnStyles[i].Width; 
     switch (type) 
     { 
      case SizeType.Absolute: 
       x += width; 
       break; 
      case SizeType.Percent: 
       x += width/100 * size.Width; 
       break; 
      case SizeType.AutoSize: 
       x += cellAutoSize.Width; 
       break; 
     } 
     //Check the mouse position to decide if the cell is in current column. 
     if ((int)x > p.X) 
     { 
      pos.Column = i; 
      break; 
     } 
    } 

    //return the mouse position. 
    return pos; 
} 
#endregion 

그것은 그 위치에서 Control을 얻었다 (있는 경우) 및 TableLayoutPanel.MouseHover 이벤트에 ToolTip로서의 Text 속성을 표시하는 참조 부호에 의해 계산 TableLayoutPanelCellPosition을 이용한다.

private void tableLayoutPanel1_MouseHover(object sender, EventArgs e) 
{ 
    Point pt = tableLayoutPanel1.PointToClient(Control.MousePosition); 
    TableLayoutPanelCellPosition pos = GetCellPosition(tableLayoutPanel1, pt); 
    Control c = tableLayoutPanel1.GetControlFromPosition(pos.Column, pos.Row); 
    if (c != null) 
    { 
     toolTip1.Show(c.Text, tableLayoutPanel1, pt, 500); 
    } 
} 

편집 :

은 내가 TLP는 DockStyle.Fill`로 설정 자신의 Dock 속성을 컨트롤로 채워집니다 것을 놓쳤다. TLP에 배치 된 그러한 제어 장소는 TLP 대신 마우스 이벤트를 수신합니다. 그래서 수정,이 방법을 추가하십시오.

private void showtip(object sender, EventArgs e) 
{ 
    Point pt = tableLayoutPanel1.PointToClient(Control.MousePosition); 
    TableLayoutPanelCellPosition pos = GetCellPosition(tableLayoutPanel1, pt); 
    Control c = tableLayoutPanel1.GetControlFromPosition(pos.Column, pos.Row); 
    if (c != null && c.Controls.Count > 0) 
    { 
     toolTip1.Show(c.Controls[0].Text, tableLayoutPanel1, pt, 500); 
    } 
} 

PanelLabel 다음과 같이 그룹화 그런 wireup :

this.panel4.MouseHover += new System.EventHandler(this.showtip); 
this.label4.MouseHover += new System.EventHandler(this.showtip); 
+0

언급 된 코드와이 줄을 TableLayoutPanel 선언 아래에 추가했습니다. "tlp.MouseHover + = new EventHandler (tlp_MouseHover);" 그러나 이제는 마우스가 테이블 위에 있다는 것도 등록하지 않았습니다. 이것을 잘못 지정 했습니까? – NickV987

+0

또한, C는 아마도 tableLayoutPanel1.GetControlFromPosition (pos.Column, pos.Row)이어야합니다 .Controls [0]; 왜냐하면 Panel 내부의 Label의 텍스트가 필요하기 때문입니다. 그러나 이벤트를 발생시킬 수 있다고 가정하면 쉽게 해결할 수 있습니다. – NickV987

+0

@ NickV987, 나는'DockStyle.Fill'으로 설정된'Panel' 컨트롤로 TableLayoutPanel을 채우는 것을 놓쳤습니다.이것은 이벤트의 배선을 약간 복잡하게 만듭니다. TableLayoutPanel 위의 컨트롤은 마우스 이벤트를 가로 채고 처리하기 때문에. 몇 분 안에 업데이트를 게시 할 것입니다. – TnTinMn

관련 문제