【问题标题】:System.InvalidCastException: 'Unable to cast object of type 'System.Windows.Forms.TextBox' to type 'System.IConvertibleSystem.InvalidCastException:'无法将'System.Windows.Forms.TextBox'类型的对象转换为'System.IConvertible'类型
【发布时间】:2019-11-18 21:01:08
【问题描述】:

尝试在 C# 上创建一个点钞机并收到一个名为 costofcredit 的整数的错误,我已将其声明为一个 int,然后我点击了一个按钮,它会在此按钮上添加 1p,我有代码的
costofcredit = Convert.ToInt32(textCPC); 这就是我得到以下消息的地方是我的整个代码

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!"); 
            }

【问题讨论】:

  • 更改为 Convert.ToInt32(textCPC.Text);
  • 哈哈哈!伙计,你改变了整个问题。如果你有其他问题,你不能这样做打开一个新线程,是的,请明确问题,这里没有人知道你面临什么问题,因为你没有附加任何代码 sn-p 或屏幕截图,没有那个是怎么回事应该知道问题可能出在哪里?
  • 我不能再问另一个问题 3 天所以不要等待

标签: c#


【解决方案1】:

因为textCPCTextBox 类没有实现System.IConvertible 接口。

当你使用Convert.ToInt32时,对象需要实现System.IConvertible

Convert.ToInt32(textCPC.Text); 

而不是

Convert.ToInt32(textCPC);

【讨论】:

  • 现在得到 System.FormatException: '输入字符串的格式不正确。'信贷成本
  • 你的输入数据是什么?
  • 什么是抱歉知之甚少
  • @dave 请看我的回答,我提供了如何使用System.Int32.TryParse()的信息
  • @dave 我的意思是你的textCPC.Text 价值是什么
【解决方案2】:

正如我在 cmets 中已经说过的,您需要像 Convert.ToInt32(textCPC.Text) 那样转换 textCPC 的 .Text 属性,如果文本框 textCPC 的 Value 是整数值,则可以使用,看看您如何不使用用于异常处理的尝试捕获,您可能需要使用System.Int32.TryParse,以防用户输入非整数:

//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 

【讨论】:

  • 有关System.Int32.TryParse() 的文档可在此处找到 (msdn.microsoft.com/en-us/library/f02979c7(v=vs.110).aspx)
  • 但是我已经在顶部声明了 textcpc 为 textcostpercredit int 那么如果我已经声明了它为什么不能工作
  • @dave 你是什么意思?我不明白您所说的“在顶部将 textcpc 声明为 textcostpercredit int 那么为什么它不起作用”,为了让System.Convert 正确执行,您的文本框中的字符串值必须是一个有效的整数值,例如“0”, “1”、“10”等,如果是“abc”或“10.45”等,它们不是有效的整数值,转换将失败并抛出异常。
  • 如何将字符串值添加到我的整数中
  • @dave 您将字符串转换为整数,然后将其设置为您的 costofcredit 变量,我的回答只是考虑了用户错误,其中输入的值不是有效的整数。 System.Int32.TryParse() 将它需要尝试解析的值作为第一个参数和一个整数变量,当它成功时使用解析值设置,该方法本身返回布尔值 true 或 false,因此我的答案中的 if-else 语句,如果解析成功,则将 costofcredit 设置为整数输出变量 outIntCheck,如果解析文本框值失败,则显示错误。
【解决方案3】:

您正在尝试将textCPC 本身(它是一个TextBox 控件)转换为整数值,而您应该使用textCPC.Text 转换它的值。所以换个方式

Convert.ToInt32(textCPC); 

Convert.ToInt32(textCPC.Text); 

尽量使用[Int32.TryParse]1,这样当你输入不正确的数据时就不会出现异常。或者只是在 try/catch 语句中使用它。

【讨论】:

  • 输入数据必须是整数类型。
【解决方案4】:

您正在使用textCPC,我认为它是一个文本框。您需要 TextBox 的值,因此应使用 textCPC.Text

【讨论】:

  • 现在得到 System.FormatException: '输入字符串的格式不正确。'信贷成本
【解决方案5】:
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;
        }



    }

【讨论】:

  • 欢迎来到 Stack Overflow。虽然您的代码可能会提供问题的答案,但请在其周围添加上下文,以便其他人了解它的作用以及它存在的原因。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-17
  • 2012-03-14
  • 1970-01-01
  • 1970-01-01
  • 2020-12-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多