如何将文件中的文本添加到标签中 [英] How do I add text from file to labels

查看:68
本文介绍了如何将文件中的文本添加到标签中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本文件和一个名为

hi i have a text file and an array of labels named

arraylabels

的标签数组,当我关闭我的程序时,每个标签文本都保存为文本文件中的一行,我想要当我从保存的文本文件中重新打开程序时,将文本返回到每个标签,注意:我的标签放在

when i close my program each label text is saved as a line in the text file,i want to get the text back to each label when i reopen the program from the saved text file,Note:my labels are put in

flowlayoutpanel





我尝试了什么:



我尝试了很多代码和方法这是其中之一



What I have tried:

I tried many codes and approaches this is one of them

<pre>Dim lines As String() = File.ReadAllLines(del.filename)
        Dim v As Integer = 0
        For v = 0 To UBound(labelarray)
            If lines.Length <> 0 And lines(v) <> "" Then
                labelarray(v).Text = lines(v)
            Else
                Exit For

            End If
        Next



这一行在行


this one is returning null refernce at line

labelarrays(v).text=lines(v)

推荐答案

为了获得空引用异常,点运算符左侧的对象必须为null。在这一行中,dot的唯一用途是访问您控件的Text属性以设置字符串。很明显,labelarray的至少一个元素不包含任何东西。

使用调试器在发生错误时找出 v 的值,并检查相关的数组元素以找出它包含null的原因。

当你知道它是哪个元素时,你可以开始回顾你的代码,找出它为什么是空的。



我的猜测是 v 为零,你根本没有将任何控件加载到数组中,只是为它们分配了空间 - 但没有代码,猜测就是全部它是。
In order to get an null reference exception, the object of the left of the dot operator must be null. In this line, the only use of dot is to access the Text property of you control in order to set the string. So clearly, at least one of the elements of the labelarray does not contain anything.
Use the debugger to find out the value of v when the error occurs, and check the relevant array element to find out why it contains null.
When you know which element(s) is it, you can start looking back in your code to find out why it's null.

My guess would be that v is zero and you haven't loaded any controls into the array at all, just assigned the space for them - but without the code a guess is all it is.


这篇关于如何将文件中的文本添加到标签中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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