Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.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
如何将文本框条目放入while循环?C#_C#_Textbox_While Loop - Fatal编程技术网

如何将文本框条目放入while循环?C#

如何将文本框条目放入while循环?C#,c#,textbox,while-loop,C#,Textbox,While Loop,这基本上就是我要做的。我想允许某人输入他们想要运行特定程序的次数。我不知道如何将数字10改为(textBox1.Text)。如果你有更好的办法,请告诉我。我对编程很陌生 int counter = 1; while ( counter <= 10 ) { Process.Start("notepad.exe"); counter = counter + 1; } int计数器=1; 同时(计数器尝试System.Int32.TryParse(textBox1.Text,o

这基本上就是我要做的。我想允许某人输入他们想要运行特定程序的次数。我不知道如何将数字10改为(textBox1.Text)。如果你有更好的办法,请告诉我。我对编程很陌生

int counter = 1;
while ( counter <= 10 )
{
    Process.Start("notepad.exe");
    counter = counter + 1;
}
int计数器=1;

同时(计数器尝试
System.Int32.TryParse(textBox1.Text,out counterMax)
()将字符串转换为数字


如果转换成功,则返回true;如果转换失败,则返回false(即,用户输入的不是整数)

textBox1.Text将返回字符串。您需要将其转换为int,因为它接受用户输入,您需要安全地执行此操作:

int max;
Int32.TryParse(value, out max);
if (max)
{
    while ( counter <= max ) {}
}
else
{
    //Error
}
intmax;
Int32.TryParse(值,out max);
如果(最大值)
{

while(counter这显示了如何获取用户提供的输入,并安全地将其转换为整数(System.Int32)并在计数器中使用

int counter = 1;
int UserSuppliedNumber = 0;

// use Int32.TryParse, assuming the user may enter a non-integer value in the textbox.  
// Never trust user input.
if(System.Int32.TryParse(TextBox1.Text, out UserSuppliedNumber)
{
   while ( counter <= UserSuppliedNumber)
   {
       Process.Start("notepad.exe");
       counter = counter + 1;  // Could also be written as counter++ or counter += 1 to shorten the code
   }
}
else
{
   MessageBox.Show("Invalid number entered.  Please enter a valid integer (whole number).");
}
int计数器=1;
int UserSuppliedNumber=0;
//使用Int32.TryParse,假设用户可以在文本框中输入非整数值。
//永远不要相信用户的输入。
if(System.Int32.TryParse(TextBox1.Text,out UserSuppliedNumber)
{

而(counter我建议使用MaskedTextBox控件从用户获取输入,这将帮助我们确保只提供数字。它不会约束我们使用
TryParse
功能

设置掩码如下:(可以使用“属性窗口”)

然后像这样使用:

int finalRange = int.Parse(MaskedTextBox1.Text);
int counter = 1;
while ( counter <= finalRange )
{
    Process.Start("notepad.exe");
    counter = counter + 1;
}
int finalRange=int.Parse(MaskedTextBox1.Text);
int计数器=1;

而(计数器使用Try Catch body,如此函数

bool ErrorTextBox(Control C)
    {
        try
        {
            Convert.ToInt32(C.Text);
            return true;
        }
        catch { return false; }
    }

使用

如果你建议改为使用TryParse,那会更好这是一个很好的观点,我会改变它。我的第一个想法是
Int32.Parse()
更适合这样的循环,但是
TryParse
更好。我猜你正在开始,所以我建议使用一个普通的旧for循环,而不是一段时间。没问题。我们都曾经是新的。我希望这个网站在我还是新的时候就存在。
bool ErrorTextBox(Control C)
    {
        try
        {
            Convert.ToInt32(C.Text);
            return true;
        }
        catch { return false; }
    }