简体   繁体   中英

C# timer control - good practice?

I'm building an application which basically has a lot of user input, text boxes, combo boxes etc. I came across a problem involving input validation, and basically my solution was to call a method(which checks textbox input) within the timer.tick method.

The method:

    private void AllowCreate()
    {
        if (firstNameText.Text == String.Empty || lastNameText.Text == String.Empty
             || descriptionText.Text == String.Empty)
        {
            createButton.Enabled = false;
        }
        else
        {
            createButton.Enabled = true;
        }
    }

So every tick, the method is called and checks if the text boxes are empty.

My question is: Is using a timer in this way, good practice? If not, are there more efficient ways of accomplishing what I am trying to do? Thanks.

I would not use these method.
I always catch TextChanged event for TextBoxes and SelectedIndexChanged for ComboBoxes and call check routine from there, enabling or disabling the button.

Basically, if you send all the events to

private void Something_Changed(object sender, EventArgs e)
{
    createButton.Enabled = 
        !String.IsNullOrEmpty(firstNameText.Text) &&
        !String.IsNullOrEmpty(lastNameText.Text) &&
        !String.IsNullOrEmpty(descriptionText.Text);
}

you've done.

Using timers can create a slightly strange user experience, as I can change a field, then the next field, then suddenly I get a validation error on the first field, which I fix but the error doesn't go away for a while. Setting the timout shorter helps with this, but increases load unnecessarily when nothing is changing.

Like the others have said, use the validation events - they are there for a reason.

If you want to do all your validation in one place, that's okay too, just have one big "CheckValidation()" method and call that from each TextChanged event, or wherever you feel the need to re-validate.

Is there a reason you can't use the textchanged event for this?

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.textchanged.aspx

This is not the right way because it will validate input unnecessarily

You should either

  • Validate the input when user do some action that need values in textbox or combo box. For example user presses save button. Validate the input. If they are rght save otherwise tell user the error

    or

  • validate once the value of textbox or combo box has been changed

If you are not actually doing any particular validation its better to move your current code in to the createButton click event handler and if they are empty pop up a message.

If the fields are empty don't proceed with the code execution and notify the user instead of checking it in a timer (user might be having his coffee :) )

We have various requirements to use a timer style of functionality. However we have been avoiding Timer component. Instead we user Quartz Server. Quartz.NET is a full-featured, open source job scheduling system that can be used from smallest apps to large scale enterprise systems.

Quartz is indeed a powerful library, but for simple requirements the Timer API can save the day, keeping you from putting unnecessary complexity into the system. You should think of using the Timer API in cases where you don't have a lot of tasks that need to be scheduled, and when their execution times are well-known (and unchangeable) in the design process. In these situations, you don't have to worry whether some tasks are lost because of a shutdown or crash. For more sophisticated needs, Quartz is an elegant scheduling solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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