Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当我在文本框中来回移动选项卡时,为什么我的表单会关闭_C#_Winforms - Fatal编程技术网

C# 当我在文本框中来回移动选项卡时,为什么我的表单会关闭

C# 当我在文本框中来回移动选项卡时,为什么我的表单会关闭,c#,winforms,C#,Winforms,我的文本框休假事件中有以下代码: private void txtAmount_Leave(object sender, EventArgs e) { int x = Convert.ToInt32(txtAmount.Text); double res = (double)x / 100; txtAmount.Text = "$" + res.ToString(); } 但是,如果通过按下Shift+Tab键并再次移动回同一文本框,然后尝试移动到其他文本框,返回到上一个控件

我的文本框休假事件中有以下代码:

private void txtAmount_Leave(object sender, EventArgs e)
{
   int x = Convert.ToInt32(txtAmount.Text);
   double res = (double)x / 100;
   txtAmount.Text = "$" + res.ToString();
}
但是,如果通过按下Shift+Tab键并再次移动回同一文本框,然后尝试移动到其他文本框,返回到上一个控件,则表单将自动关闭。为什么会发生这种情况?

对的调用可能引发异常,可能是因为无法将
文本框中的字符串转换为整数
Convert.ToInt32()
可以引发以下异常(来自的描述):

  • FormatException
    -值不包含可选符号和数字序列(0到9)
  • OverflowException
    -值表示小于Int32.MinValue或大于Int32.MaxValue的数字
这很可能是
格式异常
,在字符串前面加上
$
或输入任何非数字字符(字母等)后,当您离开
文本框
时抛出。要解决此问题,您有两个选项:

  • 在代码周围添加一个标记以处理任何异常
  • 使用而不是
    Convert.ToInt32()
  • 添加一些代码,防止在
    文本框中输入非数字字符
以下是事件处理程序的改进版本:

private void txtAmount_Leave(object sender, EventArgs e)
{
    string toParse = txtAmount.Text.TrimStart('$');

    int parsed;
    if (Int32.TryParse(toParse, out parsed))
    {
        double res = (double)parsed / 100;
        txtAmount.Text = "$" + res.ToString();
    }
}

第一次离开此文本框时,其内容将更改为“$123”(例如)。第二次离开时,尝试将其转换为int将引发异常

假设您的目标是只显示一个不带小数点的货币值,那么您可以使用带
NumberStyle
的重载

double number;

if (double.TryParse(txtAmount.Text, NumberStyles.Currency, CultureInfo.CurrentUICulture, out number))
{
    txtAmount.Text = number.ToString("C0");
}
else
{
    MessageBox.Show("Could not convert.");
}
在阅读了@Donut对其答案的评论后,我不确定您的目标是什么。如果要截断美分,但仍显示“.00”,可以执行以下操作:

txtAmount.Text = ((int)number).ToString("C");
或者这样做,这将使:

txtAmount.Text = (Convert.ToInt32(number)).ToString("C");
如果这没有帮助,请澄清并让我们知道您正在努力实现的目标

试试这个:

private void txtAmount_Leave(object sender, EventArgs e)
{
    if (!String.IsNullOrEmpty(txtAmount.Text) && txtAmount.Text.StartsWith("$"))
        txtAmount.Text = txtAmount.Text.Substring(1);

    int x = Convert.ToInt32(txtAmount.Text);
    double res = (double)x / 100;

    txtAmount.Text = "$" + res.ToString();
}

在尝试将$sign转换为int之前,请检查txtmount.Text中的$sign。如果存在$sign,请将其去掉,然后将其余输入转换为int


此外,您可能需要使用Int32.TryParse方法,因为这将帮助您确定输入的文本是否可以转换为int。

在使用数字之前,您可能需要更改代码以删除$:

private void txtAmount_Leave(object sender, EventArgs e)
{
   int x = 0;
   int.TryParse(txtAmount.Text.Replace("$", string.Empty), out x);
   double res = (double)x / 100;
   txtAmount.Text = "$" + res.ToString();
}
我找到了解决办法

      private void txtAmount_Leave(object sender, EventArgs e)
    {
        string strAmnt = string.Empty;
        strAmnt = txtAmount.Text;
        if (strAmnt.Contains(".") && !strAmnt.Contains("$"))
        {

            txtAmount.Text = "$" + strAmnt;
            while (txtAmount.Text.Length - txtAmount.Text.IndexOf(".") <= 2)
            {
                txtAmount.Text += "0";
            }
        }
        else
            if (strAmnt.Contains("$") && !strAmnt.Contains("."))
            {
                Int64 amnt = 0;
                strAmnt = strAmnt.Replace("$", "");
                try
                {
                    amnt = Convert.ToInt64(strAmnt);
                    double amt = (double)amnt / 100;
                    txtAmount.Text = "$" + amt.ToString();
                }
                catch (FormatException ie)
                {
                    MessageBox.Show("Invalid Format");
                }
            }
            else
                if (!strAmnt.Contains(".") && !strAmnt.Contains("$"))
                {
                    try
                    {
                        int x = Convert.ToInt32(txtAmount.Text);
                        double res = (double)x / 100;
                        txtAmount.Text = "$" + res.ToString();
                        while (txtAmount.Text.Length - txtAmount.Text.IndexOf(".") <= 2)
                        {
                            txtAmount.Text += "0";
                        }
                    }
                    catch (FormatException ie)
                    {
                        MessageBox.Show("InvalidFormat");
                    }
                }
        while (txtAmount.Text.Length - txtAmount.Text.IndexOf(".") <= 2)
        {
            txtAmount.Text += "0";
        }
    }
private void txtmount\u Leave(对象发送方,事件参数e)
{
string strAmnt=string.Empty;
strAmnt=txtAmount.Text;
if(strAmnt.Contains(“.”&!strAmnt.Contains($”)
{
txtAmount.Text=“$”+strAmnt;

while(txtmount.Text.Length-txtmount.Text.IndexOf(“.”)你必须给我们更多的代码。没有任何东西可以解释发生了什么。你有没有尝试捕获抛出的异常,而不仅仅是未捕获的异常?你可以在Debug->exceptions中执行此操作…只有在编写此事件之后,我才发现问题,然后它才能正常工作,所以你没有看到任何异常?好的,为了克服这个问题,我想问一下e至do@Dorababu例如,如果
txtmount.Text
123.99
,你想把它改成什么:
$123.99
$123.00
$124.00
$124
,或者别的什么?如果只有一个plai,我将不会对这些进行转换n给定金额,然后我将其转换为$value这也会在我来回移动多次时引发错误,因为输入字符串的格式不正确,甚至正如我所说的那样跳过。这似乎很好,但如果我最初给出的金额为500,它将转换为$5,如果我再次返回并离开,它将转换为$0.05。那么,您认为呢试着让它去做吗?你最初写的代码是每次文本框失去焦点时尝试将值除以100,这是预期的行为吗?如果不是,是什么?除以100很好。我假设OP试图取整为一个整数,但现在我不确定实际目标是什么…@adrift Mmhmm。我认为OP需要重新思考一下他到底想完成什么。@dorababu:很抱歉……我编辑了我的答案以消除错误。