Click here to Skip to main content
15,880,392 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I made 4 Text boxes in that first text box is for entering Name and 2,3,4 are for entering Date in format DD-MM-YYYY. i want to specify an exception for name if user inputs Null or Numbers in that box. And an exception to handle characters input in date text boxes and also length, that means if user inputs numbers not in the correct format.please help me how to specify those exceptions.Merry Christmas

VB
Dim i As Integer = 0
       Dim l As Integer = 0
       Dim Nm As String

       Nm = (TextBox3.Text).ToString
       Dim lcn As String = ""
       If String.IsNullOrEmpty(textBox4.Text) AndAlso String.IsNullOrEmpty(textBox5.Text) AndAlso String.IsNullOrEmpty(textBox6.Text) Then
           MessageBox.Show("Please Enter Name & Date of Birth!!!")
           Return
       End If
Posted

Why stuff everything in one piece of unreadable code?

First of all, simply use a DateTimePicker control for dates. It makes it a lot easier for you and the users of the application.

Second, just make a function for checking if the value is correct. You can also add a simple function that shows a message based on a condition. Just somewhat like assert would.
Function ShowErrorIf(isValid as boolean, ShowThisErrorMessage as string)
 if not isValid then 
    MessageBox.Show(ShowThisErrorMessage)
 end if
End function

...
if ShowErrorIf(String.IsNullOrEmpty(txtBirthdate.Text), "Please enter a valid birthdate") and
   ShowErrorIf(String.IsNullOrEmpty(txtName.Text), "Please enter a valid name") and
   ... etc...


Good luck!
 
Share this answer
 
If it is absolutely required to use textbox to enter date then go for it, but otherwise I'd suggest for date just use DateTimePicker control and whatever value the user has to input, in a button event handler method or any event handler method where you will use the date u can retrieve it by:

C#
DateTime birthday = dateTimePicker1.Value;


This way you don't have to worry about exceptions or formats the user enters the date.

And for handling exceptions in the textbox, its best to make another method where you pass the input text as a string parameter and make some if-else statements to check them and forcefully throw an exception, for example if this is your button click event handler method:

C#
public void Button1_Click(Object sender, EventArgs e)
{
    try
    {
        //This is the string input from the textbox
        string inputText = textBox1.Text;

        //This is the datetime input from the DateTimePicker controls
        DateTime date1 = dateTimePicker1.Value;
        DateTime date1 = dateTimePicker2.Value;
        DateTime date1 = dateTimePicker3.Value;

        //Then call the method where you are checking for the exceptions
        CheckExceptionsMethod(inputText);
    }
    catch(Exception ex) //If any custom exception is thrown it will go to the catch method
    {
        MessageBox.Show(ex.Message); //This will display the custom exception message
    }
}

public void CheckExceptionsMethod(string inputText)
{
    if(inputText.Equals("") || inputText.Text == null) //double checking if there is null value
    {
        //Throwing custom exception type of NullEntryException
        throw(new NullEntryException("Please enter a value in the textbox.", null));
    }
    else if(inputText.Contains("0123456789")) //This will be true if any number exists in text
    {
        //Throwing custom exception type of NumberEntryException
        throw(new NumberEntryException("Please do not enter numbers in the textbox.", null));
    }
}


Then the last task is for you to create two custom classes for the exceptions you have thrown, because they do not really exist I made them up for you. You can make any custom exception class by inheriting the ApplicationException class. So create two new classes as following, very short coding so no worries. Just inherit the ApplicationException class and make a constructor because when I am throwing the exception I was initializing an object of these classes.

C#
public class NullEntryException : ApplicationException
{
    public NullEntryException(string Message, Exception Inner): base(Message, Inner)
    {
        //No need to write any code, the base class is called so it will take care of it
    }
}

public class NumberEntryException : ApplicationException
{
    public NumberEntryException(string Message, Exception Inner): base(Message, Inner)
    {
        //No need to write any code, the base class is called so it will take care of it
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900