2010-11-11 87 views
0

我希望它顯示包含我在textbox5.text中輸入的所有值的所有行。顯示內容的問題

當我輸入一個月的例子是10到textbox5,然後單擊button4我想搜索爲具有示例10的BirthMonth的enteries創建的文件,然後在label7中顯示結果。

public void writetext() 
    { 

     using (TextWriter writer = File.AppendText("filename.txt")) 

     { 
     writer.WriteLine("First name, {0} Lastname, {1} Phone,{2} Day of birth,{3} Month of Birth{4}", textBox1.Text, textBox2.Text, maskedTextBox1.Text, textBox4.Text, textBox3.Text); 
     MessageBox.Show(String.Format("First Name,{0} Lastname, {1} Phone,{2} Day of birth,{3} Month of Birth{4}", textBox1.Text, textBox2.Text, maskedTextBox1.Text, textBox4.Text, textBox3.Text)); 
     } 
     } 
    public void reset() 
    { 
     textBox1.Text = ""; 
     textBox2.Text = ""; 
     textBox3.Text = ""; 
     textBox4.Text = ""; 
     maskedTextBox1.Text = ""; 
    } 

private void button4_Click(object sender, EventArgs e) 
    { 
     string[] lines = File.ReadAllLines("filename.txt"); 
    try{ 
     int month = Int32.Parse(textBox5.Text); 
     label7.Text = String.Format("Month of Birth {0}", lines[month]); 
     } 
    catch(Exception){ 
     label7.Text = "Invalid input"; 
    } 



    } 
    public void readfile() 
    { 
     string[] lines = File.ReadAllLines("filename.txt"); 
     label6.Text = String.Join(Environment.NewLine, lines); 

    } 




First name, 99999 Lastname, 87t8t8 Phone,999-9999 Day of birth,ug Month of Birthoi 
First name, 9898988 Lastname, 787877 Phone,999-9999 Day of birth,67 Month of Birth78 
First name, Lastname, Phone,588-8888 Day of birth, Month of Birth 
First name, ytyt Lastname, Phone, - Day of birth, Month of Birth 
First name, iuiu Lastname, Phone, - Day of birth, Month of Birth 
First name, 98989 Lastname, Phone, - Day of birth, Month of Birth 
First name, plplpl Lastname, Phone, - Day of birth, Month of Birth 
First name, okok Lastname, uihuhuh Phone,777-7777 Day of birth,54 Month of Birth76 
First name, 090909 Lastname, Phone, - Day of birth, Month of Birth 
First name, ijijij Lastname, Phone, - Day of birth, Month of Birth 
First name, kjkj Lastname, Phone, - Day of birth, Month of Birth 
First name, Lastname, Phone, - Day of birth, Month of Birth 
First name, Lastname, Phone, - Day of birth, Month of Birth 
First name, ygyg Lastname, Phone, - Day of birth, Month of Birth 
+0

我敢打賭,你可以是一個小更明確你想完成什麼!我幾乎可以猜到! – 2010-11-11 02:36:10

+0

我試圖解釋它好一點。但是當我在textbox5中輸入一個數字時,我想要它,然後單擊按鈕4我想搜索爲所有輸入數字創建的文件,這些輸入數字已經輸入到textbox5中以顯示在標籤中7 – user770022 2010-11-11 02:41:18

回答

1

鑑於您編寫文件的方式,您可以這樣做。

例如:

string[] lines = { "Month of Birth1", "Month of Birth3" }; 
string[] matchingLines = lines.Where(x => x.Contains("Month of Birth1")).ToArray(); 

應用:

string[] lines = File.ReadAllLines("filename.txt"); 
int month = Int32.Parse(textBox5.Text); 
string matchText = string.Format("Month of Birth{0}", month); 
string[] matchingLines = lines.Where(x => x.Contains(matchText)).ToArray(); 
label7.Text = String.Join(Environment.NewLine, lines); 

你可能考慮CSV風格文件格式的,雖然,在那裏你在一個預先只值 - 在您的文本文件中定義的順序 - 標題列文本可能只是該文件的第一行。

+0

錯誤名稱'lines '在當前上下文中不存在\t 錯誤名稱「行」在當前上下文中不存在 – user770022 2010-11-11 02:50:02

+0

emmh我直接從您在問題中提供的示例中取出「行」,但我會將其添加到代碼清晰。 – BrokenGlass 2010-11-11 02:51:48

0

把字符串數組列表,以使搜索更容易一點,然後搜索「出生月份」 +月,並打印結果:

List<string> l = new List<string>(lines); 
     List<string> matchingLines = l.FindAll(x => x.Contains("Month of Birth" + month)); 
     string output = ""; 
     foreach (string line in matchingLines) 
     { 
      output += line + Environment.NewLine; 
     } 
     output.TrimEnd(Environment.NewLine); 
     label7.Text = output;