2013-09-01 137 views
2

试着做下一步 - 我想检查一些信息是否已经存在,如果是csv文件,如果是的话 - 打开带标签的表格,并把文件中的信息放到 代码是下一个:为什么element.Text =不工作

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"; 
+0

您是在设计器还是编程添加标签? – keyboardP

+0

我手动添加标签到disigner窗体,名称 - notLabel – gbk

回答

2

你在哪里调用方法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; } 
    } 
+0

'get event'我在主窗体加载'eventViewer中调用。getEventTime(FilePath,currentDateTimeD,currentDateTimeHM);'notLabel - 它是通知标签意味着 – gbk

+0

我如何搜索notLabel.text的每个变化?只使用调试?在我使用它的代码中没有任何额外的地方。也许问题是因为我在代码的一部分中做了一些变化,但在另一部分中调用了窗口?并在呼叫窗口这个标签变为空? – gbk

+0

@Kirill试试这个:制作一个按钮,当你按下按钮,你调用getEventTime,并告诉我,如果那工作 –