简体   繁体   中英

System.InvalidCastException: 'Unable to cast object of type 'System.Windows.Forms.TextBox' to type 'System.IConvertible

Trying to make a money counter on C# and getting an error with an integer called costofcredit, i have declared this as an int I then have a button which i click and it will add 1p on and within this button i have the code of
costofcredit = Convert.ToInt32(textCPC); and this is where i get the message below is my whole code

if (textCPC.Text != "0") 
            {
                onepence = onepence + 1
                label1p.Text = onepence.ToString(); 
                totalpence = totalpence + 1; 
                textTPV.Text = totalpence.ToString(); 
                totalpounds = totalpence / 100; 
                textTPVal.Text = totalpounds.ToString("n2"); 
                costofcredit = Convert.ToInt32(textCPC); 
                amountofcredits = Convert.ToInt32(totalpence) / costofcredit; 
                textAPC.Text = amountofcredits.ToString(); 
            else 
            {
                MessageBox.Show("Please enter the cost per credit!"); 
            }

Because textCPC TextBox class did't implement System.IConvertible interface.

When you use Convert.ToInt32 the object need to implement System.IConvertible

Convert.ToInt32(textCPC.Text); 

instead of

Convert.ToInt32(textCPC);

You are using textCPC which I believe is a TextBox. You need value of the TextBox so should use textCPC.Text .

You are trying to convert the textCPC itself (which is a TextBox control) to an integer value while you should convert it's value by using textCPC.Text . So just change

Convert.ToInt32(textCPC); 

To

Convert.ToInt32(textCPC.Text); 

Try to use [Int32.TryParse] 1 so that when you input incorrect data it won't through an exception. Or just use it inside a try/catch statement.

As I already said in the comments, you need to convert the .Text property of textCPC like so Convert.ToInt32(textCPC.Text) which if the Value of textbox textCPC is an integer value will work, seeing how you aren't using a try catch for exception handling you may want to use System.Int32.TryParse in case the user enters something non-integer:

//Declare an out parameter of type int
int outIntCheck = 0;

//Check to see if you can successfully parse an integer value
if(System.Int32.TryParse(textCPC.Text, out outIntCheck)
   costofcredit = outIntCheck;
else
   //Show incorrect integer format error 
private void saveButton_Click(object sender, EventArgs e)

{

        TaskEmployee taskEmployee = new TaskEmployee();
        TaskEmployeeBLL taskEmployeeBLL = new TaskEmployeeBLL();
        Task1 task1 = new Task1();

        int employeeid = Convert.ToInt32(employeeNameShowComboBox.SelectedValue);

        bool save = false;

        foreach (ListViewItem itemRow in taskShowListView.Items)
        {

            if (itemRow.Selected == true)
            {
                int taskId = Convert.ToInt32(itemRow.SubItems[0].Text);
                string taskDate = itemRow.SubItems[1].ToString();
                string taskDescription = itemRow.SubItems[2].ToString();

                task1.TaskID = taskId;
                task1.TaskDate = taskDate;
                task1.TaskDescription = taskDescription;

                taskEmployee.EmployeeId = employeeid;
                save = taskEmployeeBLL.TaskEmployeeSaveShow(taskEmployee, task1);

            }

        }


        if (save)
        {
            MessageBox.Show("save success");

        }
        else
        {
            MessageBox.Show("Don't save");
            return;
        }



    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM