Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio keeps inserting absurd default property value in designer file

i have a simple usercontrol with following properties:

public partial class RichTextEditorControl : UserControl
{

    public string EditorText
    {
        get { return richTextBox1.Rtf; }
        set { richTextBox1.Rtf = value; }
    }

    public string EditorPlainText
    {
        get { return richTextBox1.Text; }
        set { richTextBox1.Text = value; }
    }
}

Now whenever I EDIT a form which contains this control, VS fills its designer file with the following code line, and then throws a designer error :

this.richTextEditorControl1.EditorPlainText = 
   global::Project.Resources.MyResources_de_DE.SomeString;

Now I don't know where it gets this value from ??? I've searched entire solution, and nowhere there's mention of this var, except for 1 file, where it's needed ...

Moreover, the code VS writes, has an error in it ?! It doesn't compile ... The only thing I can do is edit the designer file, but the next time I have to edit the form with the designer, the same error happens again ...

 Error  25  The type or namespace name 'MyResources_de_DE' does not exist   
 in the namespace 'Project.Resources' (are you missing an assembly reference?)  

Where on earth is VS getting this value from ??

like image 604
Run CMD Avatar asked Dec 15 '25 09:12

Run CMD


2 Answers

You cannot meaningfully edit the RTF in the designer. I can only guess that you've used a custom designer and something is wrong with it to get a value assigned like that. Or you somehow managed to really confuse the Windows Forms designer.

Anyhoo, you cannot afford to have the EditorText property serialized at all. That conflicts badly with the EditorPlainText property. They both set the RTF of the rich text box. One with formatting, the other without. You'll now get random failure, depending on which property will be set last. Force the designer to not serialize the value like this:

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public string EditorText {
      // etc..
    }

You'll have to remove the control from your forms and add it back after this change.

like image 99
Hans Passant Avatar answered Dec 16 '25 21:12

Hans Passant


In this case you would think that the designer would compare the value you set in the usercontrol's property panel (when placed in the consumer) to the value that the property has when the object is first created, and only produce a line of code in the usercontrol consumer's designer file if the values are different. Not so. You need to throw on an attribute like this right in front of the declaration:

 <System.ComponentModel.DefaultValue("")> public string EditorPlainText ...

Delete the errant line from the designer and try rebuilding. This will stop the problem.

Sometimes you'll need to do other fancy stuff to get this to fit, e.g., if your property is of type 'control' you would have to use this to get it to tell the designer not to generate extra (useless) code if the property were usually Null:

 <System.ComponentModel.DefaultValue(GetType(Control), Nothing)> etc...

You should do this even if you don't care, in case you are refactoring and removing a no-longer or little used method (which you can of course only do if you control all the consuming code) - there are fewer lines to delete from designer files saying things like 'MyUserControl.UselessProperty=Nothing'.

Good luck.

like image 21
FastAl Avatar answered Dec 16 '25 22:12

FastAl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!