代码之家  ›  专栏  ›  技术社区  ›  user770022

string.format中的indexoutofrange异常

c#
  •  -2
  • user770022  · 技术社区  · 15 年前

    我想按出生月份搜索一个文件,并在label7中显示结果。所以我想在文本框5中输入数字11,按下按钮4,将出生月份为11的所有输入显示到label7.text中。文件名.txt是在程序的第一部分创建的,现在我可以搜索该文件名.txt了。我想做的另一个例子是。创建文件时,输入的数据包括firstname、lastname、birthday和birth month。我想按出生月份搜索该文件,并在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 回复  |  直到 15 年前
        1
  •  0
  •   Smashery    15 年前

    从您对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函数。

        2
  •  3
  •   xandy    15 年前

    相反

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

    使用

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

    大括号中的0 0表示字符串中的0位置参数。在本例中,格式参数列表指的是文本框5。文本

    --更新--

    似乎您需要将文本文件的[月]-第行打印到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";
    }
    
        3
  •  0
  •   John Saunders    15 年前

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