2012-10-18 4 views
0

지금 코드는 전체 텍스트 파일을 가져 와서 하나의 텍스트 상자에 배치합니다. 어떻게해야하는지 알아 내려고하는 것은 파일의 각 줄을 각기 다른 텍스트 상자에 넣는 것입니다.텍스트 파일을 파트/라인으로 나누고 텍스트 상자에 파트를 넣습니다.

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); 

     } 
    } 

답변

2

라인의 순서는 항상 동일하다고 가정하면, 각 TextBox은 라인에 속하는 :

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

가에서 지정된 인덱스의 요소를 반환 시퀀스 또는 인덱스가 범위를 벗어나는 경우이 값은 기본값 인 입니다 (이 경우 null).

1

System.IO.File.ReadAllLines (string filename)을 사용할 수 있습니다. 이것은 파일의 각 행을 문자열 배열로 읽습니다. 다음과 같이 할 수 있습니다.

using System.IO; 


//Namespace, Class Blah Blah BLah 


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


textBox1.Text = FileLines[0]; 

textbox2.Text = FileLines[1]; 

등등. 이 도움이되기를 바랍니다 :)

관련 문제