简体   繁体   English

如何读取 txt 文件中的行,然后在 TextBox VB.NET 中仅显示某些字符?

[英]How Can I read line in txt file and then display only certain characters in TextBox VB.NET?

I am trying to search for, then display a specific line in a.ino file then display that in a TextBox.我正在尝试搜索,然后在 a.ino 文件中显示特定行,然后在 TextBox 中显示该行。 First I write many lines to a text file and once the vb.net program is closed I would like to then read from the same file.首先,我将多行写入文本文件,一旦 vb.net 程序关闭,我想从同一个文件中读取。 This is so if I have 50 textboxes I dont have remember what order everthing goes in. I can read from the file and make 1 change.如果我有 50 个文本框,我不记得所有内容的顺序是什么。我可以从文件中读取并进行 1 次更改。 So far I can write everything to file I need.到目前为止,我可以将所有内容写入我需要的文件。 So next step is to read from it.所以下一步是从中读取。 Any advice here is much appreciated.非常感谢这里的任何建议。

So far everything is just created with normal objectwriter and so on到目前为止,一切都是用普通的 objectwriter 创建的,依此类推

When I create the file I am only inserting the textbox info amongst other text below is an example.当我创建文件时,我只在下面的其他文本中插入文本框信息是一个示例。 Idealy read the charater '49' and then display what i wrote in text box.理想情况下阅读字符“49”,然后在文本框中显示我写的内容。 49 is what I would search for and then display nuts when i read from the file 49 是我从文件中读取时要搜索然后显示坚果的内容

The code is pretty staright forward to write to a text file该代码非常直接地写入文本文件

Public Class Form1
    Private Sub BtnWrite_Click(sender As Object, e As EventArgs) Handles BtnWrite.Click
        Dim filepath As String = "C:\temp\test.txt"
        If System.IO.File.Exists(filepath) = True Then
            Dim objWriter As New System.IO.StreamWriter(filepath)
            objWriter.Write("#include <Arduino.h>" & vbCrLf)
700 line inbetween
            objWriter.Close()
            MessageBox.Show("File Create")
        Else
            MessageBox.Show("File Does Not Exist")
        End If
    End Sub

The text file saves as文本文件另存为

if(nuts_state){strip.setPixelColor(49, red);strip.show();delay(0);}
else{strip.setPixelColor(0, off);strip.show();delay(0);}

在此处输入图像描述

Think about storing the data in a Dictionary allowing you to use key-value pairs:考虑将数据存储在允许您使用键值对的字典中:

Dictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary.Add("1", "Some value 1");
dictionary.Add("2", "Something");

Then using a JSON serialization library, like Json.Net, write the dictionary out to a file:然后使用 JSON 序列化库,如 Json.Net,将字典写入文件:

string json = JsonConvert.SerializeObject(dictionary);
File.WriteAllText("File.Txt", json);

Then read it back in using the JSON library:然后使用 JSON 库将其读回:

Dictionary<string, string> previousDictionary = 
JsonConvert.DeserializeObject<Dictionary<string, string> (File.ReadAllText("File.txt"));

I did not need to any of this.我不需要这些。 Usersavforms allows you to enter values for the textboxes then recall them Usersavforms 允许您输入文本框的值,然后调用它们

Sub SaveFormState(ByVal SourceForm As Form)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM