开发者

Problem with what is displayed

I would like for it display all lines that have all the values that I entered in textbox5.text.

When I enter a month example is 10 into textbox5 and then click button4 I would like to search the file that was created for the enteries that have a BirthMonth of example 10 then display the results in 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,  Phon开发者_如何学编程e,   - 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


Given the way you write your file you could do something like this.

example:

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

applied:

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

Your probably want to consider a CSV style of file format though, where you have only the values in a pre-defined order in your text file - the header column texts could just be the first row of that file.


Put the string array into a list to make searching a bit easier, then search for "Month of birth" + month, and print the results:

 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; 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜