Results 1 to 3 of 3

Thread: Putting a decimal in a textBox for Timer Interval?

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2012
    Posts
    32

    Putting a decimal in a textBox for Timer Interval?

    At the moment my interval is set by

    Code:
            private void btnStart_Click(object sender, EventArgs e)
            {
                tmrClick.Interval = Convert.ToInt32(textBox1.Text) * 1000;
                tmrClick.Enabled = true;
            }
    It works for whole numbers fine, but when I try to stick a decimal in the box (0.5 for half a second) it just gives me the error:

    System.FormatException was unhandled
    Message=Input string was not in a correct format.
    Source=mscorlib
    StackTrace:
    at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
    at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
    at System.Convert.ToInt32(String value)
    at AutoTyper1.Clicker.btnStart_Click(Object sender, EventArgs e) in C:\Users\Tom\documents\visual studio 2010\Projects\AutoTyper1\AutoTyper1\Clicker.cs:line 32
    at System.Windows.Forms.Control.OnClick(EventArgs e)
    at System.Windows.Forms.Button.OnClick(EventArgs e)
    at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
    at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
    at System.Windows.Forms.Control.WndProc(Message& m)
    at System.Windows.Forms.ButtonBase.WndProc(Message& m)
    at System.Windows.Forms.Button.WndProc(Message& m)
    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
    at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
    at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoCompo nentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
    at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
    at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
    at System.Windows.Forms.Application.Run(Form mainForm)
    at AutoTyper1.Program.Main() in C:\Users\Tom\documents\visual studio 2010\Projects\AutoTyper1\AutoTyper1\Program.cs:line 18
    at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
    at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
    at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
    at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
    at System.Threading.ThreadHelper.ThreadStart()
    InnerException:
    Sadly I've only started looking at C# today and the syntax is... well I'm used to the Visual Basic syntax.

    How do I fix this?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Putting a decimal in a textBox for Timer Interval?

    Of course it does, because you can't convert 0.5 to an Int32. You would need to convert the Text of the TextBox to a Single, Double or Decimal first, then scale up, THEN ROUND OFF, then convert to an Int32. Even in VB using CInt your code would be bad because you would be rounding the 0.5 to an Integer first before scaling, so 0.5 would end up 1000 instead of 500.

    Whether using VB or C#, you should be using the TryParse method of the appropriate numeric type first to make sure that you have a valid number and convert it if you do. You then scale from seconds to milliseconds. Using VB you can then round and convert to Int32 in one go using CInt, but C# requires you to do it in two steps, using Math.Round and then Convert.ToInt32.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Jan 2012
    Posts
    32

    Re: Putting a decimal in a textBox for Timer Interval?

    Quote Originally Posted by jmcilhinney View Post
    Of course it does, because you can't convert 0.5 to an Int32. You would need to convert the Text of the TextBox to a Single, Double or Decimal first, then scale up, THEN ROUND OFF, then convert to an Int32. Even in VB using CInt your code would be bad because you would be rounding the 0.5 to an Integer first before scaling, so 0.5 would end up 1000 instead of 500.

    Whether using VB or C#, you should be using the TryParse method of the appropriate numeric type first to make sure that you have a valid number and convert it if you do. You then scale from seconds to milliseconds. Using VB you can then round and convert to Int32 in one go using CInt, but C# requires you to do it in two steps, using Math.Round and then Convert.ToInt32.
    I did it! Yay!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width