簡體   English   中英

c#從另一個類/事件更新WinForm GUI文本框

[英]c# Update WinForm GUI textBox from another class/event

我正在學習使用PtGrey相機的API。

我有公司提供的以下類示例,用於設備到達和移除。

    Class SystemEventListener: ManagedInterfaceEvent
    {
        private IManagedSystem system;

        public SystemEventListener(IManagedSystem sys) { system = sys; }


        protected override void OnDeviceArrival(UInt64 serialNumber)
        {
            int count = system.GetCameras().Count;

            Console.WriteLine("System event listener:");
            Console.WriteLine("\tThere {0} {1} {2} on the system.", (count == 1 ? "is" : "are"), count, (count == 1 ? "device" : "devices"));
        }

        protected override void OnDeviceRemoval(UInt64 serialNumber)
        {
            int count = system.GetCameras().Count;

            Console.WriteLine("System event listener:");
            Console.WriteLine("\tThere {0} {1} {2} on the system.", (count == 1 ? "is" : "are"), count, (count == 1 ? "device" : "devices"));
        }
    }

我正在嘗試使其適應我的獲勝形式環境。 在努力從此類中更新GUI之后,我設法按照此處的鏈接更新文本框。 但是,它涉及修改program.cs文件。

我的問題是如何從另一個類的這些事件中更新文本框,最好不要觸摸Program.cs。

我確實嘗試過使用委托/事件等。但是每次,我在主窗體(Form1)實例上遇到Null Reference Exception。 我肯定做錯了什么。

以下是我目前的實現方式,但我希望通過另一種方式來修改program.cs。

Program.cs

    public static Form1 MainForm;
    [STAThread]
    static void Main()
    {

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        MainForm = new Form1();
        Application.Run(MainForm);
    }

Form1.cs

    public Form1()
    {
        InitializeComponent();

        SystemEventListener systemEventListener = new SystemEventListener(system);
        system.RegisterInterfaceEvent(systemEventListener);
    }

    private void AppendTextBox(string value)
    {
        if (InvokeRequired)
        {
            this.Invoke(new Action<string>(AppendTextBox), new object[] { value });
            return;
        }
        textBoxCamProperties.AppendText(DateTime.Now.ToString("h:mm:ss tt") + "-" + value + Environment.NewLine);
    }

    public class SystemEventListener : ManagedInterfaceEvent
    {
        private IManagedSystem system;

        public SystemEventListener(IManagedSystem sys) { system = sys; }

        protected override void OnDeviceArrival(UInt64 serialNumber)
        {
            //int count = system.GetCameras().Count;

            Program.MainForm.AppendTextBox("Device attached\r\n");


        }

       protected override void OnDeviceRemoval(UInt64 serialNumber)
        {
            //int count = system.GetCameras().Count;

            Program.MainForm.AppendTextBox("Device removed\r\n");

        }
    }

謝謝。

更新:對手提袋蟹的榮譽! 我可以使用他的方法而無需接觸Program.cs。 :-)

有人還能告訴我在這種特殊情況下如何正確使用事件/委托嗎?

干杯,非常感謝! :-)

代替:

Program.MainForm.AppendTextBox("Device removed\r\n");

采用:

Application.OpenForms.OfType<MainForm>().FirstOrDefault()?.AppendTextBox("Device removed\r\n");

Application.OpenForms是一個靜態屬性,使您可以訪問在應用程序中打開的所有表單。

上面的linq行檢查OpenForms列表中是否有MainForm類型的任何類型,然后提取第一個(如果存在),並在其上調用AppendTextBox。

?. 操作員會為您執行空檢查。 等效於以下代碼:

MainForm form = Application.OpenForms.OfType<MainForm>().FirstOrDefault();
if (form != null)
    form.AppendTextBox("Device removed\r\n");

您需要使用參考詞:閱讀此鏈接https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/ref

因此,在Form1的構造函數中,您可以像這樣引用程序:

public Form1(ref Program myProgram){
    myProgram.MainForm.AppendTextBox("Device removed \r\n");
}

在Program.CS中,您可以添加以下代碼:

static void Main()
{
    Program myProgram = this;

    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    MainForm = new Form1(ref myProgram);

    Application.Run(MainForm);
}

希望能幫助到你 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM