C#
C#

help

Root Question Message

stigzler
stigzler12/18/2022
❔ 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...

namespace MyApp.Common
{
    public partial class BaseForm : Form
    {
        private Guid _guid = Guid.NewGuid();
        public Guid GUID
        {
            get { return _guid; }
            set { _guid = value; }
        }

    }
}

namespace MyApp.Presentation
{
    public partial class Admin : BaseForm
    {

    }
}


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! 🧱
RiA
RiA12/18/2022
public static Guid GUID { get; private set; } = Guid.NewGuid();

You access it by:

BaseForm.GUID everywhere.
stigzler
stigzler12/18/2022
The issue is VS auto generates the .Designer.cs
RiA
RiA12/18/2022
you're adding the GUID property yourself though?
stigzler
stigzler12/18/2022
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
stigzler
stigzler12/18/2022
no, I want it auto generated.
RiA
RiA12/18/2022
if you need it to reflect on a form load
just add the startup code somewhere in the Window.Loaded() equivalent event
RiA
RiA12/18/2022
You want the same guid or each form to have different guids?
RiA
RiA12/18/2022
each form instance i mean
stigzler
stigzler12/18/2022
I was hoping to do it all in the Base class
stigzler
stigzler12/18/2022
sorry yes - different guids for different windows
RiA
RiA12/18/2022
One easy way would be
RiA
RiA12/18/2022
to add the event-compatible method to your base class.
RiA
RiA12/18/2022
then definitely don't static
RiA
RiA12/18/2022
anyway, continuing
RiA
RiA12/18/2022
The only thing you'd have to do per form is
stigzler
stigzler12/18/2022
ok - thank you for your help
RiA
RiA12/18/2022
Yeah you can add code to constructor i think
RiA
RiA12/18/2022
did you check if the designer is resetting the constructor code?
stigzler
stigzler12/18/2022
The baseclass or .designer ctor?
RiA
RiA12/18/2022
baseclass
RiA
RiA12/18/2022
baseclass ctor functions should carry over afaik
RiA
RiA12/18/2022
if not just add : base() to the derived class ctor and it should work
stigzler
stigzler12/18/2022
I think I did try setting Guid to new in base ctor, but will try again - 1 min
stigzler
stigzler12/18/2022
again - no go as this is essentially the .designer.cs code - autogenerated
RiA
RiA12/18/2022
the other way would be to add the GUID assignment to whatever creates your forms.
RiA
RiA12/18/2022
yeah winforms is very convoluted and full of bad code practices.
stigzler
stigzler12/18/2022
I know, I know...
RiA
RiA12/18/2022
How are you creating these forms?
stigzler
stigzler12/18/2022
Have you worked in winforms before?
RiA
RiA12/18/2022
yes
RiA
RiA12/18/2022
I mean to say are the forms being created by binding or you have a method to create them
RiA
RiA12/18/2022
You can do this:
BaseForm myform = new();
myform.GUID = Guid.NewGuid();
myform.Show();
RiA
RiA12/18/2022
seems like the most relevant in your case. I dont want to be hacky with designer generated stuff either.
RiA
RiA12/18/2022
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
stigzler
stigzler12/18/2022
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]
stigzler
stigzler12/18/2022
in the Form_Load auto populated method
stigzler
stigzler12/18/2022
(Adding forms via "Add New" dialog! and then just updating inheritance to BaseForm from Form
RiA
RiA12/18/2022
Wait so this guid assignment is during development or during runtime?
stigzler
stigzler12/18/2022
I'm wanting it during development
RiA
RiA12/18/2022
You may have to look at source generators then.
RiA
RiA12/18/2022
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.
stigzler
stigzler12/18/2022
Oooo - now that sounds interesting
RiA
RiA12/18/2022
Also, use incremental source generators (v2). the v1 are legacy
RiA
RiA12/18/2022
The Community toolkit for mvvm works in a similar way.
Take a look at [ObservableObject] or [ObservableProperty] source code for inspiration
stigzler
stigzler12/18/2022
Thanks Glitch - you've given me another starting point.
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy