Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

132
Visualizações
Derived class thinks my base class got deleted

I tried to make a derived WinForm class, but I have an issue: when I try to create my DerivedClass (via Add > New element > Derived Form), Visual studio tells me that the object does not exist

I'm sure the object at stake is BaseForm because I get the Setting Error message shows in the console. (my guess is because the Close()function is called )

public partial class BaseForm : Form
{
    Port port;

    public BaseForm()
    {
        InitializeComponent();
    }

    protected void Form1_Load(object sender, EventArgs e)
    {
        port = new Port();
        if (port.Port_Setting() != 0)
        {
            Console.WriteLine("Setting Error");
            Close();
        }
    }
}

For now, BaseForm is empty, just a form with no component added.

The only part used of my Port class so far is this, just initializing it for further use:

class Port
{
    protected SerialPort sp = new SerialPort();
    public int Port_Setting()
    {
        Console.WriteLine("Setting port");
        if (sp.IsOpen)
        {
            sp.Close();
        }

        sp.BaudRate = 38400;
        sp.DataBits = 8;
        sp.StopBits = System.IO.Ports.StopBits.One;
        sp.Parity = System.IO.Ports.Parity.None;
        sp.PortName = "COM3";

        bool Error = false;
        try
        {
            sp.Open();
        }
        catch (UnauthorizedAccessException) { Error = true; }
        catch (ArgumentException) { Error = true; }
        catch (System.IO.IOException) { Error = true; }
        if (Error)
        {
            return -1;
        }
        return 0;
    }
}
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Generating the derived class via the Visual Studio's interface (Add > New Element>Derived Form) –

Aha now things are getting more clear to me.

When you derive a form in Visual Studio, and you open the derived form in the designer, some code on the base form (the parent where you inherited from) will be executed.

I don't know all methods from the top of my head, but I believe that the load event for example will be executed in the base form when you load the inherited form in the designer at design time.

This is not a flaw from VS, this is by design. The idea is that when you create a custom user control, the code in the user control can also be executed in design time. And inheriting a form from another is somewhat the same thing for the designer.

What you can do to solve this is check in the code of the base form if you are on design time or not.

I have done this by adding this method on my base form:

 protected bool IsInDesignMode
 {
     get { return DesignMode || LicenseManager.UsageMode == LicenseUsageMode.Designtime; }
 }

you can use it like this

    private void FormBase_Load(object sender, EventArgs e)
    {
        if (IsInDesignMode == false)
        {
            // write code here that should only run at runtime
        }
    }
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda