1

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.

Kestami
  • 2,045
  • 3
  • 32
  • 47
  • Can u be more specific..newConnect is written in child for of the main form?? – doneyjm May 28 '12 at 09:51
  • Still interested in a solution after all this time? The two standard idioms for handling this are to simply pass to the calling class a reference to your form class (simple but inflexible), or to declare an event in the calling class and have the form class subscribe to it (slightly more complicated, but much more flexible). Please see http://stackoverflow.com/q/1665533 for answers with examples of both. If it addresses your question, close this one as a duplicate of the other. If not, please edit your question explaining how this is different and what specifically you need help with. – Peter Duniho Jun 17 '15 at 16:31

2 Answers2

0

The best way is to use events. Otherwise if it is the parent form , u can user

((MainForm) currentform.Parent).SetAlarmColor()

Events:

 public delegate void SetDelegate(byte[] result, int buttonNumber);
        public event SetDelegate SetAlarmColor;

make a delegate and event in ur child class.

Child child = new Child();

     child.SetAlarmColor += new Child.SetDelegate(child_SetAlarmColor);

use that event while creating the child form

doneyjm
  • 145
  • 6
  • can you give me an idea of what sort of event i would need to use? i already have several events being used and wondered if i could integrate it into one of those. – Kestami May 28 '12 at 10:00
  • Still I am not very clear about ur requirement..may be above help u out – doneyjm May 28 '12 at 10:13
0

You can declare the setAlarmColour method as static and simply call BF2300.SetAlarmMethod from your interface.

Chibueze Opata
  • 9,856
  • 7
  • 42
  • 65
  • however then things like .this become invalid, and as it's on the main form itself i can't instantiate from here. – Kestami May 28 '12 at 13:58
  • Just setup a thread and a delegate, the delegate method should be the guy to run your colour changing task, and the static method should instantiate and start the thread instead, and you're good to go. – Chibueze Opata May 28 '12 at 20:03