【问题标题】:read line from text file then How show line 1st word textbox1 until : symbol then 2nd textbox show after : symbol?从文本文件中读取行然后如何显示第一个单词 textbox1 直到:符号然后第二个文本框显示在:符号之后?
【发布时间】:2014-11-19 13:44:47
【问题描述】:

我有以下进程 1 .txt 文件

firstname:lastname
sumon:kamal
abdul:halim
kamrul:khan
rafiq: akbor

我如何显示第一个名称文本框 1 和第二个文本框显示姓氏?

清除示例

textbox1 显示 sumon & textbox2 显示 kamal

我正在尝试更多的道路,但仍然没有为此找到任何道路。

我正在尝试使用 streamReader 来显示文本框数据,但只有整行。

如何读取 1 行和 2 个文本框的名称?

【问题讨论】:

标签: c# textbox streamreader


【解决方案1】:

如果我没听错你的问题,你想要这样的东西。

public partial class Main : Form
{
    private const string PATH = "C:\\person.txt";

    public Main()
    {
        InitializeComponent();

        // read lines from file at specified path
        var lines = File.ReadLines(PATH);

        // take first line from aquired collection
        var line = lines.FirstOrDefault();
        if (line == null)
            return;

        // split string to first and last names
        var parts = line.Split(':');
        if (parts.Length != 2)
            return;

        // send them to textBox.Text property
        textBox1.Text = parts[0];
        textBox2.Text = parts[1];
    }
}

不要忘记将 textBox1 和 textBox2 放到表单中。 如果您需要其他内容,请遍历行。

【讨论】:

  • { public 部分类 Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { var lines = File.ReadLines("D:\\ovi"); // 从获取的集合中取出第一行 var line = lines.FirstOrDefault(); if (line == null) 返回; var parts = line.Split(':'); if (parts.Length != 2) 返回; textBox1.Text = 部分[0]; textBox2.Text = 部分[1]; } } }
  • 我正在使用 windows 窗体应用程序,但不知道如何使用类,所以我是那里的新人,所以你能告诉我如何直接将它放在 c# windows 应用程序中吗?我正在使用 webbrowser windows 应用程序表单。请给我一些清楚的东西。我急需这个,这对我来说真的很重要。马贝你的帮助我会成功。
  • 出色的工作!!! StackOverflow 的大力支持。我很高兴从谷歌收集网站。真的很棒你的支持真的。非常感谢您必须对您的生活有所帮助。
猜你喜欢
  • 2011-12-17
  • 2015-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多