Programmatically resizing a column in a propertyGrid

211 views
Skip to first unread message

Chandler Grissom

unread,
Oct 17, 2006, 3:36:46 PM10/17/06
to
A property grid has two columns which can be sized by the user, but I'm
foxed as to how to size them at load time.

I need the left column to be about 25% of the width and the right column to
be the remaining 75%.

Can this be done by setting a property in the property grid?

Thanks!


Nico

unread,
Oct 18, 2006, 9:21:50 AM10/18/06
to
Hi,

There is unfortunately no way to know the width of the label and value
columns and no way to set it. I found a hack that works at init time
when we know that the 2 columns have the same width. This is really a
hack ;)
Here is what I did:

In my Form_Load:

ActiveControl = propertyGrid2;
propertyGrid.Controls[2].GotFocus += new EventHandler(Form1_GotFocus);

The Form1_GotFocus code:

void Form1_GotFocus(object sender, EventArgs e)
{
int count = (int)((propertyGrid.Controls[2].Width - 16 -
SystemInformation.VerticalScrollBarWidth) * 0.25 / 3);
keybd_event(0x11 /*VK_CONTROL*/, 0x9d, 0, UIntPtr.Zero); // Ctrl
Press
for (int i = 0; i < count; i++)
{
keybd_event(0x25 /*VK_LEFT*/, 0x9e, 0, UIntPtr.Zero); // Left
arrow Press
keybd_event(0x25 /*VK_LEFT*/, 0x9e, 0x0002/*KEYEVENTF_KEYUP*/,
UIntPtr.Zero); // Left arrow Press Release
}
keybd_event(0x11 /*VK_CONTROL*/, 0x9d, 0x0002/*KEYEVENTF_KEYUP*/,
UIntPtr.Zero); // Ctrl Release
}

The keybd_event is defined as:

[DllImport("user32.dll")]
[CLSCompliant(false)]
public static extern void keybd_event(byte bVk, byte bScan, uint
dwFlags, UIntPtr dwExtraInfo);

I assumed there was a width of 16 for the first grayed column and that
the vertical scrollbar was here. I also harcoded your 25% for the label
column.

This hack works on the fact that if a user presses CTRL + left or right
arrow, the splitter bar will move 3 pixels in either directions.

Hope this helps and that you will be able to use this solution.

Best regards,

Nicolas Cadilhac @ VisualHint (http://www.visualhint.com)
Home of Smart PropertyGrid for .Net and MFC

Nico

unread,
Oct 18, 2006, 9:25:38 AM10/18/06
to
Oh, I just forgot to give some credits to the CodeProject article that
helped me find quicker the correct keybd_event parameters :
http://www.codeproject.com/system/keyboard.asp

Best regards,

Nicolas Cadilhac @ VisualHint (http://www.visualhint.com)
Home of Smart PropertyGrid for .Net and MFC

Nico

unread,
Oct 18, 2006, 9:28:04 AM10/18/06
to
And at last, there is a small typo in my code but I guess you fixed it.
propertyGrid2 should read propertyGrid (the name of your PropertyGrid
variable in fact).

Best regards,

Nicolas Cadilhac @ VisualHint (http://www.visualhint.com)
Home of Smart PropertyGrid for .Net and MFC

Chandler Grissom

unread,
Oct 18, 2006, 6:07:14 PM10/18/06
to
"Nico" <cadi...@gmail.com> wrote in message
news:1161178084.8...@m7g2000cwm.googlegroups.com...

> And at last, there is a small typo in my code but I guess you fixed it.
> propertyGrid2 should read propertyGrid (the name of your PropertyGrid
> variable in fact).
>
> Best regards,
>
> Nicolas Cadilhac @ VisualHint (http://www.visualhint.com)
> Home of Smart PropertyGrid for .Net and MFC
>
>
Certainly grateful!

I'll get to work on your snippet right away :)

Thanks!


Reply all
Reply to author
Forward
0 new messages