Expecto
Expecto

Reputation: 521

break a text file into parts/lines and place the parts into text boxes

Right now my code takes the entire text file and just places it all into one text box. What I am trying to figure out how to do is have it place each line of the file into each separate text box.

namespace HomeInventory2
{
    public partial class Form1 : Form
    {
        public Form1(string prepopulated)
        {
            InitializeComponent();
            textBoxAmount.Text = prepopulated;
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void submitButton_Click(object sender, EventArgs e)
        {
            CreateInventory create = new CreateInventory();
            create.ItemAmount = textBoxAmount.Text;
            create.ItemCategory = textBoxCategories.Text;
            create.ItemProperties = textBoxValue.Text;
            create.ItemValue = textBoxValue.Text;

            InventoryMngr invtryMngr = new InventoryMngr();
            invtryMngr.Create(create);

        }
    }

Upvotes: 0

Views: 97

Answers (2)

xlTobylx
xlTobylx

Reputation: 67

You could use System.IO.File.ReadAllLines(string filename). What this does is reads each line of the file into a String array. You could then do something like:

using System.IO;


//Namespace, Class Blah Blah BLah


String[] FileLines = File.ReadAllLines("Kablooey");


textBox1.Text = FileLines[0];

textbox2.Text = FileLines[1];

And so on. I hope this helps :)

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460168

Assuming that the order of the lines is always the same and that each TextBox belongs to a line:

IEnumerable<String> lines = File.ReadLines(path);
textBoxAmount.Text = lines.ElementAtOrDefault(0);
textBoxCategories.Text = lines.ElementAtOrDefault(1);
textBoxValue.Text = lines.ElementAtOrDefault(2);
...

Enumerable.ElementAtOrDefault<TSource> Method

Returns the element at a specified index in a sequence or a default value if the index is out of range (null in this case).

Upvotes: 2

Related Questions