简体   繁体   中英

Visual Studio 2010 hardcoding unwanted values in Designer.cs

I have a user control on a form in Visual Studio, which is a custom date entry control (called for the purposes of discussion CustomDate ). This control contains a property called CompareDate which is of type DateTime . This gets/sets a value to a private variable called compareDate . (Not a control or anything just a variable used for internal logic)

I place this control on a form CustomerForm and call it uctCustomDate .

For some reason any change ever made to CustomerForm causes a line to be automatically inserted into CustomerForm.Designer.cs hard-coding the value of uctCustomDate.CompareDate to today's date!

Is there a way to avoid this? I can just delete the line and build the application but it means I have to remember to do this every time I make a change to this form. Anything I should look out for (eg that another developer might have put in)?

Here's some code if it helps...

in CompareDate.cs

private DateTime compareDate = DateTime.Today;   // BTW it happens whether or not I initialise to DateTime.Today

.
.
.

public DateTime CompareDate 
{
    get { return compareDate; }
    set { compareDate = value; }
}

And the code automatically inserted to CustomerForm.Designer.cs whenever I make a change on the CustomerForm form design

// 
// uctCustomDate
// 
this.uctCustomDate.CompareDate = new System.DateTime(2015, 6, 30, 0, 0, 0, 0);

if you use BrowsableAttribute on CompareDate property, it will not be displayed in Properties window in form designer

if you use DesignerSerializationVisibilityAttribute with visibility = Hidden on CompareDate , property will not be serialized in CustomerForm.Designer.cs

[DesignerSerializationVisibilityAttribute]

existing lines this.uctCustomDate.CompareDate = ... will not be removed automatically from Designers files

[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public DateTime CompareDate 
{
    get { return compareDate; }
    set { compareDate = value; }
}

Have you tried making compareDate a nullable Datetime ? This way the designer should not initialize it by himself.

DateTime? compareDate;


public DateTime? CompareDate 
{
    get { return compareDate; }
    set { compareDate = value; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM