-1

i want the number copied in seconds to be the timer interval for me timer1. how can I do that?

the user must first copy autoclicker to enter the mode then the user must copy the interval in seconds as a number. But when I now copy auto clicker he says that the copied is not a number

this is my code

            if (clipboardText == "auto clicker")
            {
                if (int.TryParse(Clipboard.GetText(), out int x))
                {
                    timer1.Interval = x * 1000;
                }
                //when clipboard isnt a number
                else
                {
                    notifyIcon1.Icon = SystemIcons.Exclamation;
                    notifyIcon1.BalloonTipTitle = "";
                    notifyIcon1.BalloonTipText = "No number in clipboard";
                    notifyIcon1.BalloonTipIcon = ToolTipIcon.Error;
                    notifyIcon1.ShowBalloonTip(1000);
                    return;
                }
            }
tewew
  • 1
  • 2
  • 4
    Possible duplicate of [How can I convert String to Int?](https://stackoverflow.com/questions/1019793/how-can-i-convert-string-to-int) – vasily.sib Oct 03 '19 at 08:32
  • @vasily.sib it isn't a duplicate and that is not where i'm looking for – tewew Oct 03 '19 at 08:33
  • vasily's right in that your essential problem is how to convert the clipboard text string to a number, though it's not an exact solution to your specific problem, hence why he posted a comment rather than an answer – Caius Jard Oct 03 '19 at 08:35
  • @tewew, [timer1.Interval](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.timer.interval?view=netframework-4.8) is an `int`. [Clipboard.GetText()](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.clipboard.gettext?view=netframework-4.8) is a `string`. You need a string to int conversion. A perfect duplicate. – vasily.sib Oct 03 '19 at 08:37

1 Answers1

1

Timer intervals are in milliseconds and are integers. Clipboard.GetText returns a string which you say will be seconds, so you want to first convert string to int and then multiply it by 1000 to make seconds into milliseconds.

something like:

if(int.TryParse(Clipboard.GetText(), out int x))
  timer1.Interval = x * 1000;
else
  //whatever you want to do if the clipboard is not a number

Don't forget to start the timer if needed..

This notation is fairly recent c#. If you get a syntax error because you're on an older version of c# (I think VS2015 or earlier) you'll have to use the older form where you declare the output variable x outside of the TryParse:

int x;
if(int.TryParse(Clipboard.GetText(), out x))
  timer1.Interval = x * 1000;
else
  //whatever you want to do if the clipboard is not a number
Caius Jard
  • 72,509
  • 5
  • 49
  • 80
  • 1
    inlined declaration of `out` parameter is C#7 feature (VS2017) – vasily.sib Oct 03 '19 at 08:41
  • @Caius Jard i did your code but i got another problem i want to make a autoclicker and the user first want to copy auto clicker to start the autoclicker and then the user must copy a number for the timer interval but now my application says always that input isn't a number check my updated code in my question – tewew Oct 03 '19 at 08:41
  • 1
    Couple of things: 1) StackOverflow isn't intended to be a place where you ask one question, then get an answer, integrate it, hit another problem and edit your question to be the new problem (because it invalidates the answers already given and annoys the people writing the answers) - **don't move the goalposts**, ask a whole new question. 2) Though I cant see how your `clipboardText` variable got its value, if it's from the `Clipboard.GetText()` then the code will never work, because the only way you can try and parse a number is if the clipboard contains `"auto clicker"` which isn't a number – Caius Jard Oct 03 '19 at 08:49