C
C#2y ago
stigzler

❔ 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
{

}
}
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! 🧱
25 Replies
RiA
RiA2y ago
public static Guid GUID { get; private set; } = Guid.NewGuid(); You access it by: BaseForm.GUID everywhere.
stigzler
stigzler2y ago
The issue is VS auto generates the .Designer.cs
RiA
RiA2y ago
you're adding the GUID property yourself though?
stigzler
stigzler2y ago
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.
RiA
RiA2y ago
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
stigzler
stigzler2y ago
I was hoping to do it all in the Base class sorry yes - different guids for different windows
RiA
RiA2y ago
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
stigzler
stigzler2y ago
ok - thank you for your help
RiA
RiA2y ago
Yeah you can add code to constructor i think did you check if the designer is resetting the constructor code?
stigzler
stigzler2y ago
The baseclass or .designer ctor?
RiA
RiA2y ago
baseclass baseclass ctor functions should carry over afaik if not just add : base() to the derived class ctor and it should work
stigzler
stigzler2y ago
I 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
RiA
RiA2y ago
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.
stigzler
stigzler2y ago
I know, I know...
RiA
RiA2y ago
How are you creating these forms?
stigzler
stigzler2y ago
Have you worked in winforms before?
RiA
RiA2y ago
yes I mean to say are the forms being created by binding or you have a method to create them You can do this:
BaseForm myform = new();
myform.GUID = Guid.NewGuid();
myform.Show();
BaseForm myform = new();
myform.GUID = Guid.NewGuid();
myform.Show();
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
stigzler
stigzler2y ago
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
RiA
RiA2y ago
Wait so this guid assignment is during development or during runtime?
stigzler
stigzler2y ago
I'm wanting it during development
RiA
RiA2y ago
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.
stigzler
stigzler2y ago
Oooo - now that sounds interesting
RiA
RiA2y ago
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 inspiration
stigzler
stigzler2y ago
Thanks Glitch - you've given me another starting point.
Accord
Accord2y ago
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.
Want results from more Discord servers?
Add your server
More Posts