Spirit
Spirit

Reputation: 150

Enable and disable buttons dynamically on window

I'm trying to update a custom message box I made (Inherited from Window)

The solution im trying to find is to show the messagebox with the buttons disabled, until a condition changes, then enable them again to allow the program to continue.

Ive tried using the "show" method instead of show dialogue, but then the context does not get displayed.

Please see below for my current approach:

TPromptWindow w_continue;
bool b_enableBtn = false;

do {
w_continue = new TPromptWindow("Please Wait", "OK", "Cancel", b_enableBtn);
w_continue.ShowDialog();
} 
while ( conditionBoolean );
w_continue.Close(); // I know this doesnt work, but just tryign to show my intentions

b_enableBtn = true;
w_continue = new TPromptWindow("Please Wait", "OK", "Cancel", b_enableBtn);
w_continue.ShowDialog();

The first problem with my solution is that the first message shown will not self destroy, it always waits on the user input. I tried calling "Close" after the do while statement but that wouldn't close down the first dialogue to show the next one with buttons enabled.

Can anyone give me any guidance please?

Upvotes: 1

Views: 971

Answers (1)

Your dialog will stop the code from working until you confirm it. I think for what you want to do, you need to start a thread, block the current input with the dialog and when the thread with "the work to do" has finished close the dialog or continue.

With the code you wrote, you can not exit the do while loop.

Try looking at this: https://msdn.microsoft.com/en-us/library/aa645740(v=vs.71).aspx

I have a program where I do something in the background and I display that the user should wait with a progressdialog. You could replace this progressdialog with any other dialog you want. You just need to create a form and make it modal so it blocks the input. This is done with the locked attribute. So here is an example how i do it:

//This is a bool to see if some other process is already running.
if (isProcessRunning)
{
     MessageBox.Show("Currently someone else is working!!!");
     return;
}`var progressDialog = new ProgressDialog();
    //if you don't have a maximum value because you don't know how long it will take you should set indeterminate to true.
    progressDialog.setMaximum(<maximum value>);
    progressDialog.Text = "<what are you doing>";
    var backgroundThread = new Thread(() =>
            {
                isProcessRunning = true;
                //this does not have to be a loop, you can also do other stuff you just have to set you bar to indeterminate then.
                foreach (<do my stuff>)
                {
                    <do stuff here that takes time>
                    progressDialog.IncrementProgress();
                }

                //Close the progressdialog threadsave.
                if (progressDialog.InvokeRequired)
                    progressDialog.BeginInvoke(new Action(() => progressDialog.Close()));

                isProcessRunning = false;
            });

        backgroundThread.Start();
        progressDialog.ShowDialog();

This should solve your problem. If you need the progressdialog. Don't forget that when you access your dialog you need to take care that it is threadsave. For example to increment the progress you do it like that:

public void IncrementProgress()
    {
        if (progressBar1.InvokeRequired)
        {
            progressBar1.BeginInvoke(
                new Action(() =>
                {
                    progressBar1.Value++;
                }
                    ));
        }
        else
        {
            progressBar1.Value++;
        }
    }

I hope I could help you with that.

Upvotes: 1

Related Questions