簡體   English   中英

Visual Studio C#標簽更新循環

[英]visual studios c# label update in loop

我希望有人可以幫助我,我有一個循環,可以為每個實例生成一個標簽,其中還包括一個if語句,該語句可以更改標簽的顏色。 但是,當循環再次開始時,標簽在那里,但它們不會更改if語句中的任何顏色。 我將在下面粘貼我的代碼; 我希望有人可以幫助我解決更改標簽顏色的問題

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinSCP;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Configuration;

namespace VIACamMonitoring
{
    public partial class Form1 : Form
    {          
        private static Form1 instancef;

        string tempString = "";
        List<string> quick = new List<string>();
        List<TextBox> list = new List<TextBox>();

        string[] cat2;
        string[] cat;

        int location = 99;
        int location2 = 102;

        int hello = new int();   

        public Form1()
        {
            InitializeComponent();
            instancef = this;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            System.Timers.Timer timer = new System.Timers.Timer();
            timer.Interval = 300000; //1800000;
            timer.Elapsed += timer_Elapsed;
            timer.Start();

        }  

        static void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            string[] cat2;
            string[] cat;
            List<string> quick = new List<string>();
            List<TextBox> list = new List<TextBox>();
            List<Label> labellist = new List<Label>();
            int location = 99;
            int location2 = 102;
            byte[] buffer = new byte[300];  

            StreamReader textread = new StreamReader("config.txt");
            string AllData = textread.ReadToEnd();
            string[] ssize = AllData.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);

            foreach (string line in ssize)
            {    
                cat2 = line.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                string instance = cat2[0];
                string instance2 = cat2[1];
                string instance3 = cat2[2];
                string instance4 = cat2[3];
                string instance5 = cat2[4];    

                quick.Add(instance + "," + instance2 + "," + instance3 + "," + instance4 + "," + instance5);    
            }  

            for (int i = 0; i < quick.Count; i++)
            {
                char[] delimiterChars = { ',', '\t' };
                string happy = quick[i];
                cat = happy.Split(delimiterChars);

                string element = cat[0];
                string element1 = cat[1];
                string element2 = cat[2];
                string element3 = cat[3];
                string element4 = cat[4];

                list.Add(new TextBox());
                list[i].Text = element;
                list[i].Location = new System.Drawing.Point(53, location);
                list[i].Size = new System.Drawing.Size(441, 22);

                labellist.Add(new Label());
                labellist[i].Text = "Status";
                labellist[i].Location = new System.Drawing.Point(520, location2);
                labellist[i].Size = new System.Drawing.Size(48, 17);                

                instancef.Invoke((MethodInvoker)delegate { instancef.Controls.Add(list[i]); });

                SessionOptions sessionOptions = new SessionOptions
                {
                     Protocol = Protocol.Sftp,
                     HostName = element1,
                     UserName = element2,
                     Password = element3,
                     SshHostKeyFingerprint = element4
                };

                try
                {
                    using (Session session = new Session())
                    {
                        session.Open(sessionOptions);

                        if (session.Opened == true)
                        {
                            Console.WriteLine(element + " - " + "Connection Opened");                               
                            labellist[i].BackColor = Color.Green; 
                        }
                        session.Close();
                    }    
                 }
                 catch (Exception ex)
                 {
                    Console.WriteLine(element + " Error");
                    labellist[i].BackColor = Color.Red;

                    UdpClient udpClient = new UdpClient();
                    udpClient.Connect(element1, 50);
                    Console.WriteLine(element1 + "REBOOT");
                    Byte[] senddata = Encoding.ASCII.GetBytes("reboot");
                    udpClient.Send(senddata, senddata.Length);
                    udpClient.Send(senddata, senddata.Length);
                    udpClient.Send(senddata, senddata.Length);
                }
                // Console.WriteLine(element1);
                Console.WriteLine(element + " - Connection Closed");
                instancef.Invoke((MethodInvoker)delegate { instancef.Controls.Add(labellist[i]); });
                location = location + 60;
                location2 = location2 + 60;
            }    
        } 
    }
}

我用一種非常糟糕的方法來解決我的問題,這是

 if(i == 0)
                {

                    instancef.Invoke((MethodInvoker)delegate { instancef.Controls.Clear(); });
                }

但是現在我輸入的不屬於循環的其余標簽現在也消失了,有沒有辦法保留它們,而在循環開始時它們不會消失

您無法在GUI線程上進行冗長的操作,例如連接到遠程服務器。

阻止窗口更新。

暫無
暫無

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

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