English 中文(简体)
从类中访问窗窗窗方法而不创建新实例
原标题:Accessing form method from a class without creating a new instance

我最近改变了我的计划, 以纳入一个界面来更好地整合, 但这意味着要围绕一些最初以主形式呈现的方法, 而不是其他班级。 我真正坚持着如何从我从界面上继承的班级中获取我主形式( 用于更新表格控制) 的方法。 下面是一些代码的片段, 应该清晰清晰。

首先,这是我在主要形式上的方法 改变控件 位于我的主要形式上

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


            });

        }
    }
 }

最后,我的班级方法 需要访问这个:

//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());
        }

    }
}

任何忠告都会非常感激。 我只是坚持我怎样才能做到这一点。 我无法简单地创建我主形式的新实例, 因为需要引用当前表格。 起初,我认为将 secAlarmColour 移动到窗体上, 这样我就可以访问窗体控制, 确实如此, 但后来我无法访问方法本身, 因此我实在没有更好的方法了 。

问题回答

最佳方式是使用事件。 否则, 如果它是父窗体, 您可以使用 。

(主Form) 当前格式。 Parrent. SetAlarmColor ()

活动:

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

代表与活动 在您的儿童类。

Child child = new Child();

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

创建子窗体时使用该事件

您可以从您的界面中将 setAlarmColour 方法声明为静态, 简单调用 BF2300.SetAlarmMethod





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签