Вопрос

The form I am using requires a copy pasted URL. I am trying to have a textChanged event that will check the url as soon as it is pasted, telling the user whether it is valid or invalid. I also want to be able to lock out the textbox when this happens, with a message saying something like "Processing...".

The problem is with the code below, the textbox is never disabled, the program will do the checkUrl() method and the textbox is never disabled even though it is first to execute (I assume it is but the fact there is a function call right underneath it is messing around with something or getting higher priority).

How do I go about making the control visually disabled while the method runs?

private void urlTxtBx_TextChanged(object sender, EventArgs e)
{
    urlTxtBx.Enabled = false;

    checkUrl();

    urlTxtBx.Enabled = true;
}
Это было полезно?

Решение

I think this is happening because the Application needs to complete all the active threads before disabling the TextBox. Please try the following code:

private void urlTxtBx_TextChanged(object sender, EventArgs e)
{
    urlTxtBx.Enabled = false;
    Application.DoEvents();
    checkUrl();
    urlTxtBx.Enabled = true;
}

This will let the UI to be updated. For more details check here.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top