C#TryParse TextBox问题 [英] C# TryParse TextBox Issue

查看:81
本文介绍了C#TryParse TextBox问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我是新来的。在我的下面的代码中,我得到了一个异常,其中包含输入字符串格式不正确的其他信息。



我明白将要发生什么在(某种程度上),但我不知道如何解决它。如果multiplyBx(文本框)中有一个数字但在startBx或endBx(另外两个文本框)中没有数字,我会得到上面提到的错误。但我以为我在代码中使用TryParse修复​​了这个问题。在某些测试中,如果所有文本框都是空白的,或者使用字母或者在startBx或endBx中使用数字或者在multiplyBx中使用字母或空格,则TryParse肯定会起作用,防止抛出相同类型的异常。只是当multiplyBx中有一个数字并且空格或者startBx或endBx中的字母时我才会收到错误。



有人可以建议如何解决问题吗?非常感谢。



// ##下面的代码##



 使用系统; 
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
使用 System.Windows.Forms;

命名空间循环
{
public partial class Form1:Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click( object sender,EventArgs e)
{
int outputvalue = 0 ;
bool isNumber = false ;

isNumber = int .TryParse(startBx.Text, out outputvalue);
isNumber = int .TryParse(endBx.Text, out outputvalue);
isNumber = int .TryParse(multiplyBx.Text, out outputvalue);

int answer = 0 ;
listBox1.Items.Clear();


if (!isNumber)
{
MessageBox.Show( 请在文本框中输入数字。);
}

else
{
int loopStart = int .Parse(startBx.Text);
int loopEnd = int .Parse(endBx.Text);
int multiplyBy = int .Parse(multiplyBx.Text);

for int i = loopStart; i < = loopEnd; i ++)
{
answer = multiplyBy * i;
listBox1.Items.Add(i + 乘以 + multiplyBy + = + answer.ToString());



}
}
}

私人 void button2_Click( object sender,EventArgs e)
{
listBox1.Items。明确();
startBx.Clear();
endBx.Clear();
multiplyBx.Clear();
}
}
}

解决方案

 isNumber = < span class =code-keyword> int  .TryParse(startBx.Text, out  outputvalue); 
isNumber = int .TryParse(endBx.Text, out outputvalue);
isNumber = int .TryParse(multiplyBx.Text, out outputvalue);





您覆盖isNumber变量,因此最后一次调用计数。如果你在multiplyBx文本框中输入一个数字,它将继续解析其余部分。



为什么不用int.TryParse来获取数字?



  int  loopStart,loopEnd,multiplyBy; 

if (!int.TryParse(startBx.Text, out loopStart) ||
!int.TryParse(endBx.Text, out loopEnd)||
!int.TryParse(multiplyBx.Text, out multiplyBy))
{
MessageBox.Show( 请在文本框中输入数字。);
}
其他
{
// < span class =code-comment>做你需要做的事。
}


因为你没有使用这个值输出值你可以通过使用&来轻松检查isNumber。

 isNumber =  int  .TryParse( startBx.Text, out  outputvalue)&  int  .TryParse(endBx.Text, out  outputvalue)&  int  .TryParse(multiplyBx.Text, out  outputvalue); 


Hello,

I'm new here. In my code below, I'm getting an "exception" thrown with additional info saying "Input string was not in a correct format".

I understand what is going on (sort of) but I don't know how to resolve it. If there is a number in multiplyBx (a text box) but no numbers in either startBx or endBx (two other text boxes) I get the aforementioned error above. But I thought I fixed this problem using the TryParse in my code. In some tests the TryParse certainly works, preventing the same type of exception being thrown, if you have all the text boxes blank or with letters or with a number in either startBx or endBx and a letter or blank in multiplyBx. It's just when there is a number in multiplyBx and blank or letters in either startBx or endBx I get the error.

Can anyone advise how to resolve the problem please? Many thanks.

// ##Code Below##

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;

namespace Loops
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int outputvalue = 0;
            bool isNumber = false;

            isNumber = int.TryParse(startBx.Text, out outputvalue);
            isNumber = int.TryParse(endBx.Text, out outputvalue);
            isNumber = int.TryParse(multiplyBx.Text, out outputvalue);

            int answer = 0;
            listBox1.Items.Clear();
                        

            if (!isNumber)
            {
                MessageBox.Show("Please enter numbers in the text boxes.");
            }

            else
            {
                int loopStart = int.Parse(startBx.Text);
                int loopEnd = int.Parse(endBx.Text);
                int multiplyBy = int.Parse(multiplyBx.Text);

                for (int i = loopStart; i <= loopEnd; i++)
                {
                    answer = multiplyBy * i;
                    listBox1.Items.Add(i + " multiplied by " + multiplyBy + " = " + answer.ToString());


                    
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            startBx.Clear();
            endBx.Clear();
            multiplyBx.Clear();
        }
    }
}

解决方案

isNumber = int.TryParse(startBx.Text, out outputvalue);
isNumber = int.TryParse(endBx.Text, out outputvalue);
isNumber = int.TryParse(multiplyBx.Text, out outputvalue);



You overwrite the isNumber variable, so the last call counts. If you enter a number in the multiplyBx text box, it'll continue to parse the rest.

Why don't you just use int.TryParse to get the numbers?

int loopStart, loopEnd, multiplyBy;

if (!int.TryParse(startBx.Text, out loopStart) || 
    !int.TryParse(endBx.Text, out loopEnd) || 
    !int.TryParse(multiplyBx.Text, out multiplyBy))
{
    MessageBox.Show("Please enter numbers in the text boxes.");
}
else
{
  // Do whatever you need to do.
}


Since you do not use the value of output value you could easily check on isNumber by using &.

isNumber = int.TryParse(startBx.Text, out outputvalue) & int.TryParse(endBx.Text, out outputvalue) & int.TryParse(multiplyBx.Text, out outputvalue);


这篇关于C#TryParse TextBox问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆