首页 文章

如何将数据从几个文本框保存到文本文件到文本文件

提问于
浏览
-1

我正在用一个按钮开发awindows表单应用程序 . 每次按下按钮都会生成文本框 . 例如,如果我单击该按钮5次,则会有5个文本框 . 现在,用户将向这些文本框输入数据 . 当他按下回车键(另一个按钮)时,它必须存储到文本文件中 . 我在这一点上构造,因为我在运行时生成文本框 . 谁能帮我吗 .

我的代码是

private void create_pos(object sender,EventArgs e){counter_1;

if (counter_1 == count)
        {
            left += 300;
            top = 50;
            count = count + 25;

        }
        List<Button> buttons = new List<Button>();

        Button newButton = new Button();
        buttons.Add(newButton);
        this.Controls.Add(newButton);
        newButton.Left = left;
        newButton.Top = top;



            TextBox newtextbox = new TextBox();

        Controls.Add(newtextbox);


        if (counter_1 == 100)
        {
            button1.Enabled = false;
        }

        newtextbox.Left = left + 100;
        newtextbox.Name = "text" + counter_1;

       // TextWriter tsw = new StreamWriter(@"d:\test.txt", true);
        //tsw.WriteLine(newtextbox.Text);
       // tsw.Close();

        newtextbox.Top = top;
        top += newButton.Height + 2;
        newButton.Text = "position" + counter_1;


        textBox1.Text = newtextbox.Name;
    }
    private void Save_Click(object sender, EventArgs e)
    {
        foreach (Control item in Controls)
        {

            if (item.GetType() == typeof(TextBox))
            {
                savetext[counter_1] = item.Text.ToString();
            }



            System.IO.File.WriteAllText("d:\\test.txt", savetext.ToString());
        }
    }

3 回答

  • 1

    我会跟踪您的动态文本框,以便您可以区分您现在关注的文本框和可能已经在表单上的任何其他文本框,或者您可能稍后添加的文本框 . 我假设,当您生成TextBox并将其添加到表单时,您还会在列表中存储对它的引用,例如

    List<TextBox> dynamicTextBoxes = new List<TextBox>();
    

    在Enter键的事件处理程序(或您要触发写入的任何其他事件处理程序)中,您可以从控件中收集文本并将其写出 . 目前还不清楚你的要求是什么,所以让我们编写一个函数,接受一个TextBox列表和一个文件名来写文本,一次一行

    private void WriteTextBoxes(string path, List<TextBox> textBoxes)
    {
        // Here we use some Linq to quickly get all of the text, but you could
        // also use an explicit loop
        string text = string.Join(Environment.NewLine, textBoxes.Select(t => t.Text));
        File.WriteAllText(path, text);
    }
    

    然后,您可以从事件处理程序中调用该方法,并传入动态TextBox控件列表

    WriteTextBoxes(@"C:\Temp\My.txt", dynamicTextBoxes);
    
  • 0

    你想要做的是,迭代 TextBox 类型的子控件然后收集它的文本并使用这些收集的值创建一个字符串 . 现在您有要写入文件的文本和文件的路径 . 然后您可以使用 File.WriteAllText 方法执行操作:

    StringBuilder contentBuilder = new StringBuilder();
    foreach (TextBox dynamicText in this.Controls.OfType<TextBox>())
    {
        contentBuilder.AppendFormat("Text in {0} is {1} \n", dynamicText.Name, dynamicText.Text);
    }
    string pathOfFile = @"path here";
    System.IO.File.WriteAllText(pathOfFile,contentBuilder.ToString());
    
  • 0

    使用this.FindControl方法查找表单上的控件,并检查它是否是文本框 . 如果是文本框,则使用其.Text属性读取内容 . 使用循环遍历控件 .

    如果您没有维护生成的控件列表,请执行以下操作:

    string filePath = "path to text file";
    StringBuilder contentBuilder = new StringBuilder();
    foreach (Control item in Controls)
    {
         //i don't remember if this check would work. if it doesn't work try item.GetType() == typeof(TextBox)
         if (item is TextBox)
         {
              contentBuilder.Append(((TextBox)item).Text);
         }
    }
    
    string content = contentBuilder.ToString();
    
    if(!string.IsEmptyOrWhiteSpace(content))
    {
        using(StreamWriter writer = new StreamWriter(filePath))
        {
            //check if Write or WriteLine or something else would work.
            writer.Write(content);
        }
    }
    

    如果您使用list来维护生成的控件,那么只需更改循环的for部分即可 .

相关问题