2015-05-22 29 views
1

我是比較新的編程和我已經成立了一個「相當」簡單的任務,我去努力。我試圖完成的是單擊Form1上的「設置」按鈕,該按鈕將打開並將「config.txt」的結果發佈到Form2上的標籤。讀一個.txt,顯示它在形式,編輯表格,並保存

CONFIG.TXT看起來是這樣的:

[VERSION] 7544 
[WIDTH] 480 
[HEIGHT] 768 
[SCALE] 1 
[UI] 8 
[SERVER] 2 
[DEMO] 1 
[BRIGHT] 50 
[CURSOR] 1 

我已經能夠創建.txt文件,如果它不使用

using (StreamWriter sw = new StreamWriter("config.txt")) 
{ 
    sw.Write("[DATA1] 7544"); 
    sw.Write("[DATA2] 8"); 
    sw.Write("[DATA3] 2"); 
} 

默認值存在我有問題閱讀代碼行分開並顯示給單獨的標籤。

int counter = 0; 
string line; 
System.IO.StreamReader file = new System.IO.StreamReader(@"config.txt"); 
while ((line = file.ReadLine()) != null) 
{ 
    //System.Console.WriteLine(line); 
    string labelTest = string.Format(line); 

    labelVersRead.Text = "Version: " + line; 
    counter++; 
}  
file.Close(); 

我相信我遇到的問題是說var line3 = line[3]。我只能得到它輸出完整的.txt成一個單一的字符串。

+1

是否「有問題」的意思是你有企圖說明了什麼?如果是這樣,請發佈。您用來編寫文件的代碼似乎不相關。 – Blorgbeard

+0

你可以[編輯]你的帖子;這通常比在評論中發佈代碼要好。 – Blorgbeard

+0

「[DATA1] 7544」行中的Data1是什麼?它有映射標籤嗎? – vishalvatsal

回答

0

在這種情況下,你可以有一個列表。

var list = new List<Config>(); 
public class Config{ 
    string LabelText 
    string LabelValue 
} 

while ((line = file.ReadLine()) != null) 
{ 
    //Split the line based on the pattern and build the list object for Labeltext and LabelValue. 

    //You will have to come up with the logic to split the line into string based on the pattern. Where text in the [] is LabelTesxt and anything followed after ] is LabelValue 

    list.Add(new Config{LavelText = "VERSION" ,LabelValue="7544"}); 

    counter++; 
} 

//Once done, you could bind the data to the label 
var item = list.Find(item => item.LabelText == "VERSION"); 
lblVersionLabel.Text = item.LabelValue 
+0

這是如何工作的? 當前labelVersRead(標籤)輸出所有的config.txt。 這裏您已經定義了LabelText和LabelValue。我不確定我是否理解這是如何工作的。 – NooBeX

+0

經過進一步調查,我不確定這會做我想找的。我會進一步研究我的問題,也許我可以更好地解釋它。 – NooBeX

0

它看起來像你總是凌駕Text的標籤。

使用+追加的文本。

labelVersRead.Text += "Version: " + line; 

,或者在年底

labelVersRead.Text += "Version: " + line + "\r\n"; 

新生產線就這樣解決所遇到的問題?

+0

這實際上提供了相同的視覺效果,因爲它將整個config.txt輸出到單個單行字符串中。 – NooBeX

相關問題