Click here to Skip to main content
Licence CPOL
First Posted 21 Sep 2004
Views 64,485
Bookmarked 23 times

PropertyGrid Control in WinForms

By | 20 Oct 2005 | Article
PropertyGrid control in WinForms.

Disclaimer:

This source code is written and maintained by an individual, not a company. It has been provided for free to the Microsoft .NET user community with the expectation that users will take whatever action necessary to get the code to operate according to their needs. This includes debugging and enhancements. The source code is provided "as is" and there are neither warrantees to the quality of the work nor any expressed or implied agreements that the programmer will release any updates. The programmer will release updates and provide any support at his own discretion.

Introduction:

The article is mainly to show how to use the PropertyGrid control in WinForms. Using this PropertyGrid control, we can change the property of other controls while at runtime.

This article is to give a simple example in C# showing how easy it is to use the PropertyGrid control in WinForms. This is developed using Visual Studio .NET final release. PropertyGrid provides a user interface for browsing the properties of an object.

Adding "PropertyGrid" to VS.NET Toolbox:

Select ‘Tools’ menu and Select ‘Add/Remove Tool Box Items’. Then the ‘Customize Toolbox’ will appear in the screen. Select ‘.NET Framework Component’ tab and then select the ‘PropetyGrid’ checkboxes and hit OK. Now you can drag and drop the ‘PropetyGrid’ into the Windows Form.

Example:

In the example, I used the TextBox control for explanation.

public Form1()
{
    // The initial constructor code goes here.

    PropertyGrid propertyGrid1 = new PropertyGrid();
    propertyGrid1.CommandsVisibleIfAvailable = true;
    propertyGrid1.Location = new Point(10, 20);
    propertyGrid1.Size = new System.Drawing.Size(400, 300);
    propertyGrid1.TabIndex = 1;
    propertyGrid1.Text = "Property Grid";

    this.Controls.Add(propertyGrid1);
    propertyGrid1.SelectedObject = textBox1;
}

The above code is to add the PropertyGrid control to your WinForm. And the code will select the TextBox as the selected component. The information displayed in the grid is a snapshot of the properties at the time the object is assigned. If a property value of the object specified by the SelectedObject is changed in code at run time, the new value is not displayed until an action is taken in the grid that causes the grid to refresh.

  • PropertyGrid.SelectedObject

    Gets or sets the object for which the grid displays properties.

  • public object SelectedObject {get; set;}

    The SelectedObject property sets a single object into the grid to be browsed. If multiple objects are being browsed, this property returns the first one in the list. If no objects are selected, a null reference is returned.

The below mentioned is the PropertyGrid class signature.

public class PropertyGrid : ContainerControl

Any public static members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

To exit from the application, use the following code:

private void Exit_Click(object sender, System.EventArgs e)
{
    this.Dispose();
}

For more help and queries, please mail me at vivekthangaswamy@rediffmail.com.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

vivekthangaswamy

Architect

India India

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 PinmvpOriginalGriff21:24 8 May '11  
GeneralMy vote of 1 PinmemberDavid Catriel11:19 25 Apr '11  
GeneralMy vote of 1 PinmemberSteve Dunn9:49 18 Feb '10  
GeneralMy vote of 1 PinmemberGDMonkey23:58 28 Oct '09  
Questionwow! and you're an architect? this is the best you could come up with? Pinmemberjeffsaremi6:08 12 Jul '09  
GeneralMy vote of 1 Pinmemberjeffsaremi6:07 12 Jul '09  
GeneralUnethical PinmemberBubba Ole10:32 18 Jun '09  
GeneralMy vote of 1 PinmemberElTchoupi2:37 31 Mar '09  
GeneralMoron PinmemberNaveenChander23:51 20 Oct '08  
GeneralVery poor article Pinmemberrajeshkanakarajan14:26 13 Apr '08  
GeneralRe: Very poor article PinmemberDaeMan200122:19 25 May '08  
Very poor comment. Why did you post it?
 
As to the article, thank you - I didn't realise the PropertyGrid control was included in VS the whole time D'Oh! | :doh:
GeneralRe: Very poor Comments PinmemberAGC Blogger4:52 21 Sep '08  
GeneralRe: Very poor article PinmemberJhollman2:46 16 Nov '11  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120529.1 | Last Updated 20 Oct 2005
Article Copyright 2004 by vivekthangaswamy
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid