vendredi 30 août 2019

Clipboard.clear with timer with 2 different times does not work C#

I'm making a clipboard application where my program must be able to paste both questions from my list and my sums. Now I want that if I copy a sum that the result can be pasted for 10 seconds. And that my question from my list can only be pasted for 1 second. At the moment it only works with the questions from my list. That timer for that is called DeleteQuestionTimer. now wants another timer with the nameSumDeleteCopyTimer that ensures that the result of the sum that can be pasted into this piece of code for 10 seconds

clipboardText = clipboardText.Replace(',', '.');
            //de regex voor de operator
            var regex = new Regex(@"^(?<lhs>\d+(?:[,.]{1}\d)*)[ ]*(?<operator>[+\-\:x\%])[ ]*(?<rhs>\d+(?:[,.]{1}\d)*)$");
            Match match = regex.Match(clipboardText);
            if (!match.Success)
                return; // not valid form (a + b)
                        //de operators
            Dictionary<string, Func<double, double, double>> binaryOperators = new Dictionary<string, Func<double, double, double>>()
            {

               { "+", (a, b) => a + b},
               { "x", (a, b) => a * b },
               { "-", (a, b) => a - b },
               { "%", (a, b) => a / b * 100 },
               { ":", (a, b) => b == 0 ? double.NaN : a / b },
        };
            var lhs = double.Parse(match.Groups["lhs"].Value, CultureInfo.InvariantCulture);
            var rhs = double.Parse(match.Groups["rhs"].Value, CultureInfo.InvariantCulture);
            var answer = binaryOperators[match.Groups["operator"].Value](lhs, rhs);
            System.Windows.Forms.Clipboard.SetText(answer.ToString());
            //Kopieert het resultaat van de som in het clipboard
            //De timer zorgt ervoor dat het resultaat van de som naar x aantal seconde niet meer te plakken is
            // Laat antwoord zien
            ShowNotification(clipboardText, answer.ToString());
        }

so what i want is that the result of the sum can be pasted for 10 seconds and the copied for my questionList can be pasted for 1 second.

This is my full code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;

namespace it
{
    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 object result;

        // Demonstrates SetText, ContainsText, and GetText.



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

            //Add question/answer to list
            question newQuestion = new question("What is the capital of the Netherlands?", "Amsterdam");
            questionList.Add(newQuestion);

        }

        private void GetAnswer(string clipboardText)
        {
            //Loop through all questions and answers//
            foreach (question q in questionList)
            {
                if (q._question.StartsWith(clipboardText) || q._question.EndsWith(clipboardText))
                {
                    ShowNotification(q._question, q._answer);
                    break;
                }
            }
            clipboardText = clipboardText.Replace(',', '.');
            //de regex voor de operator
            var regex = new Regex(@"^(?<lhs>\d+(?:[,.]{1}\d)*)[ ]*(?<operator>[+\-\:x\%])[ ]*(?<rhs>\d+(?:[,.]{1}\d)*)$");
            Match match = regex.Match(clipboardText);
            if (!match.Success)
                return; // not valid form (a + b)
                        //de operators
            Dictionary<string, Func<double, double, double>> binaryOperators = new Dictionary<string, Func<double, double, double>>()
            {

               { "+", (a, b) => a + b},
               { "x", (a, b) => a * b },
               { "-", (a, b) => a - b },
               { "%", (a, b) => a / b * 100 },
               { ":", (a, b) => b == 0 ? double.NaN : a / b },
        };
            var lhs = double.Parse(match.Groups["lhs"].Value, CultureInfo.InvariantCulture);
            var rhs = double.Parse(match.Groups["rhs"].Value, CultureInfo.InvariantCulture);
            var answer = binaryOperators[match.Groups["operator"].Value](lhs, rhs);
            System.Windows.Forms.Clipboard.SetText(answer.ToString());
            //Kopieert het resultaat van de som in het clipboard
            //De timer zorgt ervoor dat het resultaat van de som naar x aantal seconde niet meer te plakken is
            // Laat antwoord zien
            ShowNotification(clipboardText, answer.ToString());
        }

        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)
                {
                    // Kopieert en kijkt of het overeen komt met de list
                    DeleteQuestionTimer.Enabled = false;
                    var text = Clipboard.GetText(TextDataFormat.UnicodeText);
                    DeleteQuestionTimer.Interval = 5000;
                    DeleteQuestionTimer.Enabled = true;
                    // als je gekopieert hebt reset de clipboard en als je voor 5 minuten niet kopieert sluit het programma zich af
                    if (!string.IsNullOrEmpty(text))
                    {
                        InteractiveTimer.Enabled = false;
                        GetAnswer(Clipboard.GetText(TextDataFormat.UnicodeText));
                        InteractiveTimer.Interval = 300000; // reset the timer
                        InteractiveTimer.Enabled = true;   // and start it again
                    }
                }
            }
        }
        private void InteractiveTimer_Tick(object sender, EventArgs e)
        {
            this.Close();
        }
        //Deze timer zorgt ervoor dat het programa zich naar x aantal minuten zichzelf sluit

        private void DeleteQuestionTimer_Tick(object sender, EventArgs e)
        {
            DeleteQuestionTimer.Enabled = false;
            Clipboard.Clear();
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire