使用Windows Form应用程序(C#)扫描名称的.txt文件 [英] Scanning a .txt file of names using a windows form app (c#)

查看:173
本文介绍了使用Windows Form应用程序(C#)扫描名称的.txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个扫描列表(doctors.txt)并确定第一个和第二个字母(输入到文本框1& 2中​​的字母)是否与列表中的名称匹配的程序.匹配的名称应在消息框中弹出.我可以将其作为c ++控制台应用程序使用,但不能在c#表单应用程序中使用.

我收到以下错误:对象引用未设置为对象的实例";该错误显示在以下代码上:"name = temp.ToCharArray();"我用错误信息搜索了一下,但是我实在是个菜鸟,无法用我发现的东西做任何事情.在此先感谢您的帮助.

下面假设在"C:\"下存在一个名为"doctors.txt"的文件.

I want a program that scans a list(doctors.txt) and determines if the first and second letter (the ones entered into textbox 1&2) matches a name on the list. The names that match should popup in a messagebox. I can get it to work in as a c++ console app, but not in a c# form app.

I''m getting the following error: "Object reference not set to an instance of an object"; the error shows on the following code: "name = temp.ToCharArray();" I googled the error message, but I''m too much of a noob to make anything out of what I found. Thanks in advance for any help.

The below assumes a file named "doctors.txt" exists at "C:\"

namespace doc_forms
{
    public partial class Form1 : Form
    {

        string temp;
        const string path = @"C:\doctors.txt";
        StreamReader FileOut = new StreamReader (path);
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {

            while ((temp = FileOut.ReadLine()) != null)
            {
                temp = FileOut.ReadLine();
                char[] name = new char[10];
                name = temp.ToCharArray();
                string letter = textBox1.ToString();
                string letter2 = textBox2.ToString();
                string nameL1 = name[0].ToString();
                string nameL2 = name[1].ToString();
                if (letter == nameL1 && letter2 == nameL2)
                {
                    MessageBox.Show(temp);
                }
            }
        }
    }
}



*更新* 11/12 8PM
是的,解决了,谢谢!我知道程序很烂.我无法走得更远,因为我从未听说过"Substring".我现在将抛光此 turd .再次感谢.

[请使用适合家庭使用的语言.]



*UPDATE* 11/12 8PM
Yep, that solved it, thanks! I know the program is crap. I wasn''t able to get farther because I never heard of "Substring". I will polish this turd now. Thanks again.

[Family friendly language please.]

推荐答案

在这里.首先,过一会儿再删除第二条"temp"行,您将不会看到任何异常.

Here it is. First of all remove the second "temp" line after while and you wont see any exceptions.

string temp;
        const string path = @"F:\TEMP\doctors.txt";
        StreamReader FileOut = new StreamReader(path);
        private void button1_Click(object sender, EventArgs e)
        {
            while ((temp = FileOut.ReadLine()) != null)
            {
                if ((textBox1.Text+textBox2.Text)==(temp.Substring(0,2)))
                {
                    MessageBox.Show(temp);
                }
            }
        }


但是整个程序似乎并不那么好


But the whole program seems to be ... not so good


这种设计似乎更好,因为您可以使用任意数量的字母作为模式.此外,您在一个文本框中输入内容.
This design seems to be better, because you can use as many letters as you want to use as a pattern; moreover, you input is in one Textbox.
const string path = @"F:\TEMP\doctors.txt";
       private void button1_Click(object sender, EventArgs e)
       {
           string input = File.ReadAllText(path);
           string pattern = @"^" + textBox1.Text + @"\w*\r";
           foreach (Match m in Regex.Matches(input, pattern, RegexOptions.Multiline))
           {
               MessageBox.Show(m.Value);
           }
       }


namespace doc_forms{    public partial class Form1 : Form    {        string temp;        const string path = @"C:\doctors.txt";        StreamReader FileOut = new StreamReader (path);        public Form1()        {            InitializeComponent();        }        private void button1_Click(object sender, EventArgs e)        {            while ((temp = FileOut.ReadLine()) != null)            {                temp = FileOut.ReadLine();                char[] name = new char[10];                name = temp.ToCharArray();                string letter = textBox1.ToString();                string letter2 = textBox2.ToString();                string nameL1 = name[0].ToString();                string nameL2 = name[1].ToString();                if (letter == nameL1 && letter2 == nameL2)                {                    MessageBox.Show(temp);                }            }        }    }}


这篇关于使用Windows Form应用程序(C#)扫描名称的.txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆