private static void ShowAboutDialog(TaskDialogPage origpage) { var newPage = new TaskDialogPage() { Heading = "USB Function Mode Switcher", Text = "Version " + Assembly.GetEntryAssembly().GetCustomAttribute <AssemblyFileVersionAttribute>().Version + "\n" + Assembly.GetEntryAssembly().GetCustomAttribute <AssemblyCopyrightAttribute>().Copyright + "\nReleased under the MIT License", Caption = "About", Icon = TaskDialogIcon.Information, AllowCancel = true, SizeToContent = true }; var srcbutton = new TaskDialogCommandLinkButton("Source Code", allowCloseDialog: false); var closebutton = new TaskDialogCommandLinkButton("Close", allowCloseDialog: false); newPage.Buttons.Add(srcbutton); newPage.Buttons.Add(closebutton); srcbutton.Click += (object sender2, EventArgs args2) => { Process.Start("https://github.com/WOA-Project/USBFunctionModeSwitcher"); }; closebutton.Click += (object sender2, EventArgs args2) => { newPage.Navigate(origpage); }; origpage.Navigate(newPage); }
private static void ShowDisclaimerDialog(TaskDialogPage origpage, Action action) { var newPage = new TaskDialogPage() { Heading = "USB Function Mode Switcher", Text = "Switching to this mode will enable power output from the USB Type C port. This may harm your device if you plug in a charging cable or a continuum dock. In this mode NEVER plug in any charging cable, wall charger, PC USB Cable (connected to a PC) or any externally powered USB hub! We cannot be taken responsible for any damage caused by this, you have been warned!", Caption = "Do you really want to do this?", Icon = TaskDialogIcon.Warning, AllowCancel = true, SizeToContent = true }; var nobutton = new TaskDialogCommandLinkButton("No", allowCloseDialog: false); var yesbutton = new TaskDialogCommandLinkButton("Yes I understand all the risks"); newPage.Buttons.Add(nobutton); newPage.Buttons.Add(yesbutton); yesbutton.Click += (object sender2, EventArgs args2) => { action(); }; nobutton.Click += (object sender2, EventArgs args2) => { newPage.Navigate(origpage); }; origpage.Navigate(newPage); }
public static AfterSetupAction StartGuidedSetupDialog() { var quit = new TaskDialogButton("Quit HolzShots"); var downloadButton = new TaskDialogCommandLinkButton() { Text = "Download automatically", AllowCloseDialog = false, }; var manualDownload = new TaskDialogCommandLinkButton() { Text = "Set up manually", AllowCloseDialog = false, }; var doNothing = new TaskDialogCommandLinkButton() { Text = "Cancel", AllowCloseDialog = false, }; var initialPage = CreateInitialPage(downloadButton, manualDownload); var manualSetupPage = CreateManualSetupPage(quit); manualDownload.Click += (s, e) => initialPage.Navigate(manualSetupPage); var noActionPage = CreateNoActionPage(); doNothing.Click += (s, e) => initialPage.Navigate(noActionPage); var downloadPage = CreatetDownloadPage(); downloadButton.Click += (s, e) => initialPage.Navigate(downloadPage); var answer = TaskDialog.ShowDialog(initialPage, TaskDialogStartupLocation.CenterScreen); if (answer == TaskDialogButton.OK) { // The user clicked "Cancel" and then OK (or an error ocurred) return(AfterSetupAction.AbortCurrentAction); } if (answer == TaskDialogButton.Cancel) { // The user closed the dialog via "X" return(AfterSetupAction.AbortCurrentAction); } if (answer == quit) { return(AfterSetupAction.QuitApplication); } return(AfterSetupAction.Coninue); }
static void Main() { (bool supported, string reason) = IsSupported(); if (supported) { ShowTaskDialog(); } else { var dialogPage = new TaskDialogPage() { Heading = "USB Function Mode Switcher", Caption = "Unsupported device", Text = reason, Footnote = { Text = "Please verify that you run a supported device with the latest drivers available for it.", Icon = TaskDialogIcon.Information, }, AllowCancel = true, SizeToContent = true, Icon = TaskDialogIcon.Error }; var closemainbutton = new TaskDialogCommandLinkButton("Close"); var aboutbutton = new TaskDialogCommandLinkButton("About", "About USB Function Mode Switcher", allowCloseDialog: false); dialogPage.Buttons.Add(closemainbutton); dialogPage.Buttons.Add(aboutbutton); aboutbutton.Click += (object sender, EventArgs args) => { ShowAboutDialog(dialogPage); }; TaskDialogButton result = TaskDialog.ShowDialog(dialogPage); } }
private static void ShowTaskDialog() { var dialogPage = new TaskDialogPage() { Heading = "USB Function Mode Switcher", Caption = "Select a USB function mode to switch to", Text = "Below are the available modes your phone supports switching to.", Footnote = { Text = "Switching modes will require a reboot of the device.", Icon = TaskDialogIcon.Warning, }, AllowCancel = true, SizeToContent = true }; try { var handler = new USBRoleHandler(); foreach (var role in handler.USBRoles) { var rolebutton = new TaskDialogCommandLinkButton(role.DisplayName, role.Description); if (role.IsHost && role.HostRole.EnableVbus) { rolebutton.AllowCloseDialog = false; } dialogPage.Buttons.Add(rolebutton); if (role == handler.CurrentUSBRole) { rolebutton.Enabled = false; } rolebutton.Click += (object sender, EventArgs args) => { if (role.IsHost && role.HostRole.EnableVbus) { ShowDisclaimerDialog(dialogPage, () => { handler.CurrentUSBRole = role; RebootDevice(); }); } else { handler.CurrentUSBRole = role; RebootDevice(); } }; } if (USBRoleHandler.IsUSBCv2()) { var polaritybutton = new TaskDialogCommandLinkButton("Polarity", "Change the polarity of the USB C port"); polaritybutton.AllowCloseDialog = false; dialogPage.Buttons.Add(polaritybutton); polaritybutton.Click += (object sender, EventArgs args) => { ShowPolarityDialog(dialogPage); }; } var aboutbutton = new TaskDialogCommandLinkButton("About", "About USB Function Mode Switcher", allowCloseDialog: false); dialogPage.Buttons.Add(aboutbutton); aboutbutton.Click += (object sender, EventArgs args) => { ShowAboutDialog(dialogPage); }; TaskDialogButton result = TaskDialog.ShowDialog(dialogPage); } catch (Exception ex) { dialogPage = new TaskDialogPage() { Heading = "USB Function Mode Switcher", Caption = "Something happened", Text = ex.ToString(), AllowCancel = true, SizeToContent = true, Icon = TaskDialogIcon.Error }; var closemainbutton = new TaskDialogCommandLinkButton("Close"); var aboutbutton = new TaskDialogCommandLinkButton("About", "About USB Function Mode Switcher", allowCloseDialog: false); dialogPage.Buttons.Add(closemainbutton); dialogPage.Buttons.Add(aboutbutton); aboutbutton.Click += (object sender, EventArgs args) => { ShowAboutDialog(dialogPage); }; TaskDialogButton result = TaskDialog.ShowDialog(dialogPage); } }
private static void ShowPolarityDialog(TaskDialogPage origpage) { var newPage = new TaskDialogPage() { Heading = "USB Function Mode Switcher", Text = "You can change the polarity of the USB C port. This effectively allows you to use the cable in another direction.", Caption = "Polarity", AllowCancel = true, SizeToContent = true }; var PolarityFirst = new TaskDialogCommandLinkButton("Polarity 1"); var PolaritySecond = new TaskDialogCommandLinkButton("Polarity 2"); var closebutton = new TaskDialogCommandLinkButton("Close", allowCloseDialog: false); newPage.Buttons.Add(PolarityFirst); newPage.Buttons.Add(PolaritySecond); newPage.Buttons.Add(closebutton); int pol = 0; using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SYSTEM\usbc", true)) { if (key != null && key.GetValueNames().Any(x => x.ToLower() == "polarity")) { pol = (int)key.GetValue("Polarity"); } } if (pol == 0) { PolarityFirst.Enabled = false; } else { PolaritySecond.Enabled = false; } PolarityFirst.Click += (object sender2, EventArgs args2) => { using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SYSTEM\usbc", true)) { key.SetValue("Polarity", 0, RegistryValueKind.DWord); } RebootDevice(); }; PolaritySecond.Click += (object sender2, EventArgs args2) => { using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SYSTEM\usbc", true)) { key.SetValue("Polarity", 1, RegistryValueKind.DWord); } RebootDevice(); }; closebutton.Click += (object sender2, EventArgs args2) => { newPage.Navigate(origpage); }; origpage.Navigate(newPage); }
private void ShowEventsDemoTaskDialog() { var page1 = new TaskDialogPage() { Caption = Text, Heading = "Event Demo", Text = "Event Demo...", }; page1.Created += (s, e) => Console.WriteLine("Page1 Created"); page1.Destroyed += (s, e) => Console.WriteLine("Page1 Destroyed"); page1.HelpRequest += (s, e) => Console.WriteLine("Page1 HelpRequest"); page1.Expander = new TaskDialogExpander("Expander") { Position = TaskDialogExpanderPosition.AfterFootnote }; page1.Expander.ExpandedChanged += (s, e) => Console.WriteLine("Expander ExpandedChanged: " + page1.Expander.Expanded); var buttonOK = TaskDialogButton.OK; var buttonHelp = TaskDialogButton.Help; var buttonCancelClose = new TaskDialogCommandLinkButton("C&ancel Close", allowCloseDialog: false); var buttonShowInnerDialog = new TaskDialogCommandLinkButton("&Show (modeless) Inner Dialog", "(and don't cancel the Close)"); var buttonNavigate = new TaskDialogCommandLinkButton("&Navigate", allowCloseDialog: false); page1.Buttons.Add(buttonOK); page1.Buttons.Add(buttonHelp); page1.Buttons.Add(buttonCancelClose); page1.Buttons.Add(buttonShowInnerDialog); page1.Buttons.Add(buttonNavigate); buttonOK.Click += (s, e) => Console.WriteLine($"Button '{s}' Click"); buttonHelp.Click += (s, e) => Console.WriteLine($"Button '{s}' Click"); buttonCancelClose.Click += (s, e) => { Console.WriteLine($"Button '{s}' Click"); }; buttonShowInnerDialog.Click += (s, e) => { Console.WriteLine($"Button '{s}' Click"); TaskDialog.ShowDialog(new TaskDialogPage() { Text = "Inner Dialog" }); Console.WriteLine($"(returns) Button '{s}' Click"); }; buttonNavigate.Click += (s, e) => { Console.WriteLine($"Button '{s}' Click"); // Navigate to a new page. var page2 = new TaskDialogPage() { Heading = "AfterNavigation.", Buttons = { TaskDialogButton.Close } }; page2.Created += (s, e) => Console.WriteLine("Page2 Created"); page2.Destroyed += (s, e) => Console.WriteLine("Page2 Destroyed"); page1.Navigate(page2); }; page1.Verification = new TaskDialogVerificationCheckBox("&CheckBox1"); page1.Verification.CheckedChanged += (s, e) => Console.WriteLine("CheckBox CheckedChanged: " + page1.Verification.Checked); var radioButton1 = page1.RadioButtons.Add("Radi&oButton1"); var radioButton2 = page1.RadioButtons.Add("RadioB&utton2"); radioButton1.CheckedChanged += (s, e) => Console.WriteLine("RadioButton1 CheckedChanged: " + radioButton1.Checked); radioButton2.CheckedChanged += (s, e) => Console.WriteLine("RadioButton2 CheckedChanged: " + radioButton2.Checked); var dialogResult = TaskDialog.ShowDialog(page1); Console.WriteLine("---> Dialog Result: " + dialogResult); }
private void ShowMultiPageTaskDialog() { // Disable the "Yes" button and only enable it when the check box is checked. // Also, don't close the dialog when this button is clicked. var initialButtonYes = TaskDialogButton.Yes; initialButtonYes.Enabled = false; initialButtonYes.AllowCloseDialog = false; var initialPage = new TaskDialogPage() { Caption = "My Application", Heading = "Clean up database?", Text = "Do you really want to do a clean-up?\nThis action is irreversible!", Icon = TaskDialogIcon.ShieldWarningYellowBar, AllowCancel = true, // A modeless dialog can be minimizable. AllowMinimize = true, Verification = new TaskDialogVerificationCheckBox() { Text = "I know what I'm doing" }, Buttons = { TaskDialogButton.No, initialButtonYes }, DefaultButton = TaskDialogButton.No }; // For the "In Progress" page, don't allow the dialog to close, by adding // a disabled button (if no button was specified, the task dialog would // get an (enabled) 'OK' button). var inProgressCloseButton = TaskDialogButton.Close; inProgressCloseButton.Enabled = false; var inProgressPage = new TaskDialogPage() { Caption = "My Application", Heading = "Operation in progress...", Text = "Please wait while the operation is in progress.", Icon = TaskDialogIcon.Information, AllowMinimize = true, ProgressBar = new TaskDialogProgressBar() { State = TaskDialogProgressBarState.Marquee }, Expander = new TaskDialogExpander() { Text = "Initializing...", Position = TaskDialogExpanderPosition.AfterFootnote }, Buttons = { inProgressCloseButton } }; // Add an invisible Cancel button where we will intercept the Click event // to prevent the dialog from closing (when the User clicks the "X" button // in the title bar or presses ESC or Alt+F4). var invisibleCancelButton = TaskDialogButton.Cancel; invisibleCancelButton.Visible = false; invisibleCancelButton.AllowCloseDialog = false; inProgressPage.Buttons.Add(invisibleCancelButton); var finishedPage = new TaskDialogPage() { Caption = "My Application", Heading = "Success!", Text = "The operation finished.", Icon = TaskDialogIcon.ShieldSuccessGreenBar, AllowMinimize = true, Buttons = { TaskDialogButton.Close } }; TaskDialogButton showResultsButton = new TaskDialogCommandLinkButton("Show &Results"); finishedPage.Buttons.Add(showResultsButton); // Enable the "Yes" button only when the checkbox is checked. TaskDialogVerificationCheckBox checkBox = initialPage.Verification; checkBox.CheckedChanged += (sender, e) => { initialButtonYes.Enabled = checkBox.Checked; }; // When the user clicks "Yes", navigate to the second page. initialButtonYes.Click += (sender, e) => { // Navigate to the "In Progress" page that displays the // current progress of the background work. initialPage.Navigate(inProgressPage); // NOTE: When you implement a "In Progress" page that represents // background work that is done e.g. by a separate thread/task, // which eventually calls Control.Invoke()/BeginInvoke() when // its work is finished in order to navigate or update the dialog, // then DO NOT start that work here already (directly after // setting the Page property). Instead, start the work in the // TaskDialogPage.Created event of the new page. // // See comments in the code sample in https://github.com/dotnet/winforms/issues/146 // for more information. }; // Simulate work by starting an async operation from which we are updating the // progress bar and the expander with the current status. inProgressPage.Created += async(s, e) => { // Run the background operation and iterate over the streamed values to update // the progress. Because we call the async method from the GUI thread, // it will use this thread's synchronization context to run the continuations, // so we don't need to use Control.[Begin]Invoke() to schedule the callbacks. var progressBar = inProgressPage.ProgressBar; await foreach (int progressValue in StreamBackgroundOperationProgressAsync()) { // When we display the first progress, switch the marquee progress bar // to a regular one. if (progressBar.State == TaskDialogProgressBarState.Marquee) { progressBar.State = TaskDialogProgressBarState.Normal; } progressBar.Value = progressValue; inProgressPage.Expander.Text = $"Progress: {progressValue} %"; } // Work is finished, so navigate to the third page. inProgressPage.Navigate(finishedPage); }; // Show the dialog (modeless). TaskDialogButton result = TaskDialog.ShowDialog(initialPage); if (result == showResultsButton) { Console.WriteLine("Showing Results!"); }