none
Visual Basic 2010 Form Load and a TimeMaskedTextBox

    Question

  • Hi Good People

    I have a form in design view and when the form loads, I want the TimeMaskedTextBox to show the time now. But allow the user to change the time for later time in the day.

    Let's say the time now is: 09:00 and the user wishes to change time to: 22:00 and when the form is saved, And a new form is loaded then the TimeMaskedTextBox will go back to the time now. How would the code be written.

    Kind Regards

    Gary 


    Gary Simpson


    Sunday, April 17, 2011 1:27 AM

Answers

  • You haven't indicated what the mask is, so it's not possible to match the time string to your mask, but if the mask was 90:00LL, for instance, then the code would be something like:

        TimeMaskedTextBox.Text = Now.ToString("hhmmtt")


    • Marked as answer by Gary Simpson Saturday, April 23, 2011 6:33 PM
    Sunday, April 17, 2011 1:53 AM
  • Hi Acamar

    I have clicked on the TimeMaskedTextbox on the form in design view, And then clicked the tab top right of the TimeMaskedTextbox, I got A dropdown, Then I cliked "set mask" which took me to Imput mask menu. I have selected Time european/Military

    and in the Mask I have put "00:00" and the preview say's " __:__ " is this the way to set the Mask.

    I have put the code "TimeMaskedTexbox.text = Now.ToString ("hhmm")" Into the Form Load, But Nothing happen's. So I tried putting the Code in Timer2_Tick and when the form loads sure enough the actual time is in the TimeMaskedTextBox. But if I go to change the time "It will not let me"

    How Can I get around this Problem

    Kind Regards

    Gary


    Gary Simpson
    • Marked as answer by Gary Simpson Saturday, April 23, 2011 6:32 PM
    Sunday, April 17, 2011 3:43 PM
  • If you want the time mask text box to show am or pm, the mask will have to be 90:00LL.

    If you put that line into the form load event when the mask is set as suggested then the time will be displayed in the text box when the form loads.  I have not tested it with the alternate mask and format string, as you indicated that you wanted am and pm included. If it doesn't appear you need to determine the reason.  Perhaps you have some code that is overwriting it later in the form load procedure.  You have already confirmed that the code is OK by putting it in the timer tick event, but of course if you include it there then the current time will override whatever the user enters each time the timer ticks. Put a breakpoint in the form load event to make sure it is really being executed when the form loads.

    • Marked as answer by Gary Simpson Saturday, April 23, 2011 6:33 PM
    Sunday, April 17, 2011 9:37 PM
  • Hi Acamar

    The am/pm I have put in the code above Because some people can not work with a 24hr clock. I do not want am or pm included, it was just to help people answering my question. sorry for any confusion. so will the mask be 90:00 without the LL

    Best Regards

    Gary 


    Gary Simpson
    • Marked as answer by Gary Simpson Saturday, April 23, 2011 6:33 PM
    Sunday, April 17, 2011 11:01 PM
  • If you don't want am or pm do display in the masked text box then change the mask to 90:00 and change the time format to hhmm, but if you think people might be confused between am and pm then the format should be Hmm or HHmm.
    • Marked as answer by Gary Simpson Saturday, April 23, 2011 6:33 PM
    Sunday, April 17, 2011 11:28 PM

All replies

  • You haven't indicated what the mask is, so it's not possible to match the time string to your mask, but if the mask was 90:00LL, for instance, then the code would be something like:

        TimeMaskedTextBox.Text = Now.ToString("hhmmtt")


    • Marked as answer by Gary Simpson Saturday, April 23, 2011 6:33 PM
    Sunday, April 17, 2011 1:53 AM
  • Hi Acamar

    I have clicked on the TimeMaskedTextbox on the form in design view, And then clicked the tab top right of the TimeMaskedTextbox, I got A dropdown, Then I cliked "set mask" which took me to Imput mask menu. I have selected Time european/Military

    and in the Mask I have put "00:00" and the preview say's " __:__ " is this the way to set the Mask.

    I have put the code "TimeMaskedTexbox.text = Now.ToString ("hhmm")" Into the Form Load, But Nothing happen's. So I tried putting the Code in Timer2_Tick and when the form loads sure enough the actual time is in the TimeMaskedTextBox. But if I go to change the time "It will not let me"

    How Can I get around this Problem

    Kind Regards

    Gary


    Gary Simpson
    • Marked as answer by Gary Simpson Saturday, April 23, 2011 6:32 PM
    Sunday, April 17, 2011 3:43 PM
  • If you want the time mask text box to show am or pm, the mask will have to be 90:00LL.

    If you put that line into the form load event when the mask is set as suggested then the time will be displayed in the text box when the form loads.  I have not tested it with the alternate mask and format string, as you indicated that you wanted am and pm included. If it doesn't appear you need to determine the reason.  Perhaps you have some code that is overwriting it later in the form load procedure.  You have already confirmed that the code is OK by putting it in the timer tick event, but of course if you include it there then the current time will override whatever the user enters each time the timer ticks. Put a breakpoint in the form load event to make sure it is really being executed when the form loads.

    • Marked as answer by Gary Simpson Saturday, April 23, 2011 6:33 PM
    Sunday, April 17, 2011 9:37 PM
  • Hi Acamar

    The am/pm I have put in the code above Because some people can not work with a 24hr clock. I do not want am or pm included, it was just to help people answering my question. sorry for any confusion. so will the mask be 90:00 without the LL

    Best Regards

    Gary 


    Gary Simpson
    • Marked as answer by Gary Simpson Saturday, April 23, 2011 6:33 PM
    Sunday, April 17, 2011 11:01 PM
  • If you don't want am or pm do display in the masked text box then change the mask to 90:00 and change the time format to hhmm, but if you think people might be confused between am and pm then the format should be Hmm or HHmm.
    • Marked as answer by Gary Simpson Saturday, April 23, 2011 6:33 PM
    Sunday, April 17, 2011 11:28 PM