using System; using System.ComponentModel; using System.Windows.Forms; using AeroWizard.Properties; namespace AeroWizard { /// /// Represents a single page in a . /// [Designer(typeof(Design.WizardPageDesigner)), DesignTimeVisible(true)] [DefaultProperty("Text"), DefaultEvent("Commit")] [ToolboxItem(false)] public partial class WizardPage : Control { private bool allowCancel = true, allowNext = true, allowBack = true; private bool showCancel = true, showNext = true, suppress; private bool isFinishPage; private string helpText; private LinkLabel helpLink; /// /// Initializes a new instance of the class. /// public WizardPage() { InitializeComponent(); Margin = Padding.Empty; base.Text = Properties.Resources.WizardHeader; } /// /// Occurs when the user has clicked the Next/Finish button but before the page is changed. /// [Category("Wizard"), Description("Occurs when the user has clicked the Next/Finish button but before the page is changed")] public event EventHandler Commit; /// /// Occurs when is set and the user has clicked the link at bottom of the content area. /// [Category("Wizard"), Description("Occurs when the user has clicked the help link")] public event EventHandler HelpClicked; /// /// Occurs when this page is entered. /// [Category("Wizard"), Description("Occurs when this page is entered")] public event EventHandler Initialize; /// /// Occurs when the user has clicked the Back button but before the page is changed. /// [Category("Wizard"), Description("Occurs when the user has clicked the Back button")] public event EventHandler Rollback; /// /// Gets or sets a value indicating whether to enable the Back button. /// /// true if Back button is enabled; otherwise, false. [DefaultValue(true), Category("Behavior"), Description("Indicates whether to enable the Back button")] public virtual bool AllowBack { get { return allowBack; } set { if (allowBack == value) return; allowBack = value; UpdateOwner(); } } /// /// Gets or sets a value indicating whether to enable the Cancel button. /// /// true if Cancel button is enabled; otherwise, false. [DefaultValue(true), Category("Behavior"), Description("Indicates whether to enable the Cancel button")] public virtual bool AllowCancel { get { return allowCancel; } set { if (allowCancel == value) return; allowCancel = value; UpdateOwner(); } } /// /// Gets or sets a value indicating whether to enable the Next/Finish button. /// /// true if Next/Finish button is enabled; otherwise, false. [DefaultValue(true), Category("Behavior"), Description("Indicates whether to enable the Next/Finish button")] public virtual bool AllowNext { get { return allowNext; } set { if (allowNext == value) return; allowNext = value; UpdateOwner(); } } /// /// Gets or sets the help text. When value is not null, a help link will be displayed at the bottom left of the content area. When clicked, the method will call the event. /// /// /// The help text to display. /// [DefaultValue(null), Category("Appearance"), Description("Help text to display on hyperlink at bottom left of content area.")] public string HelpText { get { return helpText; } set { if (helpLink == null) { helpLink = new LinkLabel() { AutoSize = true, Dock = DockStyle.Bottom, Text = Resources.WizardPageDefaultHelpText, Visible = false }; helpLink.LinkClicked += helpLink_LinkClicked; Controls.Add(helpLink); } helpText = value; if (helpText == null) { helpLink.Visible = false; } else { helpLink.Text = helpText; helpLink.Visible = true; } } } /// /// Gets or sets a value indicating whether this page is the last page in the sequence and should display the Finish text instead of the Next text on the Next/Finish button. /// /// true if this page is a finish page; otherwise, false. [DefaultValue(false), Category("Behavior"), Description("Indicates whether this page is the last page")] public virtual bool IsFinishPage { get { return isFinishPage; } set { if (isFinishPage == value) return; isFinishPage = value; UpdateOwner(); } } /// /// Gets or sets the next page that should be used when the user clicks the Next button or when the method is called. This is used to override the default behavior of going to the next page in the sequence defined within the collection. /// /// The wizard page to go to. [DefaultValue(null), Category("Behavior"), Description("Specify a page other than the next page in the Pages collection as the next page.")] public virtual WizardPage NextPage { get; set; } /// /// Gets the for this page. /// /// The for this page. [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public virtual WizardPageContainer Owner { get; internal set; } /// /// Gets or sets a value indicating whether to show the Cancel button. If both and are false, then the bottom command area will not be shown. /// /// true if Cancel button should be shown; otherwise, false. [DefaultValue(true), Category("Behavior"), Description("Indicates whether to show the Cancel button")] public virtual bool ShowCancel { get { return showCancel; } set { if (showCancel == value) return; showCancel = value; UpdateOwner(); } } /// /// Gets or sets a value indicating whether to show the Next/Finish button. If both and are false, then the bottom command area will not be shown. /// /// true if Next/Finish button should be shown; otherwise, false. [DefaultValue(true), Category("Behavior"), Description("Indicates whether to show the Next/Finish button")] public virtual bool ShowNext { get { return showNext; } set { if (showNext == value) return; showNext = value; UpdateOwner(); } } /// /// Gets or sets the height and width of the control. /// /// /// /// The that represents the height and width of the control in pixels. /// [Browsable(false)] public new System.Drawing.Size Size { get { return base.Size; } set { base.Size = value; } } /// /// Gets or sets a value indicating whether this is suppressed and not shown in the normal flow. /// /// /// true if suppressed; otherwise, false. /// [DefaultValue(false), Category("Behavior"), Description("Suppresses this page from viewing if selected as next.")] public virtual bool Suppress { get { return suppress; } set { if (suppress == value) return; suppress = value; UpdateOwner(); } } /// /// Gets the required creation parameters when the control handle is created. /// /// A that contains the required creation parameters when the handle to the control is created. protected override CreateParams CreateParams { get { var createParams = base.CreateParams; var parent = FindForm(); var parentRightToLeftLayout = parent != null && parent.RightToLeftLayout; if ((RightToLeft == RightToLeft.Yes) && parentRightToLeftLayout) { createParams.ExStyle |= 0x500000; // WS_EX_LAYOUTRTL | WS_EX_NOINHERITLAYOUT createParams.ExStyle &= ~0x7000; // WS_EX_RIGHT | WS_EX_RTLREADING | WS_EX_LEFTSCROLLBAR } return createParams; } } /// /// Returns a that represents this wizard page. /// /// /// A that represents this wizard page. /// public override string ToString() => $"{Name} (\"{Text}\")"; internal bool CommitPage() => OnCommit(); internal void InitializePage(WizardPage prevPage) { OnInitialize(prevPage); } internal bool RollbackPage() => OnRollback(); /// /// Raises the event. /// /// true if handler does not set the to true; otherwise, false. protected virtual bool OnCommit() { var e = new WizardPageConfirmEventArgs(this); Commit?.Invoke(this,e); return !e.Cancel; } /// /// Raises the event. /// /// An that contains the event data. protected override void OnGotFocus(EventArgs e) { base.OnGotFocus(e); var firstChild = GetNextControl(this, true); firstChild?.Focus(); } /// /// Raises the event. /// protected virtual void OnHelpClicked() { HelpClicked?.Invoke(this, EventArgs.Empty); } /// /// Raises the event. /// /// The page that was previously selected. protected virtual void OnInitialize(WizardPage prevPage) { Initialize?.Invoke(this, new WizardPageInitEventArgs(this, prevPage)); } /// /// Raises the event. /// /// true if handler does not set the to true; otherwise, false. protected virtual bool OnRollback() { var e = new WizardPageConfirmEventArgs(this); Rollback?.Invoke(this, e); return !e.Cancel; } private void helpLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { OnHelpClicked(); } private void UpdateOwner() { if (Owner != null && this == Owner.SelectedPage) Owner.UpdateUIDependencies(); } } /// /// Arguments supplied to the events. /// public class WizardPageConfirmEventArgs : EventArgs { internal WizardPageConfirmEventArgs(WizardPage page) { Cancel = false; Page = page; } /// /// Gets or sets a value indicating whether this action is to be canceled or allowed. /// /// true if cancel; otherwise, false to allow. Default is false. [DefaultValue(false)] public bool Cancel { get; set; } /// /// Gets the that has raised the event. /// /// The wizard page. public WizardPage Page { get; } } /// /// Arguments supplied to the event. /// public class WizardPageInitEventArgs : WizardPageConfirmEventArgs { internal WizardPageInitEventArgs(WizardPage page, WizardPage prevPage) : base(page) { PreviousPage = prevPage; } /// /// Gets the that was previously selected when the event was raised. /// /// The previous wizard page. public WizardPage PreviousPage { get; } } }