0
私はC#を初めて使用していますので、Windowsフォームのラベルを上書きする必要があります。私は、ループを使ってnの値ごとにラベルの数を生成しています。実行中のプログラムのラベルテキストを上書きする
問題点:プログラムの実行中にlines[i]
の値を変更した場合、値は変更されますが、Windowsフォームでは更新されません(以前の値は新しい値に置き換えられません)。
どのようにすればいいですか?
また、私はまた、これは私がラベルを作成し、その中に値を書き込む部分である微
を働いているコード毎秒使用してタイマーを、さわやかだし、それがループしている
Label label = new Label();
label.Text = String.Format("{0}", lines[i]);
label.Left = 10;
label.Top = (i + 1) * 25;
this.Controls.Add(label);
ここ
は、誰もがそれをチェックする必要がある、私の完全なコードIDです:
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 System.IO;
namespace Admin
{
public partial class Form1 : Form
{
string pathuser = @"//192.168.2.10/Shared-Public/Users.txt";
int check = 0;
public static string usernam;
string[] lines = new String[500];
string[] lines2 = new String[500];
public Form1()
{
InitializeComponent();
}
private void timer_Tick(object sender, EventArgs e)
{
tc = tc + 1;
Console.WriteLine(tc);
int c = 2;
int u = 0;
int us = 0; //for user pass
int z = 0; // for reading values from file
using (StreamReader sr2 = new StreamReader(pathuser))
{
string line;
while ((line = sr2.ReadLine()) != null)
{
lines[us] = line;
us++;
}
}
for (int i = 0; i < us; i++)
{
//Create label
Label label = new Label();
label.Text = String.Format("{0}", lines[i]);
//Position label on screen
label.Left = 10;
label.Top = (i + 1) * 25;
Label label2 = new Label();
label2.Left = 120;
label2.Top = (i + 1) * 25;
string line2;
string path = "//192.168.2.10/Shared-Public/" + lines[i] + DateTime.Now.ToString(" MM-dd-yyyy") + ".txt";
if (!File.Exists(path))
{
lines2[z] = null;
}
else
{
using (StreamReader sr3 = new StreamReader(path))
{
while ((line2 = sr3.ReadLine()) != null)
{
lines2[z] = line2;
z++;
}
}
label2.Text = String.Format("{0}", lines2[0]);
u = z;
z = 0;
}
PictureBox picbox = new PictureBox();
picbox.Location = new Point(240, 25 + (i*25));
picbox.Size = new Size(15, 15);
if (u%2==0)
picbox.BackColor = Color.Green;
else
picbox.BackColor = Color.Red;
u = 0;
this.Controls.Add(label);
this.Controls.Add(label2);
this.Controls.Add(picbox);
}
}
int tc = 0;
private void Form1_Load(object sender, EventArgs e)
{
Timer timer = new Timer();
timer.Interval = (1000);
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
}
}
}
手動で変更するラベルを作成する場合は、フォーム上の変数にそのラベルを保持する必要があります。次に、変数を変更する場合は、変数でラベルにアクセスして変数を変更します。しかし、あなたはおそらく何か別のことをやっているでしょう。アプリケーションを実行し、コードが毎秒更新されている間にフォームを移動できるかどうかを確認します。私はあなたのUIが応答していないと思う、アミール? – Will
'Label'に' i'を含む名前を付けることができます。たとえば、 'label.Name =" label_ "+ i;'です。さらに、 'list [i]'を更新すると 'controls [" label_ "+ i] .Text = list [i];' –
ファイルから値を読み込んでいる場所に新しい値を追加すると、私は同じ値を編集する場合、それを置き換えません..... – Mubi