2010-02-11 17 views

回答

7

此代碼基於來自The Code Project的文章(http://www.codeproject.com/KB/grid/GridDescriptionHeight.aspx),其中介紹了兩個修復程序和一些清理。

private void ResizeDescriptionArea(PropertyGrid grid, int lines) 
{ 
    try 
    { 
     var info = grid.GetType().GetProperty("Controls"); 
     var collection = (Control.ControlCollection)info.GetValue(grid, null); 

     foreach (var control in collection) 
     { 
      var type = control.GetType(); 

      if ("DocComment" == type.Name) 
      { 
       const BindingFlags Flags = BindingFlags.Instance | BindingFlags.NonPublic; 
       var field = type.BaseType.GetField("userSized", Flags); 
       field.SetValue(control, true); 

       info = type.GetProperty("Lines"); 
       info.SetValue(control, lines, null); 

       grid.HelpVisible = true; 
       break; 
      } 
     } 
    } 

    catch (Exception ex) 
    { 
     Trace.WriteLine(ex); 
    } 
} 

我已經在我自己的項目中使用它;它應該適合你。

+0

工作很好。在我能找到一個很好的價值之前,我不得不使用Line的值,但它並不真正直觀地表示Line是什麼,但是低於10的數字似乎是我需要的 – PICyourBrain 2010-02-15 15:05:38

+0

它對我沒有任何作用。事實證明,這個代碼在加載Form之前不起作用。在此之前,它會使描述區域爲零。 – Qwertie 2010-09-21 20:44:29

+0

這是第一次問這個問題已經很長時間了,但我仍然在這裏提供幫助。我在Form.OnShown中調用了這個函數。試試看,它應該在這種情況下工作。 – 2010-09-22 14:19:18

0

你不能用PropertyGrid控件暴露的公共方法和屬性來做到這一點,或者至少我找不到任何有用的東西。
您可能會嘗試使用反射來獲取顯示設置或說明的屬性網格的子控件,並嘗試以編程方式設置其高度;我想分離器只是停靠了,設置它的位置不會改變任何東西。
使用調試器查看PropertyGrid的非公共成員應該可以幫助您瞭解控件的內部結構。

0

這裏是Matthew Ferreira在VB.Net中的解決方案。謝謝馬修,作品一種享受!

Imports System.Reflection 

    Public Sub ResizeDescriptionArea(grid As PropertyGrid, lines As Integer) 
     Try 
      Dim info = grid.[GetType]().GetProperty("Controls") 
      Dim collection = DirectCast(info.GetValue(grid, Nothing), Control.ControlCollection) 

      For Each control As Object In collection 
       Dim type = control.[GetType]() 

       If "DocComment" = type.Name Then 
        Const Flags As BindingFlags = BindingFlags.Instance Or BindingFlags.NonPublic 
        Dim field = type.BaseType.GetField("userSized", Flags) 
        field.SetValue(control, True) 

        info = type.GetProperty("Lines") 
        info.SetValue(control, lines, Nothing) 

        grid.HelpVisible = True 
        Exit For 
       End If 
      Next 

     Catch ex As Exception 
      Trace.WriteLine(ex) 
     End Try 
    End Sub 
相關問題