473,569 Members | 2,916 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

unexpected PropertyGrid behavior on derived WinForm

Hi all,

To repro the problem I am having:

1. Create a class that derives from System.Windows. Forms.Form
2. Add some controls such as buttons and labels (wherever you want). The default values must be set appropriately.
3. Add some public properties to the form that directly adjust the exposed private Control properties:

public class BaseForm : Form
{
[Category("Appea rance"), DefaultValue(tr ue)]
public bool ButtonVisible { get { return btn.Visible; } set { btn.Visible = value; } }

[Category("Appea rance"), DefaultValue(tr ue)]
public bool LabelVisible { get { return lbl.Visible; } set { lbl.Visible = value; } }

// initialized by the designer:
private Button btn;
private Label lbl;

// I removed some stuff for clarity
}

4. Create a class that derives from the newly created class above
(I used VS.NET 2003 and selected "Add New Item --> Inherited Form")

public class DerivedForm : BaseForm
{
// I removed some stuff for clarity

private System.Componen tModel.IContain er components = null;

#region Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
components = new System.Componen tModel.Containe r();
}
#endregion
}

5. In the designer, set focus to the "DerivedFor m" and try to set one of the properties such as ButtonVisible to "false"
6. Look in the InitializeCompo nent method. This is what mine says:

{
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(292, 269);
this.LabelVisib le = true;
this.Name = "DerivedFor m";
}

Ok, so the default value of ButtonVisible is "true". When I set it to false, the designer serialized LabelVisible = true along with
some other property values. But, it didn't serialize ButtonVisible at all.

At this point, the designer reflects the correct change, although the code-behind doesn't. Closing and reloading the class in the
designer retains those changes I made, yet the code-behind is still incorrect so the Button remains visible at runtime, yet
invisible in the designer.

Any changes to the base class reset all changes that I made to the designer.

The only work-around I found is to not use the designer and set those property values in the constructor, after the call to
InitializeCompo nent.

Just FYI, here are the things I've tried that didn't have any effect.
I wrote the following for all properties that I declared:

[Category("Appea rance")] // no default value specified
public bool ButtonVisible { get { return btn.Visible; } set { btn.Visible = value; } }

private bool ShouldSerialize ButtonVisible()
{
return !btn.Visible;
}

private void ResetButtonVisi ble()
{
btn.Visible = true;
}

public event EventHandler ButtonVisibleCh anged
{
add { btn.VisibleChan ged+= value; }
remove { btn.VisibleChan ged-= value; }
}
Also tried the following, and none had any effect:

I tried switching around the return values by removing the negation (!).
I tried adding DefaultValue(fa lse) just to see what would happen.
I also tried removing all "default" serialization code and Attributes.
I tried adding NotifyParentPro perty(true) and (false).
I tried a few combinations of all of the above.

Why would this happen and how can I fix it?

I'd like to avoid making a custom Designer if possible. It seems like DefaultValueAtt ribute should have done the trick.

--
Dave Sexton
dave@www..jwaon line..com
-----------------------------------------------------------------------
Jul 22 '05 #1
0 1218

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
6435
by: Terry | last post by:
I created a collection derived from CollectionBase that is just made up of strings, called "StringList". I have another object that has as a member one of these StringList. If I assign that object to a PropertyGrid, click on the collection editor "..." and try to add a new string, I get an error message "Constructor on type System.String...
4
18811
by: phcmi | last post by:
I have a PropertyGrid question. My task is to replace a legacy dialog box presentation with a modern one. The dialog itself allows the user to set configuration settings in our application, so it seems to me that a PropertyGrid is a perfect modern replacement. I won't bore you with the details with all the controls on the dialog since I'm...
2
2129
by: =?Utf-8?B?TWF1cmljZU0=?= | last post by:
Hi all, how can I hide a property in a derived class in a propertygrid. Let's say I have a classX and a derived classY. A property in classX will be visible in the propertygrid but the same property in the derived classY will not be visible in the propertygrid. Does anyone know how to do this ?
2
1925
by: =?Utf-8?B?Sm9obiBPbGJlcnQ=?= | last post by:
Is there a way to pass a class to the SelectedObject of PropertyGrid and have it only show the properties in the child class and not those in the base class's that it is derived from? Thanks. -- John Olbert
0
2434
by: JerryShaw | last post by:
Problem: create a new winform app in C# VS2008. Drop a PropertyGrid component, Drop a DataSet (untyped) Set the PropertyGrid.SelectedObject to the DataSet. Run the application, and try to select the table editor. Can not bring up the tables collection editor Drop a DataGridView, set the PropertyGrid.SelectedObject to the DataGridView. Can...
0
7605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8118
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7665
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6277
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5501
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2105
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.