String.Format的IndexOutOfRange异常

时间:2010-11-11 01:14:52

标签: c#

我想按出生月份搜索文件,并在label7中显示结果。所以我想要的是将数字11输入textbox5按钮4,并将所有出生月份为11的企业显示在label7.text中。 filename.txt是在程序的第一部分创建的,我现在可以搜索filename.txt。我想要做的另一个例子是。创建文件时,数据输入了姓氏,姓氏,生日和出生月份。我想按出生月份搜索该文件,并在label7中显示结果。

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

    }
}

}

3 个答案:

答案 0 :(得分: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

- 更新 -

似乎你需要将文本文件的[月] - 行打印到Label7,代码应为:

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";
}

答案 1 :(得分:0)

括号中的数字不是字段宽度 - 它是要使用的参数的索引。

答案 2 :(得分: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函数。