简体   繁体   English

如何在c#中使字符串为空以转换文本框

[英]How to make String empty to converted textbox in c#

Here is the form i made这是我制作的表格

When I enter an empty value in the textbox I meet exception unhandled page.当我在文本框中输入一个空值时,我遇到了异常未处理的页面。 I want to show an error messagebox when user enter an empty value to the textbox how can I do that ?当用户在文本框中输入空值时,我想显示一个错误消息框,我该怎么做?

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

Just add the empty test at the beginning of the method:只需在方法的开头添加空测试:

private void button2_Click(object sender, EventArgs e)
{
  if ( textBox1.Text == "" )
  {
    MessageBox.Show(...);
    return;
  }
  answer = Convert.ToInt32(textBox1.Text);
  ...
}

You can also use int.TryParse() instead of Convert to better catch conversion errors:.您还可以使用int.TryParse()而不是Convert来更好地捕获转换错误:。

How the int.TryParse actually works int.TryParse 的实际工作原理

You can also simply set the button disable by default instead and add this handler on the TextChanged event:您也可以简单地将按钮设置为默认禁用,并在TextChanged事件上添加此处理程序:

private void textBox1_TextChanged(object sender, EventArgs e)
{ 
  button.Enable = textBox1.Text != "";
}

And also do the int.TryParse here instead too:并且也在这里执行int.TryParse

private void textBox1_TextChanged(object sender, EventArgs e)
{ 
  button.Enable = textBox1.Text != "" && int.TryParse(textBox1.Text, out _);
}

Hence the button is enabled only if not empty and convertible and you have a consistent UX.因此,只有当按钮不是空的且可转换并且您具有一致的用户体验时,才会启用该按钮。

Now the button click handler is:现在按钮单击处理程序是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM