如果复制了句子的一部分而不是整个句子,则也会得到回报c#

时间:2018-10-06 09:19:29

标签: c# string class notifications clipboard

我想要什么?

我希望,如果您选择疑问句的一部分,您得到的收益与复制整个句子时的收益相同。我已经在看StartWith方法,但是我不知道如何使用它。有人对此有解决方案吗?这是我的代码。预先感谢。

namespace test
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            //Invoke a clipboard monitor
            [DllImport("User32.dll", CharSet = CharSet.Auto)]
            public static extern IntPtr SetClipboardViewer(IntPtr hWndNewViewer);
            private IntPtr _ClipboardViewerNext;

            //Make some global variables so we can access them somewhere else later
            //This will store all Questions and Answers
            //In here will be the Questions and Answers
            List<question> questionList = new List<question>();

            private void Form1_Load(object sender, EventArgs e)
            {
                //Set our application as a clipboard viewer
                _ClipboardViewerNext = SetClipboardViewer(this.Handle);

                //Add question/answer to list
                question newQuestion = new question("When a computer is being assembled, which action can be taken to help eliminate cable clutter within a computer case?", "Install a modular power supply.*");
                questionList.Add(newQuestion);
                newQuestion = new question("vraag2", "antwoord2");
                questionList.Add(newQuestion);
                newQuestion = new question("vraag3", "antwoord3");
                questionList.Add(newQuestion);
            }

            private void getAnswer(string clipboardText)
            {
                //Loop through all questions and answers
                foreach (question q in questionList)
                {
                    //If we have found an answer that is exactly the same show an Notification
                    if (q._question == clipboardText)
                    {
                        showNotification(q._question, q._answer);
                    }
                }
            }

            private void showNotification(string question, string answer)
            {
                notifyIcon1.Icon = SystemIcons.Exclamation;
                notifyIcon1.BalloonTipTitle = question;
                notifyIcon1.BalloonTipText = answer;
                notifyIcon1.BalloonTipIcon = ToolTipIcon.Error;
                notifyIcon1.ShowBalloonTip(100);
                Clipboard.Clear();
            }

            protected override void WndProc(ref Message m)
            {
                base.WndProc(ref m);
                {
                    const int WM_DRAWCLIPBOARD = 0x308;
                    if (m.Msg == WM_DRAWCLIPBOARD) {
                        getAnswer(Clipboard.GetText());
                    }
                }
            }
        }
    }

预先感谢

1 个答案:

答案 0 :(得分:1)

首先从问题中删除任何“空格”,并将其存储在单独的字段中。例如:

string question="how are you today";
string questionWithNoSpace=question.Replace(" ","");//Which makes the question like this:"howareyoutoday".

现在从剪贴板上获取问题并按“空格”进行拆分,如下所示:

string userQuestion ="how are you";
list<string> parts=userQuestion.Splite(" "); //Which returns a list containing {"how","are","you"}
if (parts.All(a=>questionWithNoSpace.Contains(a)))
        {
            return "Blah! that is your question";
        }

记住要将所有内容都转换为小写