PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, October 28, 2022

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

 October 28, 2022     c#, integer, is-empty, null, textbox     No comments   

Issue

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

Solution

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:.

How the int.TryParse actually works

You can also simply set the button disable by default instead and add this handler on the TextChanged event:

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

And also do the int.TryParse here instead too:

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


Answered By - user12031933
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

1,215,539

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © 2025 PHPFixing