问题
I know this can be a duplicate, but I already searched the web and tried literally ALL and EVERY solution I found, but to no avail...
I have a custom form in my project called ParentForm
it is the parent of most of my other forms in the project, it has some standard elements on it, like panels and timers.
I have no problem with showing this form, editing its design or code.
but whenever I open a form that is inheriting from it, it opens for a moment (not responding thou) and then visual studio crashes immediately, I think it was only the designer first, but now, even when I open the form's code, the same happens!
I tweaked some of the parent form's code and now it stopped crashing.. but now, whenever I open a child form of it, I get this:
ContaierPanel
and panel2
are two panels I have on the parent form, and as I see no problem with them, they are used as containers in the child forms, their access modifiers are public too.
The problems seems to be related to the parent form itself, but I already removed every single line of code in it, that didn't help at all, now I'm stuck with the error you see in the image above.
I also uploaded the complete project to Github in case someone wanted to see the code, here's the link.
I'm using the Material Skin in the project.
Your help is so appreciated, I have no much time before the deadline and there is still much to do... Thanks
Edit: After missing around the code for a few extra hours, I finally discovered the code part that is causing the problem:
As I said before I have a timer on the parent form called tmrCheckConnection
, here's the code of the timer:
private void tmrCheckConnection_Tick(object sender, EventArgs e)
{
if (!Program.dbConnection.IsConnect())
{
if (!controlsAreDisabled)
{
disabledControls.Clear();
foreach (Control c in this.Controls)
{
if (c.Enabled)
{
disabledControls.Add(c);
c.Enabled = false;
}
}
controlsAreDisabled = true;
}
lblNoDBConnection.Enabled = true;
lblNoDBConnection.Visible = true;
this.Controls.SetChildIndex(lblNoDBConnection, 0);
} else {
if (controlsAreDisabled)
{
foreach (Control c in disabledControls)
c.Enabled = true;
lblNoDBConnection.Visible = false;
}
}
}
Obviously, the error happens here >> Program.dbConnection.IsConnect() << because that is not an instantiated class yet, and the IsConnect()
functions uses some of its un-instatiated members.
So the solution is simply to prevent the timer code from running at design time, so I tried putting this:
if (System.ComponentModel.LicenseManager.UsageMode == System.ComponentModel.LicenseUsageMode.Designtime)
return;
at the beginning of the timer's code, it didn't change anything.
Next, I tried to remove the event handler from the timer, and reassign it in the Form_Load like this:
if (System.ComponentModel.LicenseManager.UsageMode == System.ComponentModel.LicenseUsageMode.Runtime)
tmrCheckConnection.Tick += tmrCheckConnection_Tick;
And again, it didn't help.
Any ideas?
Oh, and I removed the github repository.
来源:https://stackoverflow.com/questions/61267470/visual-studio-crashes-when-opening-forms-inheriting-from-a-specific-form-in-the