C#Clipboard.SetDataObject(“”)总是给出我副本中列表中的第一项

问题描述 投票:-1回答:1

我使用clipboard.gettext()创建一个回复程序;如果他们在我的名单中,他将看到句子和答案是否相同。现在我希望你将剪贴板复制到动作并用Clipboard.SetDataObject ("");删除它只有成功而不是相关的答案总是给出我的列表中的第一个句子。我想防止这种情况,但我不知道如何解决这个问题

这是我的代码

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

        //Add question/answer to list
        //hoofdstuk 3 it
        //Add question/answer to list
        //hoofdstuk 3 it
        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("What is the best way to apply thermal compound when reseating a CPU?", "Clean the CPU and the base of the heat sink with isopropyl alcohol before applying the thermal compound.*");
        questionList.Add(newQuestion);
        newQuestion = new question("A technician is installing additional memory in a computer. How can the technician guarantee that the memory is correctly aligned?", "A notch in the memory module should be aligned with a notch in the memory slot.*");
        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

            //Startwith zoekt naar alle vragen die matchen vanaf het begin van de zin en Endwith alle vragen die matchen vanaf het eind van de zin//
            if (q._question.StartsWith(clipboardText) || q._question.EndsWith(clipboardText))
            {
                ShowNotification(q._question, q._answer);
                break;
            }
        }
    }

    private void ShowNotification(string question, string answer)
    {
        notifyIcon1.Icon = SystemIcons.Exclamation;
        notifyIcon1.BalloonTipTitle = question;
        notifyIcon1.BalloonTipText = answer;
        notifyIcon1.BalloonTipIcon = ToolTipIcon.Error;
        notifyIcon1.ShowBalloonTip(1000);
    }

    protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);
        {
            const int WM_DRAWCLIPBOARD = 0x308;
            if (m.Msg == WM_DRAWCLIPBOARD)
            {
                GetAnswer(Clipboard.GetText(TextDataFormat.UnicodeText));
                Clipboard.SetDataObject("");
            }
        }
    }
}
c# list winforms clipboard wndproc
1个回答
0
投票

当传递的字符串为空时,字符串StartsWith方法每次返回'true'。因此,首先检查剪贴板中是否有答案=>检查空字符串。

var text = Clipboard.GetText(TextDataFormat.UnicodeText);
if(text != ""){
      GetAnswer(Clipboard.GetText(TextDataFormat.UnicodeText));
      Clipboard.SetDataObject("");
}
© www.soinside.com 2019 - 2024. All rights reserved.