72771

Property value of base class not saved by designer

<h3>Question</h3>

I have added a form to my WinForms project in Visual Studio Express 2013 that I want to use as a base form for other forms. Say I put a button on this form and I want to have a property that makes this button visible or invisible.

<pre class="lang-vb prettyprint-override">Imports System.ComponentModel Public Class MyForm <DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> Public Property ButtonVisible As Boolean Get Return Button1.Visible End Get Set(value As Boolean) Button1.Visible = value End Set End Property End Class

The designer for this file was not changed. I just added a button to a new form.

When I now create a new form that inherits this class I can change the value of this property and at design time the button indeed becomes visible or invisible. However when I compile the project the property is resetted back to the default value. When I examine the designer file of the derived form I see, that the changed property value is not added to it and therefore vanishes into thin air. When I add ButtonVisible = False manually to the designer file it works and stays there, so I guess the problem lies in the fact, that the Designer does not add the line to the designer file.

This is the designer file of the derived form, after I changed the property value in the designer:

<pre class="lang-vb prettyprint-override"><Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _ Partial Class Form2 Inherits MyForm 'Das Formular überschreibt den Löschvorgang, um die Komponentenliste zu bereinigen. <System.Diagnostics.DebuggerNonUserCode()> _ Protected Overrides Sub Dispose(ByVal disposing As Boolean) Try If disposing AndAlso components IsNot Nothing Then components.Dispose() End If Finally MyBase.Dispose(disposing) End Try End Sub 'Wird vom Windows Form-Designer benötigt. Private components As System.ComponentModel.IContainer 'Hinweis: Die folgende Prozedur ist für den Windows Form-Designer erforderlich. 'Das Bearbeiten ist mit dem Windows Form-Designer möglich. 'Das Bearbeiten mit dem Code-Editor ist nicht möglich. <System.Diagnostics.DebuggerStepThrough()> _ Private Sub InitializeComponent() Me.SuspendLayout() ' 'Form2 ' Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(284, 261) Me.Name = "Form2" Me.Text = "Form2" Me.ResumeLayout(False) End Sub End Class

As you can see in the code at the top, I already tried to solve the problem by testing the different values for DesignerSerializationVisible, but they seem to have no effect.

Is there anything I overlooked? How should I add properties that change controls in the underlying base class?
Both C# or VB.NET answers are very much appreciated, whatever you like best.


<h3>Answer1:</h3>

First, it seems you misunderstood the DesignerSerializationVisibility.Content value of DesignerSerializationVisibility attribute.

You need to use the DesignerSerializationVisibility.Visible value to save the <em>value</em> of a property. See this related tread: Properties won't get serialized into the .designer.cs file

Then you can't refer directly to the Button.Visible property in your custom property. The visibility state of the button will be reset to its default value (True) each time you open the inherited form. So your custom property will always show True when the form will be loaded.

You need to

<ul><li>store the state in a variable </li> <li>adjust the button visibility after InitializeComponent method and when the value of your property changes.</li> </ul><hr /><pre class="lang-vb prettyprint-override">Public Class MyForm Public Sub New() InitializeComponent() Me.Button1.Visible = _buttonVisibility End Sub Private _buttonVisibility As Boolean = True <DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)> Public Property ButtonVisible As Boolean Get Return _buttonVisibility End Get Set(value As Boolean) _buttonVisibility = value Button1.Visible = value End Set End Property End Class

来源:https://stackoverflow.com/questions/26323837/property-value-of-base-class-not-saved-by-designer

Recommend

  • Mac command line tools 11.4 no longer has svn
  • Linux-firewall防火墙
  • Mongodb : Why convert string date to ISOdate if comparison operators work?
  • How can I generate DDL scripts from Entity Framework 4.3 Code-First Model?
  • suppress/redirect stderr when calling python webrowser
  • splitting a multiple FASTA file into separate files keeping their original names
  • Fill in missing date and fill with the data above
  • Angular sum dynamic data from *ngFor loop to get total, subtotal and vat value (%16)
  • MVC failed to make a json string in view for a variable in javascript
  • Facebook popup remember when user likes
  • SherlockActivity cannot be resolved to a type in android
  • Program defensively against ODAC instantclient
  • How to set and check a session after login?
  • Zero downtime deployment Nodejs application
  • Does anyone know about Kephas?
  • Adding items to an already existing jlist from another class
  • 3 transitions, pausetime between transitions
  • Salesforce consuming XML and display data in Visualforce report
  • PHP Append to Excel file
  • How to smoothly connect two signals in matlab [closed]
  • “proxy_pass” cannot have URI part in location given by regular expression
  • Why am I getting an Argument exception when creating event handler dynamically?
  • Homomorphic filtering in OpenCV
  • Disabling swipe gesture in Windows Phone 8.1 pivot control
  • How to put an object in the air?
  • Query regarding com.jcraft.jsch.JSchException: UnknownHostKey: x.y.com. DSA key fingerprint is “ac:e
  • Sample deviceQuery cuda program
  • Android studio import problems. (Apktool)
  • Facebook Error (#200) The user hasn't authorized the application to perform this action (PHP)
  • Building JavaFX 2.0 App on Mac, deploying on Windows
  • How to load dynamic images in custom ListView
  • What is the difference between dynamically creating a script tag and statically embed a script tag?
  • Another “Cannot make static reference…” Question
  • Spring Boot fails to start
  • Google App Engine backend servlet not responding
  • Jersey serializes character value to ASCII equivalent numeric string
  • How to mutate multiple variables without repeating codes?