 |
|
 |
Strangely, this doesn't work if the PropertyGrid is on a form that is shown maximized by setting form.WindowState. When the maximized form is "restored", the new height for the description area takes effect. It works just fine, on the same form, if the form is not maximized upon initialization.
|
|
|
|
 |
|
 |
Hi,
Sorry to perform "thread necromancy". Your code looks as though it will work to set the height. I haven't tried it yet, because I want to implement a user setting that will save the user's defined height of the control, so I need to save it first.
Based on your sample, I tried to write another method, called GetPropertyGridDescriptionAreaHeight:
private int GetPropertyGridDescriptionAreaHeight(ref PropertyGrid grid)
{
try
{
PropertyInfo pi = grid.GetType().GetProperty("Controls");
Control.ControlCollection cc = (Control.ControlCollection)pi.GetValue(grid, null);
foreach (Control c in cc)
{
Type ct = c.GetType();
string sName = ct.Name;
if (sName == "DocComment")
{
pi = ct.GetProperty("Lines");
return Convert.ToInt32(pi.GetValue(c, null));
}
}
return 0;
}
catch (Exception error)
{
#if(DEBUG)
MessageBox.Show(error.Message, "GetDescriptionAreaSize");
#endif
return 0;
}
}
That should work, right? But it doesn't. Setting a breakpoint and running "?pi.GetValue(c, null)" after pi has been set causes "An exception has been caused by the target of an invocation."
Any idea what I'm doing wrong?
Thanks
Graham
|
|
|
|
 |
|
 |
pi.GetValue() can throw four different exceptions, but the one you see does not appear to be one of them.
If pi.GetValue(c, null) returns null, what will Convert.ToInt32() do? Throw the exception you see, perhaps?
Anyway, when I put this code in my program, it works fine:
return (int)pi.GetValue(c, null);
Additionally, when I modify the description height with my sample code (which does have a typo as mentioned in another comment), I do it as the last task in Form_Load(). That way, I can be sure that the control actually is initialized properly.
|
|
|
|
 |
|
 |
If I may quote my original message:
Setting a breakpoint and running "?pi.GetValue(c, null)" after pi has been set causes "An exception has been caused by the target of an invocation."
It wasn't the Convert.ToInt32 that threw the exception I saw, it was ?pi.GetValue(c, null).
Cheers
Graham
Edit: Also, I'm trying to run the code to get the current height of the description pane from my FormClosing event, if that makes a difference.
|
|
|
|
 |
|
 |
Looks like pi is invalid.
|
|
|
|
 |
|
 |
I'll look at it again when I get time, but off the top of my head, pi was valid when Control.ControlCollection cc = (Control.ControlCollection)pi.GetValue(grid, null); ran.
Thanks for taking the time to have a look, though. I really don't want people to write my code for me; I was just trying to find out if the people here felt that my method should work to get the height of the Description area of my PropertyGrid. And from what you're saying, it definitely should in principle, so there must be something else wrong with my code.
Thanks again!
Graham
|
|
|
|
 |
|
 |
On the position:
System.Reflection.FieldInfo fi = ct.BaseType.GetField("userSized",
System.Reflection.BindingFlags.Instance,
System.Reflection.BindingFlags.NonPublic);
You have to set an "or" for the BindingFlags, then the error goes away:
System.Reflection.FieldInfo fi = ct.BaseType.GetField("userSized",
System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
But it is very cool work, thx to Geno Carman
Cheers Helly
|
|
|
|
 |
|
 |
You, sir, are correct! Thank you for finding it. Now I hope I can figure out how to edit the article.
|
|
|
|
 |
|
 |
Hi,I am chinese.
Using your code,there throw exception.
pi.GetValue(grid, null)== null always!
My Os is windows Xp,and .net is 2005
|
|
|
|
 |
|
 |
Verify you passed in a valid GridControl argument?
ResizeDescriptionArea(ref grid1, 6);
|
|
|
|
 |
|
 |
I think I passed the right argument. I post the code and you try it.The simple code as follow:
The Form2.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace wpropertygrid
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private bool ResizeDescriptionArea(ref PropertyGrid grid, int nNumLines)
{
try
{
System.Reflection.PropertyInfo pi = grid.GetType().GetProperty("Controls");
Control.ControlCollection cc = (ControlCollection)pi.GetValue(grid, null);
foreach (Control c in cc)
{
Type ct = c.GetType();
string sName = ct.Name;
if (sName == "DocComment")
{
pi = ct.GetProperty("Lines");
pi.SetValue(c, nNumLines, null);
System.Reflection.FieldInfo fi = ct.BaseType.GetField("userSized",
System.Reflection.BindingFlags.NonPublic);
fi.SetValue(c, true);
}
}
return true;
}
catch (Exception error)
{
#if(DEBUG)
MessageBox.Show(error.Message, "ResizeDescriptionArea()");
#endif
return false;
}
}
private void button1_Click(object sender, EventArgs e)
{
ResizeDescriptionArea(ref propertyGrid1, 20);
}
}
}
The Form2.Designer.cs
namespace wpropertygrid
{
partial class Form2
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.propertyGrid1 = new System.Windows.Forms.PropertyGrid();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// propertyGrid1
//
this.propertyGrid1.Location = new System.Drawing.Point(36, 42);
this.propertyGrid1.Name = "propertyGrid1";
this.propertyGrid1.Size = new System.Drawing.Size(130, 130);
this.propertyGrid1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(189, 73);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.button1);
this.Controls.Add(this.propertyGrid1);
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.PropertyGrid propertyGrid1;
private System.Windows.Forms.Button button1;
}
}
The Program.cs:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace wpropertygrid
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form2());
}
}
}
|
|
|
|
 |
|
 |
I dont see what the problems is.
You need to figure out why pi.GetValue(grid, null) is returning NULL.
|
|
|
|
 |
|
 |
the code:
"Control.ControlCollection cc = (ControlCollection)pi.GetValue(grid, null);"will throw exception. I used the "Object obj = pi.GetValue(grid,null);"instead,and found the obj== null always.
Do you run the code on your Pc?It works OK ?
In fact,I have a question about System.Reflection.PropertyInfo pi = grid.GetType().GetProperty("Controls");.The property named "Controls" in PropertyGrid can't be found and GetProperty is used to Search for the public property with the specified name,but the statement will work.Why? hope for your reply.
Thank you~
|
|
|
|
 |
|
 |
Works on every the WinXP machine I have tried.
|
|
|
|
 |
|
 |
I try it on another pc.The exception"Unable to cast object of type'ControlCollection'to type 'ControlCollection; "appears again!
In fact,I have a question about System.Reflection.PropertyInfo pi = grid.GetType().GetProperty("Controls");.The property named "Controls" in PropertyGrid can't be found and GetProperty is used to Search for the public property with the specified name,but the statement will work.Why? hope for your reply.
|
|
|
|
 |
|
 |
try:
System.Reflection.PropertyInfo pi = grid.GetType().GetProperty("Controls");
System.Windows.Forms.Control.ControlCollection cc = (System.Windows.Forms.Control.ControlCollection)pi.GetValue(grid, null);
|
|
|
|
 |
|
 |
Hi Geno :
I had try your code in .net Framework with vs2005.
it dose not work until I adjust the description area manually.
I'm now trying to make it work,just for your notice
|
|
|
|
 |
|
 |
How do you adjust it manually?
|
|
|
|
 |
|
 |
well,when the program is running,I move the cursor to the splitter of the propertygride then drag it to adjust the description area.
|
|
|
|
 |
|
 |
Oh. I was hoping to hear how to do it at design time.
|
|
|
|
 |
|
 |
Swap the two assignments. Set the "userSized" field to 'true' first and then set the "Lines" property. This works in VS2005.
|
|
|
|
 |
|
|
 |