Home > Software engineering >  C# Windows Form Designer - Input String was not in correct Format
C# Windows Form Designer - Input String was not in correct Format

Time:05-14

[Hello everyone, I'm a new into the world of programming, I tried to follow a tutorial showing how to make a timer, that can shutDown your Pc, or put it into Saving mode... and I've done exactly everything like in the tutorial, but i'm still getting this error message after trying to run it " Input String was not in correct Format "! [I've ad a picture of the code ]

thank you in advance for your help.

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;

namespace _01_Shutdown_Timer_WindowsForm_Tuto
{
    public partial class Form1 : Form
    {
        TimeSpan timeLeft;
        public Form1()
        {
            InitializeComponent();
        }
        private void btnStart_Click(object sender, EventArgs e)
        {
            CheckTextBoxValues();

            bool isStartable = true;

            try
            {
                timeLeft = new TimeSpan(Convert.ToInt32(txtHours.Text), Convert.ToInt32(txtMinutes.Text), Convert.ToInt32(txtSeconds.Text));
            }
            catch (FormatException ex)
            {
                isStartable = false;
                MessageBox.Show(ex.Message);
            }

            if (isStartable == true)
            {
                timer.Start();
                // Zeit richtig formatieren, von Seter to Timer
                lblTimer.Text = timeLeft.ToString(@"hh\:mm\:ss\:");
                
            }
        }

        private void CheckTextBoxValues()
            //Wenn nix im Seter ist, dann = 0
        {
            if (txtHours.Text.Count() == 0)
                txtHours.Text = "0";

            if (txtMinutes.Text.Count() == 0)
                txtMinutes.Text = "0";

            if (txtSeconds.Text.Count() == 0)
                txtSeconds.Text = "0";

        }
        private void timer_Tick(object sender, EventArgs e)
        {
            timeLeft = timeLeft.Subtract(TimeSpan.FromSeconds(1));
            lblTimer.Text = timeLeft.ToString(@"hh\:mm\:ss\");
            if (timeLeft.TotalSeconds <= 0)
            {
                timer.Stop();
                PerformAction();
            }
        }
        private void PerformAction()
        {
            if (rbShutDown.Checked == true)
                Process.Start("shutdown", "/s");
            else if (rbRestart.Checked == true)
                Process.Start("shutdown", "/r");
            else if (rbSavePower.Checked == true)
                Process.Start("rundll32.exe", "powrprof.dll,SetSuspendState");
        }
        private void btnStop_Click(object sender, EventArgs e)
        {
            timer.Stop();
            lblTimer.Text = ("00:00:00");
        }

CodePudding user response:

you should remove all trailing \ or : from this method.

timeLeft.ToString(@"hh\:mm\:ss\");
timeLeft.ToString(@"hh\:mm\:ss\:");

It should be:

timeLeft.ToString(@"hh\:mm\:ss");

CodePudding user response:

You are trying to convert the Text-Property from your textboxes in your btnStart_Click event-method.

e.g.:Convert.ToInt32(txtHours.Text)

now if the value of txtHours.Text is something like "pommes rot-weiß" the application throws an exception because there is no way you can convert this to an integer.

So you should place a breakpoint at line timeLeft = new TimeSpan(Convert.ToInt32(txtHours.Text), Convert.ToInt32(txtMinutes.Text), Convert.ToInt32(txtSeconds.Text)); and check the actual value of your textboxes

  • Related