Windows Develop Bookmark and Share   
 index > Windows Forms General > Making property in property grid read-only.
 

Making property in property grid read-only.

I am using a property grid which i have set the selected object to an instance of System.Data.OleDb.OleDbConnectionStringBuilder and wish to make the property "Provider" read-only. Ive tried all sorts of methods firstly i triedinheriting OleDbConnectionStringBuilderbut is not inheritable so tried creating some kind of property wrapper to look for "Provider" then when it does find it try to change its attributes to read-only, but got stuck at that point.

I hope someone can help as it seems a simple problem thats just getting more and more complex the more i try.

Thanks

eq2k  Thursday, October 11, 2007 9:14 AM

However, to make it better, you should also block the ContextMenuStrip when right click in the TextBox when focus is in the item you want to be read only, to do this, you have to Implement the IMessageFilter interface to block the right click event, something like this:

Code Block

partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)

{

this.myPropertyGrid1.SelectedObject = new test();

Application.AddMessageFilter(this.myPropertyGrid1);

}

}

class test

{

private string propToBeReadOnly = "aaa";

public string PropToBeReadOnly

{

get { return propToBeReadOnly; }

set { propToBeReadOnly = value; }

}

private string something;

public string Something

{

get { return something; }

set { something = value; }

}

}

class myPropertyGrid : PropertyGrid,IMessageFilter

{

protected override bool ProcessDialogKey(Keys keyData)

{

if (this.SelectedGridItem.PropertyDescriptor.DisplayName

== "PropToBeReadOnly")

{

return true;

}

return base.ProcessDialogKey(keyData);

}

#region public bool PreFilterMessage(ref Message m)

{

if (m.Msg == 0x0204) //WM_RBUTTONDOWN

{

if (this.SelectedGridItem.PropertyDescriptor.DisplayName

== "PropToBeReadOnly")

{

return true;

}

}

return false;

}

#endregion

}

Use the ReadOnly attribute. For example:

private int mVark;

[ReadOnly(true)]
public int Aardvark {
get { return mVark; }
set { mVark = value; }
}
nobugz  Thursday, October 11, 2007 10:26 AM

Unfortunately I cant use the ReadOnly attribute on the provider property as

OleDbConnectionStringBuilder is already compiled and part of the .NET framework. so no way of altering the property unless some genious way of using reflection to change it top read-only.

eq2k  Thursday, October 11, 2007 4:46 PM

A trick way would be to block the keystrokes in the PropertyGrid when focus is in the item which you want to be read only, to do this, you should derive from the PropertyGrid control, override its ProcessDialogKey event to return true if the current selected item is the one need to be read only.
Something like this:


Code Block

partial class Form1 : Form

public Form1()

private void Form1_Load(object sender, EventArgs e)

this.myPropertyGrid1.SelectedObject = new test();

class test

private string propToBeReadOnly = "aaa";

public string PropToBeReadOnly

get { return propToBeReadOnly; }

set { propToBeReadOnly = value; }

private string something;

public string Something

get { return something; }

set { something = value; }

class myPropertyGrid : PropertyGrid

protected override bool ProcessDialogKey(Keys keyData)

if (this.SelectedGridItem.PropertyDescriptor.DisplayName
== "PropToBeReadOnly")

return true;

return base.ProcessDialogKey(keyData);

However, to make it better, you should also block the ContextMenuStrip when right click in the TextBox when focus is in the item you want to be read only, to do this, you have to Implement the IMessageFilter interface to block the right click event, something like this:

Code Block

partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)

{

this.myPropertyGrid1.SelectedObject = new test();

Application.AddMessageFilter(this.myPropertyGrid1);

}

}

class test

{

private string propToBeReadOnly = "aaa";

public string PropToBeReadOnly

{

get { return propToBeReadOnly; }

set { propToBeReadOnly = value; }

}

private string something;

public string Something

{

get { return something; }

set { something = value; }

}

}

class myPropertyGrid : PropertyGrid,IMessageFilter

{

protected override bool ProcessDialogKey(Keys keyData)

{

if (this.SelectedGridItem.PropertyDescriptor.DisplayName

== "PropToBeReadOnly")

{

return true;

}

return base.ProcessDialogKey(keyData);

}

#region public bool PreFilterMessage(ref Message m)

{

if (m.Msg == 0x0204) //WM_RBUTTONDOWN

{

if (this.SelectedGridItem.PropertyDescriptor.DisplayName

== "PropToBeReadOnly")

{

return true;

}

}

return false;

}

#endregion

}

Hello,
Perhaps you can add dynamically the property?

PropertySpec ps = ThePropertySpecOf( "PropToBeReadOnly" );

ps.Attributes = new Attribute[1];
ps.Attributes[0] = new ReadOnlyAttribute(true);

Llorenç Corbey  Wednesday, September 23, 2009 1:49 PM

You can use google to search for other answers

Custom Search

More Threads

• VS Custom control
• using scrolling in user control
• Vista's "Explorer" Style and the DataGridView
• Paramater Value of a Constructor - Windows Form
• Overriding ToString in a ListBox object
• Checkbox in datagrid?
• Keep cursor on richtextbox when we leave the control
• MDI Validating
• FontFamily.Families does not return fonts with same typeface name
• Creating a queue of NotifyIcon.BalloonTips