I have recently changed my project a little to include an interface for better integration, but this meant moving around some methods that were originally on the main form instead other classes. I'm really stuck with how to access a method on my main form (which is used for updating form controls) from my class which is inheriting from my interface. here below is a few snippets of code that should help with clarity.
First off, here is my method on my main form which changes form controls and is located on my main form.
//main form
public partial class BF2300 : Form
{
public void setAlarmColour(byte[] result, int buttonNumber)
{
if (result != null)
{
this.Invoke((MethodInvoker)delegate
{
if (result[4] == 0x00)
{
this.Controls["btn" + buttonNumber].BackColor = Color.Green;
}
else
{
this.Controls["btn" + buttonNumber].BackColor = Color.Red;
}
});
}
}
}
and finally my class method which needs to access this:
//class which needs to access method in main form
class bf2300deviceimp : IdeviceInterface
{
public void start(string address, int port, int i)
{
if (timer == null)
{
timer = new System.Timers.Timer(1000);
timer.Elapsed += delegate(object sender, ElapsedEventArgs e) { timerElapsed(sender, e, address, port, i); };
}
timer.Enabled = true;
// MessageBox.Show("Thread " + i + " Started.");
}
public void timerElapsed(object sender, ElapsedEventArgs e, string address, int port, int i)
{
this.newconnect(address, port, i);
}
public void newconnect(string address, int port, int buttonNumber)
{
try
{
byte[] result = this.newSendCommandResult(address, port, bData, 72);
// needs to access it here.
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
any advice would be really appreciated. I'm just stuck as to how i could go about doing this. i can't simply create a new instance of my main form as i need to reference a current form. At first i thought moving the setAlarmColour to the form would make it so i could access form controls, which is true, but then i couldn't access the method itself so i'm really no better off.