-1

I'm trying to prevent a user from entering no value in a text box so as to prevent an error during code execution.

The text box values are assigned a default value like this:

        wrkTbx.Text = "00 : 00 : 00 : 000";
        restTbx.Text = "00 : 00 : 00 : 000";

This is how I'm trying to handle the user inputting no value in the textbox but the code still runs and doesn't show the message box to signal no input.

Does anyone know how handle the null input? as the below code doesn't work.

 //Assign text box time string to string variables.
    //check if string is empty.
    if (String.IsNullOrEmpty(wrkTbx.Text) && String.IsNullOrEmpty(restTbx.Text))
    {
        MessageBox.Show("Please enter a work/rest interval");

    }
    else
    {
        rstString = restTbx.Text;
        wrkString = wrkTbx.Text;

    }
Brian Var
  • 6,029
  • 25
  • 114
  • 212
  • Why doesn't it work? If the Text is not null or empty, what _is_ being returned? If the user changes nothing, it will return the default value, so you would need to add that to your conditional statement. – AdamMc331 Oct 15 '14 at 16:57
  • 2
    If those values are assigned by default, and the user changes nothing, then they aren't null. – DonBoitnott Oct 15 '14 at 16:57
  • 1
    `Brian J` do you understand the meaning of Empty and Null...? if perhaps you need to change your logic to if (wrkTbx.Text == default value which you should assign to a property , Constant, or Variable then change the && condition to an || that would be more feasible. – MethodMan Oct 15 '14 at 17:02
  • @DJKRAZE, I was in a rush posting this question and in hindsight I should have just implemented your solution which has solved the issue. Could you post your solution as an answer below as it may come in helpful to other newbies. – Brian Var Oct 15 '14 at 19:24
  • `Brian J` can you edit the question and show the new implementation code.. your logic must be off or you are not using `return;` properly or in the correct spot within your code – MethodMan Oct 15 '14 at 19:40

1 Answers1

0

Brian J per your request

do you understand the meaning of Empty and Null...?

perhaps you need to change your logic to if (wrkTbx.Text == default value which you should assign to a property , Constant, or Variable then change the && condition to an || that would be more feasible

declare a const string in your class for example

    public const string constWrkTxt = "00 : 00 : 00 : 000";
    public const string constRestTxt = "00 : 00 : 00 : 000";

in your method check the following

 //check if wrkTbx.Text == constWrkTxt || constRestTxt
    if (wrkTbx.Text == constWrkTxt) || restTbx.Text == constRestTxt ))
    {
        MessageBox.Show("Please enter a work/rest interval");
        wrkTbx.Focus();
        //or to stop the flow / continuation use return;
    }
    else
    {
        rstString = restTbx.Text;
        wrkString = wrkTbx.Text;

    }

depending on how and where you declared rstString and wrkString you may eliminate and just use the values from the textbox when the condition passes

MethodMan
  • 18,625
  • 6
  • 34
  • 52
  • I've implemented this solution and now the messagebox shows when there is no input, but now I'm getting an argumentNullException when I assign the null string to a TimeSpan object: TimeSpan workTm = TimeSpan.ParseExact(wrkString, @"hh\ \:\ mm\ \:\ ss\ \:\ fff", CultureInfo.InvariantCulture); How do I prevent the code from continuing on to this assignment if there is no input? – Brian Var Oct 15 '14 at 19:32
  • For when the condition is met and the message box show the message. How can I stop the code from continuing and assigning the null wrkString to the DateTime object? I should be looping back to the user input if they are null instead of continuing execution. – Brian Var Oct 15 '14 at 19:35
  • For your new issue or problem please post the code in your original question it's hard to read here in the comment section.. also not that I have given you an little example on what you can do as well hope it makes sense.. – MethodMan Oct 15 '14 at 19:38
  • I run the following in my console app using the const and I get no errors `TimeSpan workTm = TimeSpan.ParseExact(constWrkTxt, @"hh\ \:\ mm\ \:\ ss\ \:\ fff", CultureInfo.InvariantCulture);` do not assign a null string assign the default for example `"00 : 00 : 00 : 000"` – MethodMan Oct 15 '14 at 19:44
  • I tried a return after the message but this just keeps looping back to the messagebox. So when I click ok it keeps coming up and the throws the argumentNullException. I'll post an edit in my question shortly. – Brian Var Oct 15 '14 at 19:44
  • also if you want default TimeSpan object just use the following for example `TimeSpan workTm = new TimeSpan();` – MethodMan Oct 15 '14 at 19:48
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/63129/discussion-between-dj-kraze-and-brian-j). – MethodMan Oct 15 '14 at 19:48
  • you really need to show the code so that we can fully understand what it is that you are doing.. the behavior you're explaining sounds like something else is going on – MethodMan Oct 15 '14 at 19:49
  • This question ontains the core implementation of my code: http://stackoverflow.com/questions/26384491/how-to-return-control-of-flow-to-a-for-loop?noredirect=1#comment41434186_26384491 – Brian Var Oct 15 '14 at 20:02
  • I am going to ask if you have stepped thru the code..? this sounds like this is where you need to start.. also having 2 different links to the same question ...not a good idea.. – MethodMan Oct 15 '14 at 20:05
  • The other question is related to loop execution but valid point. – Brian Var Oct 15 '14 at 20:09
  • I still think no matter how big the code posting the relevant code will help.. seriously sounds like you need to step thru the code in the looping section I would start there – MethodMan Oct 15 '14 at 20:11
  • I'm wondering now how I would prevent non numeric input into the text box or how could I handle it? – Brian Var Oct 16 '14 at 21:11
  • are you familiar with the `if (char.IsDigit()) ` check the characters in a for loop – MethodMan Oct 16 '14 at 21:29
  • would it be better practice to use a regex on the input? – Brian Var Oct 16 '14 at 21:46
  • this is very simple actually so what you would want to do is if it's on the Key Press Event.. check the value of what was typed look at your e. args ... from there check the `if(char.IsDigit('whatever Char`)){ }` – MethodMan Oct 16 '14 at 21:47
  • ok how well do you know / understand RegEx.. C# has all the functionality that you need you can even use Linq if you want ... – MethodMan Oct 16 '14 at 21:47
  • here is what you can do to Allow Numbers I want you to figure out the opposite using this example `private void YourTextBox_KeyPress(object sender, KeyPressEventArgs evtargs) { e.Handled = (!char.IsDigit(evtargs.KeyChar)) && (!char.IsControl(evtargs.KeyChar)); }` – MethodMan Oct 16 '14 at 21:50
  • So I need to create a key press event for textbox? And inside the event the code is checking if the char isn't a digit it's throwing an error? Do I then just show a messagbox and print the exception e? I'm a bit confused as to how its implemented.. – Brian Var Oct 16 '14 at 21:59
  • I don't mind helping you but I think that opening up a new question with code would truly benefit you.. because the original post deals with a different issues that I have helped you resolve.. I think that others may get confused based now on your new issue.. make sense..? – MethodMan Oct 17 '14 at 14:52
  • here is a link that can give you multiple ideas http://stackoverflow.com/questions/4524957/how-to-check-if-my-string-only-numeric – MethodMan Oct 17 '14 at 14:54
  • Thanks, I'll post a new question if I can't implement a solution in the mean time. – Brian Var Oct 17 '14 at 16:58
  • this is actually very simple in nature .. but keep in mind for the future post that when posting a question of any kind, that you also have some supporting coding efforts to go along with what you have or what you have tried.. ok.. – MethodMan Oct 17 '14 at 16:59