Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the code for ShowAutoClosingTaskDialog in TaskDialogDemo Form1 (vb & c#) #5087

Open
elGuille-info opened this issue Apr 7, 2022 · 0 comments

Comments

@elGuille-info
Copy link

elGuille-info commented Apr 7, 2022

Improve the code for ShowAutoClosingTaskDialog in TaskDialogDemo Form1 (vb & c#)

In the example of ShowAutoClosingTaskDialog it only works well with 5 seconds or less, to make that example easier to understand or if more than 5 seconds of waiting are used, a couple of code changes would have to be made in both Form1.cs as in Form1.vb.

Issue:

The sample code fails when the result of remainingTenthSeconds * 2 is greater than 100.

The sample code fails when a value of more than 5 seconds is used, since when assigning the value of remainingTenthSeconds = seconds * 10, the maximum value of the progress bar turns out to be less than the resulting value of: remainingTenthSeconds * 2.

The actual code is:

For C#

page.ProgressBar.Value = 100 - remainingTenthSeconds * 2;

For VB:

page.ProgressBar.Value = 100 - remainingTenthSeconds * 2

Solution:

The new update code should be something like this:

For C#:

// Set then maximum value for the ProgressBar
var maximum = remainingTenthSeconds * 2;
page.ProgressBar.Maximum = maximum;

// Create a WinForms timer that raises the Tick event every tenth second.
using (var timer = new Timer()
{
    Enabled = true,
    Interval = 100
})
{
    timer.Tick += (s, e) =>
    {
        remainingTenthSeconds--;
        if (remainingTenthSeconds > 0)
        {
            // Update the remaining time and progress bar.
            page.Text = string.Format(textFormat, (remainingTenthSeconds + 9) / 10);
            page.ProgressBar.Value = maximum - remainingTenthSeconds * 2;
        }
        else
        {
            // Stop the timer and click the "Reconnect" button - this will
            // close the dialog.
            timer.Enabled = false;
            reconnectButton.PerformClick();
        }
    };

    TaskDialogButton result = TaskDialog.ShowDialog(this, page);
    if (result == reconnectButton)
        Console.WriteLine("Reconnecting.");
    else
        Console.WriteLine("Not reconnecting.");
}

For VB:

' Set then maximum value for the ProgressBar
Dim maximum = remainingTenthSeconds * 2
page.ProgressBar.Maximum = maximum

' Create a WinForms timer that raises the Tick event every tenth second.
Using timer = New Windows.Forms.Timer() With {
    .Enabled = True,
    .Interval = 100
    }
    AddHandler timer.Tick,
        Sub(s, e)
            remainingTenthSeconds -= 1
            If remainingTenthSeconds > 0 Then
                ' Update the remaining time and progress bar.
                page.Text = String.Format(textFormat, (remainingTenthSeconds + 9) \ 10)
                page.ProgressBar.Value = maximum - remainingTenthSeconds * 2
            Else
                ' Stop the timer and click the "Reconnect" button - this will
                ' close the dialog.
                timer.Enabled = False
                reconnectButton.PerformClick()
            End If
        End Sub

    Dim result As TaskDialogButton = TaskDialog.ShowDialog(Me, page)
    If result = reconnectButton Then
        Console.WriteLine("Reconnecting.")
    Else
        Console.WriteLine("Not reconnecting.")
    End If
End Using

Hope this helps.
Guillermo


Issue metadata

  • Issue type: sample-update
@elGuille-info elGuille-info changed the title Improved the code for ShowAutoClosingTaskDialog in TaskDialogDemo Form1 (vb & c#) Improve the code for ShowAutoClosingTaskDialog in TaskDialogDemo Form1 (vb & c#) Apr 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant