❔ Winforms Property from BaseClass Form not getting transferred to derived Form
Hi. Don't know if you guys can help an amateur. I know most of you guys don't like winforms, but...
When I bring up the form "Admin" in designer view, it opens with a new Guid every time (obs using the base class Guid), unless I manually set Guid myself and then it adds it in Admin.Designer.cs.this.Guid="(whatever)" - this is missing until I manually add it. Is there any inheritance trick I can pull in the base class to force VS to create the this.Guid in Admin.Desginer.cs. Been messing around with this all day now! 🧱
25 Replies
public static Guid GUID { get; private set; } = Guid.NewGuid();
You access it by:
BaseForm.GUID
everywhere.The issue is VS auto generates the .Designer.cs
you're adding the GUID property yourself though?
Thus, querying if there's any way to force it to generate .Guid in .Designer.cs in all instances, not just in the user entering it manually in Designer view
no, I want it auto generated.
if you need it to reflect on a form load
just add the startup code somewhere in the Window.Loaded() equivalent event
You want the same guid or each form to have different guids?
each form instance i mean
I was hoping to do it all in the Base class
sorry yes - different guids for different windows
One easy way would be
to add the event-compatible method to your base class.
then definitely don't static
anyway, continuing
The only thing you'd have to do per form is
ok - thank you for your help
Yeah you can add code to constructor i think
did you check if the designer is resetting the constructor code?
The baseclass or .designer ctor?
baseclass
baseclass ctor functions should carry over afaik
if not just add
: base()
to the derived class ctor and it should workI think I did try setting Guid to new in base ctor, but will try again - 1 min
again - no go as this is essentially the .designer.cs code - autogenerated
the other way would be to add the GUID assignment to whatever creates your forms.
yeah winforms is very convoluted and full of bad code practices.
I know, I know...
How are you creating these forms?
Have you worked in winforms before?
yes
I mean to say are the forms being created by binding or you have a method to create them
You can do this:
seems like the most relevant in your case. I dont want to be hacky with designer generated stuff either.
you can have the guid assignment as a method in your base class but you'd still have to call that method after creating the form
Forgive me - I'm an amateur. Is there a way to specify code in the Base class and have this populate verbatim into the Derived class? Then I could just code (pseudo) [if guid is nothing then guid = guid.new]
in the Form_Load auto populated method
(Adding forms via "Add New" dialog! and then just updating inheritance to BaseForm from Form
Wait so this guid assignment is during development or during runtime?
I'm wanting it during development
You may have to look at source generators then.
I'm not great at that stuff.
you could add an attribute like
[AutoGuid]
to your class
and then have a source generator generate that GUID property for you in a generated partial class fragment.Oooo - now that sounds interesting
Also, use incremental source generators (v2). the v1 are legacy
The Community toolkit for mvvm works in a similar way.
Take a look at
[ObservableObject]
or [ObservableProperty]
source code for inspirationThanks Glitch - you've given me another starting point.
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.