ListViewItem中的自动刷新值

时间:2018-11-03 17:53:44

标签: c# listviewitem robotics

我目前正在为工业机器人开发应用程序。该应用程序连接到网络中的所有机械手控制器,并在ListViewItem中列出它们。双击控制器之一,将显示第二个ListVieItem中的详细信息。到目前为止,我设法连接到机器人控制器并使用双击显示信息。现在的问题是控制器内部变量的值正在更改,但是我不知道如何调整代码,因此更改将自动显示。

我使用的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using ABB.Robotics.Controllers;
using ABB.Robotics.Controllers.Discovery;
using ABB.Robotics.Controllers.RapidDomain;
using ABB.Robotics.Controllers.IOSystemDomain;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        private NetworkScanner networkScanner = null;
        private Controller ctrl = null;
        private Controller controller = null;
        private Task[] tasks = null;
        private NetworkWatcher networkwatcher = null;
        private Num rapidNum;
        private double VarValue = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            networkScanner = new NetworkScanner();
            networkScanner.Scan();
            ControllerInfoCollection controllers = networkScanner.Controllers;
            networkwatcher = new NetworkWatcher(networkScanner.Controllers);

            networkwatcher.Found += new EventHandler<NetworkWatcherEventArgs>(HandleFoundEvent);
            networkwatcher.Lost += new EventHandler<NetworkWatcherEventArgs>(HandleLostEvent);
            networkwatcher.EnableRaisingEvents = true;

            foreach (ControllerInfo controller in controllers)
            {
                ListViewItem item = new ListViewItem(controller.IPAddress.ToString());
                item.SubItems.Add(controller.Id);
                item.SubItems.Add(controller.Availability.ToString());
                item.SubItems.Add(controller.IsVirtual.ToString());
                item.SubItems.Add(controller.SystemName);
                item.SubItems.Add(controller.Version.ToString());
                item.SubItems.Add(controller.ControllerName);
                item.Tag = controller;
                this.listControllersView.Items.Add(item);
            }
        }

        private void Form1_Closed(object sender, FormClosedEventArgs e)
        {
            this.networkwatcher.Lost -= new EventHandler<NetworkWatcherEventArgs>(HandleLostEvent);
            this.networkwatcher.Found -= new EventHandler<NetworkWatcherEventArgs>(HandleFoundEvent);
        }

        private void ListControllersView_DoubleClick(object sender, EventArgs e)
        {
            ListViewItem itemView = listControllersView.SelectedItems[0];

            if(itemView.Tag != null)
            {
                ControllerInfo controllerInf = (ControllerInfo)itemView.Tag;

                if(controllerInf.Availability == ABB.Robotics.Controllers.Availability.Available)
                {
                    if(this.controller != null)
                    {
                        this.controller.Logoff();
                        this.controller.Dispose();
                        this.controller = null;
                    }

                    this.controller = ControllerFactory.CreateFrom(controllerInf);
                    this.controller.Logon(UserInfo.DefaultUser);

                    RapidData rd = this.controller.Rapid.GetRapidData("T_ROB1", "RoboDK_Driver", "TESTVAR");

                    if (rd.Value is Num)
                    {
                        rapidNum = (Num)rd.Value;
                        VarValue = rapidNum.Value;
                    }

                    ListViewItem item = new ListViewItem(controller.RobotWare.ToString() + " " + controller.State.ToString() + " " + controller.OperatingMode.ToString() + " " + "VarValue" + " " + VarValue);
                    this.listOutput.Items.Add(item);
                }
                else
                {
                    MessageBox.Show("Selected controller not available");
                }
            }
        }

        void HandleFoundEvent(object sender, NetworkWatcherEventArgs e)
        {
            this.Invoke(new
            EventHandler<NetworkWatcherEventArgs>(AddControllerToListView),
            new Object[] { this, e });
        }

        void HandleLostEvent(object sender, NetworkWatcherEventArgs e)
        {
            this.Invoke(new EventHandler<NetworkWatcherEventArgs>(RemoveControllerFromListView),
            new Object[] { sender, e });
        }

        private void AddControllerToListView(object sender, NetworkWatcherEventArgs e)
        {
            ControllerInfo controllerInfo = e.Controller;
            ListViewItem item = new ListViewItem(controllerInfo.IPAddress.ToString());
            item.SubItems.Add(controllerInfo.Id);
            item.SubItems.Add(controllerInfo.Availability.ToString());
            item.SubItems.Add(controllerInfo.IsVirtual.ToString());
            item.SubItems.Add(controllerInfo.SystemName);
            item.SubItems.Add(controllerInfo.Version.ToString());
            item.SubItems.Add(controllerInfo.ControllerName);
            item.Tag = controllerInfo;
            this.listControllersView.Items.Add(item);
        }

        private void RemoveControllerFromListView(object sender, NetworkWatcherEventArgs e)
        {
            foreach(ListViewItem item in this.listControllersView.Items)
            {
                if((ControllerInfo)item.Tag == e.Controller)
                {
                    this.listControllersView.Items.Remove(item);
                    break;
                }
            }
        }
    }
}

0 个答案:

没有答案