Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从文本文件插入文本框_C# - Fatal编程技术网

C# 从文本文件插入文本框

C# 从文本文件插入文本框,c#,C#,因此,我想做的是将一个txt文档加载到C#中,并在txt文档的每一行中,让该部分从文本文件写入不同的文本框中 NTM-120 = textBox1 NTM-130 = textBox2 NTM-140 etc.... NTM-150 NTM-160 NTM-170 这可能吗 像这样的东西 using (StreamReader reader = File.OpenText("yourFileName.txt")) { textBox1.Text = reader.ReadLine(

因此,我想做的是将一个txt文档加载到C#中,并在txt文档的每一行中,让该部分从文本文件写入不同的文本框中

NTM-120 = textBox1
NTM-130 = textBox2
NTM-140   etc....
NTM-150
NTM-160
NTM-170
这可能吗

像这样的东西

using (StreamReader reader = File.OpenText("yourFileName.txt"))
{
    textBox1.Text = reader.ReadLine();
    textBox2.Text = reader.ReadLine();
    textBox3.Text = reader.ReadLine();
    textBox4.Text = reader.ReadLine();
    textBox5.Text = reader.ReadLine();
    textBox6.Text = reader.ReadLine();
    textBox7.Text = reader.ReadLine();
    textBox8.Text = reader.ReadLine();
    textBox9.Text = reader.ReadLine();
}
试试这个:

int count=1;
var lines = File.ReadAllLines("C:\\Data.txt");
int totalTxtBoxControls=20;
if(lines.Count==totalTxtBoxControls)
{
((TextBox)this.Controls.Find("TextBox" + count, true)[0]).Text = line[count-1];
count++;
}

听起来你似乎没有真正想过如何做,也没有尝试过,但基本上

string[] lines = System.IO.File.ReadAllLines("source.txt");
foreach(string line in lines)
{
    // put you logic on which line goes to which textbox
}
嗯?

你可以用

private void button1_Click(object sender, EventArgs e)
{
    using (StreamReader sr = new StreamReader(filePath))
    {
        int lineNumber = 0;
        while (!sr.EndOfStream)
        {
           lineNumber++;
           var readLine = sr.ReadLine();
           if (readLine != null)
           {
               TextBox textBox = GetControle(this, "textBox"+lineNumber);
                if (textBox != null)
                {
                    textBox.Text = readLine;
                }
            }
        }
     }
}

private TextBox GetControle(Control ctrlContainer, string name)
{
    foreach (Control ctrl in ctrlContainer.Controls)
    {
        if (ctrl.GetType() == typeof(TextBox))
        {
            if (ctrl.Name == name)
            {
                return (TextBox)ctrl;
            }
        }
         if (ctrl.HasChildren)
            GetControle(ctrl, name);
    }
    return null;
}

试试这个。它会为文本文件中的每一行动态创建一个文本框

TableLayoutPanel tlp = new TableLayoutPanel();
tlp.Dock = DockStyle.Fill;

int row = 0;
foreach (string s in File.ReadAllLines("file.txt"))
{
    tlp.RowStyles.Add(new RowStyle());

    TextBox tb = new TextBox();
    tb.Text = s;
    tlp.Controls.Add(tb, 0, row++);
}

this.Controls.Add(tlp);

是的,这是可能的。您标记此sql的原因是什么?我在问题中没有看到sql。。。当然也可能是你到目前为止试过的代码,这要看情况而定。什么样的文本框?在网页或windows窗体应用程序上?表示。。。。textbox1.text=NTM-120 rite?亲爱的@user3324892,您应该提供一些关于您尝试了什么、您遇到了什么问题、您希望如何处理来自文本框的信息等信息。