如何在 C# 中使字符串为空以转换为文本框
How to make String empty to converted textbox in c#
Here is the form i made
当我在文本框中输入一个空值时,我遇到异常未处理的页面。我想在用户向文本框输入空值时显示错误消息框,我该怎么做?
namespace Random_çalışması
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Random rnd = new Random();
int number;
int answer;
private void button1_Click(object sender, EventArgs e)
{
number = rnd.Next(1, 101);
label1.Text = number.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
answer = Convert.ToInt32(textBox1.Text);
if(answer == number)
{
MessageBox.Show("Welcome to the page", "Welcome!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else if(String.IsNullOrEmpty(textBox1.Text))
{
MessageBox.Show("Icorrect entrance", "You can not acces to the page", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
MessageBox.Show("Icorrect entrance", "You can not acces to the page", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
只需在方法开头添加空测试即可:
private void button2_Click(object sender, EventArgs e)
{
if ( textBox1.Text == "" )
{
MessageBox.Show(...);
return;
}
answer = Convert.ToInt32(textBox1.Text);
...
}
您也可以使用 int.TryParse()
而不是 Convert
来更好地捕获转换错误:.
How the int.TryParse actually works
您也可以简单地将按钮设置为默认禁用,然后在 TextChanged
事件中添加此处理程序:
private void textBox1_TextChanged(object sender, EventArgs e)
{
button.Enable = textBox1.Text != "";
}
也在这里做int.TryParse
:
private void textBox1_TextChanged(object sender, EventArgs e)
{
button.Enable = textBox1.Text != "" && int.TryParse(textBox1.Text, out _);
}
因此只有在不为空且可转换且用户体验一致的情况下才会启用该按钮。
现在按钮点击处理程序是:
private void button2_Click(object sender, EventArgs e)
{
answer = Convert.ToInt32(textBox1.Text);
if ( answer == number )
MessageBox.Show("Welcome to the page", "Welcome!", MessageBoxButtons.OK, MessageBoxIcon.Information);
else
MessageBox.Show("Icorrect entrance", "You can not acces to the page", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Here is the form i made
当我在文本框中输入一个空值时,我遇到异常未处理的页面。我想在用户向文本框输入空值时显示错误消息框,我该怎么做?
namespace Random_çalışması
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Random rnd = new Random();
int number;
int answer;
private void button1_Click(object sender, EventArgs e)
{
number = rnd.Next(1, 101);
label1.Text = number.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
answer = Convert.ToInt32(textBox1.Text);
if(answer == number)
{
MessageBox.Show("Welcome to the page", "Welcome!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else if(String.IsNullOrEmpty(textBox1.Text))
{
MessageBox.Show("Icorrect entrance", "You can not acces to the page", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
MessageBox.Show("Icorrect entrance", "You can not acces to the page", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
只需在方法开头添加空测试即可:
private void button2_Click(object sender, EventArgs e)
{
if ( textBox1.Text == "" )
{
MessageBox.Show(...);
return;
}
answer = Convert.ToInt32(textBox1.Text);
...
}
您也可以使用 int.TryParse()
而不是 Convert
来更好地捕获转换错误:.
How the int.TryParse actually works
您也可以简单地将按钮设置为默认禁用,然后在 TextChanged
事件中添加此处理程序:
private void textBox1_TextChanged(object sender, EventArgs e)
{
button.Enable = textBox1.Text != "";
}
也在这里做int.TryParse
:
private void textBox1_TextChanged(object sender, EventArgs e)
{
button.Enable = textBox1.Text != "" && int.TryParse(textBox1.Text, out _);
}
因此只有在不为空且可转换且用户体验一致的情况下才会启用该按钮。
现在按钮点击处理程序是:
private void button2_Click(object sender, EventArgs e)
{
answer = Convert.ToInt32(textBox1.Text);
if ( answer == number )
MessageBox.Show("Welcome to the page", "Welcome!", MessageBoxButtons.OK, MessageBoxIcon.Information);
else
MessageBox.Show("Icorrect entrance", "You can not acces to the page", MessageBoxButtons.OK, MessageBoxIcon.Error);
}