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;
}
}