C # Clipboard.SetDataObject ("") всегда дает первый элемент в моем списке при копировании. - PullRequest
0 голосов
/ 28 марта 2019

Я создаю программу ответа, используя 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("");
            }
        }
    }
}

1 Ответ

0 голосов
/ 28 марта 2019

Метод StartWith строки каждый раз возвращает true, когда переданная строка пуста. Поэтому сначала проверьте, есть ли ответ в буфере обмена => проверьте пустую строку.

var text = Clipboard.GetText(TextDataFormat.UnicodeText);
if(text != ""){
      GetAnswer(Clipboard.GetText(TextDataFormat.UnicodeText));
      Clipboard.SetDataObject("");
}
...