2010-11-11 69 views
-2

我想按出生月份搜索一個文件,並在label7中顯示結果。所以我想要的是將數字11輸入到textbox5中,然後按下按鈕4並將所有輸入爲11的輸入顯示在label7.text中。 filename.txt是在程序的第一部分創建的,我現在可以搜索那個filename.txt。我想要做的另一個例子是。當文件被創建時,數據被輸入名字,姓氏,生日和出生月份。我想在出生月份搜索該文件並在label7中顯示結果。IndexOutOfRange Exception from String.Format

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void tabPage2_Click(object sender, EventArgs e) 
    { 

    } 

    private void label3_Click(object sender, EventArgs e) 
    { 

    } 

    private void textBox1_TextChanged(object sender, EventArgs e) 
    { 

    } 

    private void textBox2_TextChanged(object sender, EventArgs e) 
    { 

    } 

    private void maskedTextBox1_MaskInputRejected(object sender, MaskInputRejectedEventArgs e) 
    { 

    } 

    private void textBox3_TextChanged(object sender, EventArgs e) 
    { 

    } 

    private void textBox4_TextChanged(object sender, EventArgs e) 
    { 

    } 

    private void close_Click(object sender, EventArgs e) 
    { 
     Close(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     writetext(); 
     reset(); 

    } 
    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 button3_Click(object sender, EventArgs e) 
    { 
     Close(); 
    } 

    private void button2_Click(object sender, EventArgs e) 


    { 
     readfile(); 


    } 

    private void label7_Click(object sender, EventArgs e) 
    { 

    } 

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

}

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

    } 

    private void textBox5_TextChanged(object sender, EventArgs e) 
    { 

    } 
} 

}

+0

我們不可能在不知道filename.txt可能包含什麼的情況下幫助您。告訴我們,我們可以幫助你。 – Smashery 2010-11-11 01:27:23

回答

0

根據您對xandy的回答的評論來判斷,我們無法在不知道filename.txt文件格式的情況下幫助您。不過,你可能想要這樣的東西。

private void button4_Click(object sender, EventArgs e) 
{ 
    string[] lines = File.ReadAllLines("filename.txt"); 
    string result = GetResultFromLines(lines, textBox5.Text); 
    label7.Text = (String.Format("Month of Birth{0}", result)); 
} 

根據您希望如何從文件中檢索數據,您必須自己編寫GetResultFromLines函數。

3

相反

label7.Text = (String.Format("Month of Birth{4}", textBox5.Text)); 

使用

label7.Text = (String.Format("Month of Birth{0}", textBox5.Text)); 

{0}在大括號0指的String.Format參數列表中的0定位參數,在這種情況下,是指textBox5.Text

--Update--

看來你需要打印[月]文本文件Label7的n行的代碼應該是:

string[] lines = ... 
try{ 
    int month = Int32.parse(textBox5.Text); 
    label7.Text = String.Format("Month of Birth {0}", lines[month]); 
} 
catch(Exception e){ 
    label7.Text = "Invalid input"; 
} 
+0

我希望它顯示文件中具有輸入到textbox5中的出生月份的條目列表我不想要我輸入到textbox5中的birthmonth – user770022 2010-11-11 01:20:41

+0

請閱讀更新的答案。 – xandy 2010-11-11 01:23:29

+0

錯誤無效的表達術語'。' \t 錯誤標識符預計 錯誤標識符預期; 'try'是關鍵字\t 錯誤;預計 – user770022 2010-11-11 01:29:22

0

括號中的數字是不是字段寬度 - 這是使用的參數指標。