0

私のコードが乱雑でかさばる場合はご容赦ください。私は 1 か月前にプログラミングを始めたまったくの初心者です。searchComByAuthor();これで 、searchComByStartDate();、 の4 つの関数が作成されましsearchComByEndDate();searchComByKeywords();。しかし問題は、それらを組み合わせてフィルターのようにする方法がわからないことです。ユーザーは任意のテキストボックスに入力することを選択でき、ユーザーが「分析」ボタンをクリックすると、結合された機能が作業を行います。現在、それらはすべて個別に機能しています。関数を 1 つずつ呼び出して、機能するかどうかをテストするだけです。

スクリーンショット:

フィルター

searchComByAuthor:

private void searchComByAuthor()
{
    // Process the list of files found in the directory. 
    string[] fileEntries = Directory.GetFiles(sourceDir);
    foreach (string fileName in fileEntries)
    {
        try
        {
            XmlDocument xmlDoc = new XmlDocument(); //* create an xml document object.

            string docPath = fileName;

            xmlDoc.Load(docPath); //* load the XML document from the specified file.

            XmlNodeList nodeList = xmlDoc.GetElementsByTagName("item");

            foreach (XmlNode node in nodeList)
            {

                XmlElement itemElement = (XmlElement)node;

                string itemAuthor = itemElement.GetElementsByTagName("author")[0].InnerText;

                if (itemAuthor.ToLower() == txtComAuthor.Text.ToString().ToLower())
                {
                    string itemTitle = itemElement.GetElementsByTagName("title")[0].InnerText;
                    string itemDate = itemElement.GetElementsByTagName("pubDate")[0].InnerText;
                    string itemDescription = itemElement.GetElementsByTagName("description")[0].InnerText;
                    string itemXMLFile = Path.GetFileNameWithoutExtension(fileName);

                    richComByTemplate.AppendText("SYMBOL: " + itemXMLFile + "\nAUTHOR: " + itemAuthor + "\nDATE: " + itemDate +
                                                "\nTITLE: " + itemTitle + "\nDESCRIPTION: " + itemDescription + "\n\n--------\n\n");

                }
                //else
                //{
                //    richComResults.AppendText("There is no author " + txtComAuthor.Text.ToString().ToLower() + ". Please ensure you are using a correct author name.");
                //}
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    int pointer = 0;
    int index = 0;
    string keyword = txtComAuthor.Text;
    string shadow = richComByTemplate.Text.ToLower();

    while (true)
    {
        //Searching in the copy/shadow
        index = shadow.IndexOf(keyword, pointer);
        //if keyword not found then the loop will break
        if ((index == -1) || (String.IsNullOrEmpty(keyword)))
        {
            break;
        }
        richComByTemplate.Select(index, keyword.Length);
        richComByTemplate.SelectionColor = Color.Red;
        richComByTemplate.SelectionFont = new System.Drawing.Font(richComByTemplate.Font, FontStyle.Bold);
        pointer = index + keyword.Length;
    }
}

searchComByStartDate:

private void searchComByStartDate()
{

    // Process the list of files found in the directory. 
    string[] fileEntries = Directory.GetFiles(sourceDir);
    foreach (string fileName in fileEntries)
    {
        XmlDocument xmlDoc = new XmlDocument(); //* create an xml document object.

        string docPath = fileName;

        xmlDoc.Load(docPath); //* load the XML document from the specified file.

        XmlNodeList nodeList = xmlDoc.GetElementsByTagName("item");

        foreach (XmlNode node in nodeList)
        {

            XmlElement itemElement = (XmlElement)node;

            string itemDate = itemElement.GetElementsByTagName("pubDate")[0].InnerText;
            CultureInfo provider = CultureInfo.InvariantCulture;
            if (DateTime.Parse(itemDate) >= DateTime.ParseExact(txtComStartDate.Text, "dd/MM/yy", provider))
            {
                string itemAuthor = itemElement.GetElementsByTagName("author")[0].InnerText;
                string itemTitle = itemElement.GetElementsByTagName("title")[0].InnerText;
                string itemDescription = itemElement.GetElementsByTagName("description")[0].InnerText;
                string itemXMLFile = Path.GetFileNameWithoutExtension(fileName);

                richComByTemplate.AppendText("SYMBOL: " + itemXMLFile + "\nAUTHOR: " + itemAuthor + "\nDATE: " + itemDate + 
                                            "\nTITLE: " + itemTitle + "\nDESCRIPTION: " + itemDescription + "\n\n--------\n\n");
            }
        }
    }
}

searchComByEndDate:

private void searchComByEndDate()
{
    // Process the list of files found in the directory. 
    string[] fileEntries = Directory.GetFiles(sourceDir);
    foreach (string fileName in fileEntries)
    {
        XmlDocument xmlDoc = new XmlDocument(); //* create an xml document object.

        string docPath = fileName;

        xmlDoc.Load(docPath); //* load the XML document from the specified file.

        XmlNodeList nodeList = xmlDoc.GetElementsByTagName("item");

        foreach (XmlNode node in nodeList)
        {

            XmlElement itemElement = (XmlElement)node;

            string itemDate = itemElement.GetElementsByTagName("pubDate")[0].InnerText;
            CultureInfo provider = CultureInfo.InvariantCulture;
            if (DateTime.Parse(itemDate) <= DateTime.ParseExact(txtComEndDate.Text, "dd/MM/yy", provider))
            {
                string itemAuthor = itemElement.GetElementsByTagName("author")[0].InnerText;
                string itemTitle = itemElement.GetElementsByTagName("title")[0].InnerText;
                string itemDescription = itemElement.GetElementsByTagName("description")[0].InnerText;
                string itemXMLFile = Path.GetFileNameWithoutExtension(fileName);

                richComByTemplate.AppendText("SYMBOL: " + itemXMLFile + "\nAUTHOR: " + itemAuthor + "\nDATE: " + itemDate + 
                                            "\nTITLE: " + itemTitle + "\nDESCRIPTION: " + itemDescription + "\n\n--------\n\n");
            }
        }
    }
}

searchComByKeywords:

private void searchComByKeywords()
{
    List<TextBox> boxes = new List<TextBox>();
    boxes.Add(txtComKeyword1);
    boxes.Add(txtComKeyword2);
    boxes.Add(txtComKeyword3);
    boxes.Add(txtComKeyword4);

    // Process the list of files found in the directory. 
    string[] fileEntries = Directory.GetFiles(sourceDir);
    foreach (string fileName in fileEntries)
    {
        try
        {
            XmlDocument xmlDoc = new XmlDocument(); //* create an xml document object.

            string docPath = fileName;

            xmlDoc.Load(docPath); //* load the XML document from the specified file.

            XmlNodeList nodeList = xmlDoc.GetElementsByTagName("item");

            foreach (XmlNode node in nodeList)
            {
                XmlElement itemElement = (XmlElement)node;

                string itemDescription = itemElement.GetElementsByTagName("description")[0].InnerText;

                if (txtComKeyword1.Text != (String.Empty) && itemDescription.ToLower().Contains(txtComKeyword1.Text.ToLower()) ||
                    txtComKeyword2.Text != (String.Empty) && itemDescription.ToLower().Contains(txtComKeyword2.Text.ToLower()) ||
                    txtComKeyword3.Text != (String.Empty) && itemDescription.ToLower().Contains(txtComKeyword3.Text.ToLower()) ||
                    txtComKeyword4.Text != (String.Empty) && itemDescription.ToLower().Contains(txtComKeyword4.Text.ToLower()))
                {
                    string itemTitle = itemElement.GetElementsByTagName("title")[0].InnerText;
                    string itemDate = itemElement.GetElementsByTagName("pubDate")[0].InnerText;
                    string itemAuthor = itemElement.GetElementsByTagName("author")[0].InnerText;
                    string itemXMLFile = Path.GetFileNameWithoutExtension(fileName);

                    richComByTemplate.AppendText("SYMBOL: " + itemXMLFile + "\nAUTHOR: " + itemAuthor + "\nDATE: " + itemDate + 
                                                "\nTITLE: " + itemTitle + "\nDESCRIPTION: " + itemDescription + "\n\n--------\n\n");

                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    foreach (TextBox box in boxes)
    {
        int pointer = 0;
        int index = 0;
        string keyword = box.Text;
        string shadow = richComByTemplate.Text.ToLower();

        while (true)
        {
            //Searching in the copy/shadow
            index = shadow.IndexOf(keyword, pointer);
            //if keyword not found then the loop will break
            if ((index == -1) || (String.IsNullOrEmpty(keyword)))
            {
                break;
            }
            //Customising the original data
            richComByTemplate.Select(index, keyword.Length);
            richComByTemplate.SelectionColor = Color.Red;
            richComByTemplate.SelectionFont = new System.Drawing.Font(richComByTemplate.Font, FontStyle.Bold);
            pointer = index + keyword.Length;
        }
    }
}
4

1 に答える 1

2

関数内に非常に大量の繰り返しコードがあります。

4 つの関数を 1 つの関数に結合することで、コードを簡素化しながら目標を達成できます。その単一の関数に、特定のレコードを含めるために満たす必要がある条件のリストであるパラメーターを指定します (レコードを選択するためにすべての条件が適用される必要があると仮定します)。

あなたの場合、1 つまたは複数のテキスト フィールドに入力されたものと直接一致することを望んでいるようです。これらのテキスト フィールドの値を新しい結合メソッドに渡し (ロジックを UI から分離できるようにするため)、null 以外の空白以外の値を持つ各値に対して、適切なロジック テストを適用します。Author と StartDate が渡された場合は、Author と StartDate の両方に関連するロジックを適用します。両方のテストに合格した場合は、そのレコードを含めます。

コードが現在行っていることを行うのではなく、「そのレコードを含める」ために:

richComByTemplate.AppendText(...)

結果のリストを返し、呼び出し元にテキストの追加を行わせたい場合があります (ここでも、ユーザー インターフェイスを検索ロジックから分離するためです)。

于 2012-08-20T03:54:28.383 に答える