打开.txt到List的数据,然后在文本框中显示数据,...... [英] Open data from .txt to List and then show data in textboxes,...

查看:60
本文介绍了打开.txt到List的数据,然后在文本框中显示数据,......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在.txt中,我有行数据。每个文本框,...从一行获取数据。打开数据的代码如下:



In .txt i have data in lines. Each textbox,... gets the data from a one line. Code for opening data is like this:

 ...
f1.textBox41.Text = lista.List[63].ToString();
f1.textBox42.Text = lista.List[64].ToString();
f1.textBox43.Text = lista.List[65].ToString();
f1.textBox59.Text = lista.List[66].ToString();
f1.textBox47.Text = lista.List[67].ToString();
f1.textBox46.Text = lista.List[68].ToString();
f1.textBox49.Text = lista.List[69].ToString();
... // a lot more data...



正如您所见,数据已列入清单。我需要弄清楚如何检查每一行,如果列表中有任何行没有数据(为空或或,...),则插入空格或其他东西(任何东西)。因为现在它向我显示错误,其中没有字符串。

例如:go throw code,如果在列表中的任何行中没有数据然后在文本框中插入内容 - 这是否可能?或者我是否需要为每一行编写代码来检查是否有值?



p.s.保存工作正常,每个文本框应用程序在.txt中保存一行。但是用户可以忘记,......填写所有内容。在.txt中,我确实得到了我需要的所有行,它们只是空的。


As you can see data is put into list. I need to figure it out how to check each line and if there is any line in list that there is no data (is empty or "" or,...) then insert space or something (anything). Because now it shows me the error where is no string.
For example: go throw code, if in any line in list is no data then insert something in textbox - is this even possible? Or do i need to write code for every line to check if there is a value?

p.s. Saving works fine, for each textbox app saves one line in .txt. But user can forget,... to fill them all. And in .txt i do get all the lines that i need, they are just empty.

推荐答案

你是说你得到一个空参考(或者它是什么的你的ToString是异常吗?是吗?



如果是这样你可以做几件事



1)检查是否为空(每一行)...我也假设你的List是一些其他数据类型然后字符串(导致为什么ToString一个字符串虽然你正在读取文件...)

So are you saying you are getting a null ref (or whatever it is) exception on your ToString? is that it?

If so you could do a few things

1) Check if null (Every line)...also i am assuming your List is some other data type then string (cause why ToString a string although you are reading from a file...)
f1.textBox49.Text = lista.List[69] == null ? "" : lista.List[69].ToString();





-问题是你必须将这个应用到每一行



2)创建方法/扩展方法





-Issue with this is that you have to apply this to every line

2) Create a method/Extension method

public static class ExtensionMethod
{
     public static string SafeString(this object value)
     {
          return value == null ? "" : value.ToString();

     }
}





您必须确保包含使用以引用如果它在不同的命名空间中正确分类顺便说一句。



然后你的用法是





You would have to make sure you include a using to reference the class correctly if it is in a different namespace btw.

Then your usage would be

f1.textBox49.Text = lista.List[69].SafeString();







根据我对你的问题的理解,我认为这就是你要找的东西。




Based on my understanding of your question i think thats what you are looking for.


这篇关于打开.txt到List的数据,然后在文本框中显示数据,......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆