为什么element.Text =不起作用

时间:2013-09-01 12:54:35

标签: c# string winforms text labels

尝试下一步 - 我想检查csv文件中是否已存在某些信息,如果是 - 打开带有标签的表单,并从文件中放入此标签信息 代码是下一个:

public void getEventTime(string filePath, string currDate, string currentDateTimeHM)
    {
        //reading the *.csv file and convert to the array of data
        FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
        StreamReader sr = new StreamReader(fs);

        //create array for getting any vlaue from string
        string[] arrOfData = sr.ReadToEnd().Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
        sr.Close();

        List<string> lines = new List<string>();
        bool status=false;//variable for showing form if event exist

        foreach (var l in arrOfData)
        {
            if (l.Contains(currDate) && l.Contains(currentDateTimeHM))
            {
                string[] temp = l.Split(',').Take(5).ToArray();
                notLabel.Text = temp[1].ToString();
                status = true;
            }
        }
        if (status)
        {
            //show Notification Form
            Form NotificationForm = new Notification();
            NotificationForm.Visible = true;
        }
    }

所有作品都很完美 - 如果存在信息 - 会打开新表单,但notLabel.Text = temp[0].ToString();这部分没有返回任何内容。在调试期间,我得到了下一个 enter image description here

意味着代码是正确的,但对我来说很奇怪的原因导致程序 - 没有这个文本。 我犯了什么错误?

下面带有标签的表格 enter image description here

已检查

enter image description here

文件NotificationDesigner.Form.cs

中的几行
        this.notLabel.AutoSize = true;
        this.notLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
        this.notLabel.Location = new System.Drawing.Point(12, 22);
        this.notLabel.Name = "notLabel";
        this.notLabel.Size = new System.Drawing.Size(34, 13);
        this.notLabel.TabIndex = 0;
        this.notLabel.Text = "label";

1 个答案:

答案 0 :(得分:1)

您在哪里调用方法getEventTime,什么是notLabel

如果调用方法getEventTime并设置notLabel.Text但是之后文本再次设置为string.Empty存在问题,那么您应该搜索或调试每个更改到notLabel.Text

你确定表格中显示的是notLabel吗?您可以通过注册mouseDown事件来查看,并在点击标签时看到它被调用

还有一件事,添加break; 在你的行之后

status = true;

转到设计并按标签,按F4并搜索name属性,我打赌它不是notLabel

enter image description here

修改

我认为我解决了你的问题

如果我错了,请纠正我,但这行

    if (status)
    {
        //show Notification Form
        Form NotificationForm = new Notification();
        NotificationForm.Visible = true;
    }
更改文本后发生

...当你的意思是:

public void getEventTime(string filePath, string currDate, string currentDateTimeHM)
{
    Form NotificationForm = new Notification();
    //reading the *.csv file and convert to the array of data
    FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
    StreamReader sr = new StreamReader(fs);

    //create array for getting any vlaue from string
    string[] arrOfData = sr.ReadToEnd().Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
    sr.Close();

    List<string> lines = new List<string>();
    bool status=false;//variable for showing form if event exist

    foreach (var l in arrOfData)
    {
        if (l.Contains(currDate) && l.Contains(currentDateTimeHM))
        {
            string[] temp = l.Split(',').Take(5).ToArray();
            NotificationForm.NotText = temp[1].ToString();
            status = true;
        }
    }
    if (status)
    {
        //show Notification Form

        NotificationForm.Visible = true;
    }
}

并在通知表单中执行

  public string NotText
  {
     set { notLabel.Text = value; }
  }