Changeset 17062 for stable/HeuristicLab.MainForm.WindowsForms/3.3/Controls
- Timestamp:
- 07/04/19 14:45:47 (5 years ago)
- Location:
- stable
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
stable
- Property svn:mergeinfo changed
-
stable/HeuristicLab.MainForm.WindowsForms/3.3/Controls/ProgressView.cs
r15584 r17062 25 25 namespace HeuristicLab.MainForm.WindowsForms { 26 26 internal sealed partial class ProgressView : UserControl { 27 private const int defaultControlHeight = 88;28 private const int collapsedControlHeight = 55;29 30 27 private readonly Control control; 31 28 public Control Control { … … 40 37 public ProgressView(Control control, IProgress content) 41 38 : base() { 42 if (control == null) throw new ArgumentNullException("control" , "The control is null.");43 if (content == null) throw new ArgumentNullException("content" , "The passed progress is null.");39 if (control == null) throw new ArgumentNullException("control"); 40 if (content == null) throw new ArgumentNullException("content"); 44 41 InitializeComponent(); 45 42 46 43 this.control = control; 47 44 this.content = content; 48 if (content.ProgressState == ProgressState.Started) 45 46 if (content.ProgressState != ProgressState.Finished) 49 47 ShowProgress(); 50 48 RegisterContentEvents(); 51 49 } 52 50 53 /// <summary>54 /// Clean up any resources being used.55 /// </summary>56 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>57 51 protected override void Dispose(bool disposing) { 58 52 DeregisterContentEvents(); … … 66 60 67 61 private void RegisterContentEvents() { 68 content.StatusChanged += new EventHandler(progress_StatusChanged); 69 content.ProgressValueChanged += new EventHandler(progress_ProgressValueChanged); 70 content.ProgressStateChanged += new EventHandler(Content_ProgressStateChanged); 71 content.CanBeCanceledChanged += new EventHandler(Content_CanBeCanceledChanged); 62 Content.ProgressStateChanged += new EventHandler(Content_ProgressStateChanged); 63 Content.MessageChanged += new EventHandler(Content_MessageChanged); 64 Content.ProgressBarModeChanged += new EventHandler(Content_ProgressBarModeChanged); 65 Content.ProgressValueChanged += new EventHandler(Content_ProgressValueChanged); 66 Content.CanBeStoppedChanged += new EventHandler(Content_CanBeStoppedChanged); 67 Content.CanBeCanceledChanged += new EventHandler(Content_CanBeCanceledChanged); 72 68 } 73 69 private void DeregisterContentEvents() { 74 content.StatusChanged -= new EventHandler(progress_StatusChanged); 75 content.ProgressValueChanged -= new EventHandler(progress_ProgressValueChanged); 76 content.ProgressStateChanged -= new EventHandler(Content_ProgressStateChanged); 77 content.CanBeCanceledChanged -= new EventHandler(Content_CanBeCanceledChanged); 70 Content.ProgressStateChanged -= new EventHandler(Content_ProgressStateChanged); 71 Content.MessageChanged -= new EventHandler(Content_MessageChanged); 72 Content.ProgressBarModeChanged -= new EventHandler(Content_ProgressBarModeChanged); 73 Content.ProgressValueChanged -= new EventHandler(Content_ProgressValueChanged); 74 Content.CanBeStoppedChanged -= new EventHandler(Content_CanBeStoppedChanged); 75 Content.CanBeCanceledChanged -= new EventHandler(Content_CanBeCanceledChanged); 76 } 77 78 private void Content_ProgressStateChanged(object sender, EventArgs e) { 79 UpdateProgressState(); 80 UpdateButtonsState(); 81 } 82 83 private void Content_MessageChanged(object sender, EventArgs e) { 84 UpdateProgressMessage(); 85 } 86 87 private void Content_ProgressBarModeChanged(object sender, EventArgs e) { 88 UpdateProgressValue(); 89 } 90 private void Content_ProgressValueChanged(object sender, EventArgs e) { 91 UpdateProgressValue(); 92 } 93 94 private void Content_CanBeStoppedChanged(object sender, EventArgs e) { 95 UpdateButtonsState(); 96 } 97 private void Content_CanBeCanceledChanged(object sender, EventArgs e) { 98 UpdateButtonsState(); 78 99 } 79 100 … … 83 104 return; 84 105 } 85 i nt height = Content.CanBeCanceled ? Height : collapsedControlHeight;106 if (Parent != null) return; 86 107 87 108 Left = (Control.ClientRectangle.Width / 2) - (Width / 2); 88 Top = (Control.ClientRectangle.Height / 2) - ( height / 2);109 Top = (Control.ClientRectangle.Height / 2) - (Height / 2); 89 110 Anchor = AnchorStyles.None; 90 111 91 control.Enabled = false; 112 UpdateProgressMessage(); 113 UpdateProgressValue(); 114 UpdateButtonsState(); 115 116 Control.Enabled = false; 92 117 Parent = Control.Parent; 93 118 BringToFront(); 94 95 UpdateProgressValue();96 UpdateProgressStatus();97 UpdateCancelButton();98 119 Visible = true; 99 120 } 100 121 101 122 private void HideProgress() { 102 if (InvokeRequired) Invoke((Action)HideProgress); 103 else { 104 control.Enabled = true; 105 Parent = null; 106 Visible = false; 123 if (Control.InvokeRequired) { 124 Control.Invoke((Action)HideProgress); 125 return; 126 } 127 if (Parent == null) return; 128 129 Visible = false; 130 Control.Enabled = true; 131 Parent = null; 132 } 133 134 private void UpdateProgressState() { 135 if (Control.InvokeRequired) { 136 Control.Invoke((Action)UpdateProgressState); 137 return; 138 } 139 140 if (Content.ProgressState != ProgressState.Finished) 141 ShowProgress(); 142 else 143 HideProgress(); 144 } 145 146 private void UpdateProgressMessage() { 147 if (Control.InvokeRequired) { 148 Control.Invoke((Action)UpdateProgressMessage); 149 return; 150 } 151 152 messageLabel.Text = content.Message; 153 } 154 155 private void UpdateProgressValue() { 156 if (InvokeRequired) { 157 Invoke((Action)UpdateProgressValue); 158 return; 159 } 160 161 switch (Content.ProgressMode) { 162 case ProgressMode.Determinate: 163 progressBar.Style = ProgressBarStyle.Continuous; 164 progressBar.Value = (int)Math.Round(progressBar.Minimum + content.ProgressValue * (progressBar.Maximum - progressBar.Minimum)); 165 break; 166 case ProgressMode.Indeterminate: 167 progressBar.Style = ProgressBarStyle.Marquee; 168 progressBar.Value = 0; 169 break; 170 default: 171 throw new NotImplementedException($"Invalid Progress Mode: {content.ProgressMode}"); 107 172 } 108 173 } 109 174 110 private void progress_StatusChanged(object sender, EventArgs e) { 111 UpdateProgressStatus(); 175 private void UpdateButtonsState() { 176 if (Control.InvokeRequired) { 177 Control.Invoke((Action)UpdateButtonsState); 178 return; 179 } 180 181 stopButton.Visible = Content.CanBeStopped; 182 stopButton.Enabled = Content.CanBeStopped && content.ProgressState == ProgressState.Started; 183 184 cancelButton.Visible = Content.CanBeCanceled; 185 cancelButton.Enabled = Content.CanBeCanceled && content.ProgressState == ProgressState.Started; 112 186 } 113 187 114 private void progress_ProgressValueChanged(object sender, EventArgs e) {115 UpdateProgressValue();188 private void stopButton_Click(object sender, EventArgs e) { 189 Content.Stop(); 116 190 } 117 118 private void Content_ProgressStateChanged(object sender, EventArgs e) {119 switch (content.ProgressState) {120 case ProgressState.Finished: HideProgress(); break;121 case ProgressState.Canceled: HideProgress(); break;122 case ProgressState.Started: ShowProgress(); break;123 default: throw new NotSupportedException("The progress state " + content.ProgressState + " is not supported by the ProgressView.");124 }125 }126 127 private void Content_CanBeCanceledChanged(object sender, EventArgs e) {128 UpdateCancelButton();129 }130 131 private void UpdateCancelButton() {132 cancelButton.Visible = content != null && content.CanBeCanceled;133 cancelButton.Enabled = content != null && content.CanBeCanceled;134 135 if (content != null && content.CanBeCanceled) {136 Height = defaultControlHeight;137 } else if (content != null && !content.CanBeCanceled) {138 Height = collapsedControlHeight;139 }140 }141 142 private void UpdateProgressValue() {143 // prevent problems with object disposal and invoke as suggested by http://stackoverflow.com/a/18647091144 if (!IsHandleCreated) return;145 if (InvokeRequired) {146 try {147 Invoke((Action)UpdateProgressValue);148 }149 catch (InvalidOperationException) {150 // swallow ObjectDisposedException151 // which might occur if the invoke call is executed after or while the control is disposing152 }153 } else {154 if (content != null) {155 double progressValue = content.ProgressValue;156 if (progressValue <= 0.0 || progressValue > 1.0) {157 progressBar.Style = ProgressBarStyle.Marquee;158 } else {159 progressBar.Style = ProgressBarStyle.Blocks;160 progressBar.Value =161 (int)Math.Round(progressBar.Minimum + progressValue * (progressBar.Maximum - progressBar.Minimum));162 }163 }164 }165 }166 167 private void UpdateProgressStatus() {168 if (InvokeRequired) Invoke((Action)UpdateProgressStatus);169 else if (content != null)170 statusLabel.Text = content.Status;171 }172 173 191 private void cancelButton_Click(object sender, EventArgs e) { 174 content.Cancel();192 Content.Cancel(); 175 193 } 176 194 } -
stable/HeuristicLab.MainForm.WindowsForms/3.3/Controls/ProgressView.designer.cs
r15584 r17062 27 27 private System.ComponentModel.IContainer components = null; 28 28 29 29 30 30 #region Component Designer generated code 31 31 … … 36 36 private void InitializeComponent() { 37 37 this.progressBar = new System.Windows.Forms.ProgressBar(); 38 this.statusLabel = new System.Windows.Forms.Label(); 38 this.messageLabel = new System.Windows.Forms.Label(); 39 this.borderPanel = new System.Windows.Forms.Panel(); 40 this.panel = new System.Windows.Forms.Panel(); 41 this.stopButton = new System.Windows.Forms.Button(); 39 42 this.cancelButton = new System.Windows.Forms.Button(); 40 this. panel = new System.Windows.Forms.Panel();43 this.borderPanel.SuspendLayout(); 41 44 this.panel.SuspendLayout(); 42 45 this.SuspendLayout(); … … 48 51 this.progressBar.Location = new System.Drawing.Point(3, 3); 49 52 this.progressBar.Name = "progressBar"; 50 this.progressBar.Size = new System.Drawing.Size(3 52, 23);53 this.progressBar.Size = new System.Drawing.Size(366, 23); 51 54 this.progressBar.Style = System.Windows.Forms.ProgressBarStyle.Marquee; 52 55 this.progressBar.TabIndex = 0; 53 56 // 54 // statusLabel57 // messageLabel 55 58 // 56 this.statusLabel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 59 this.messageLabel.Dock = System.Windows.Forms.DockStyle.Fill; 60 this.messageLabel.Location = new System.Drawing.Point(0, 0); 61 this.messageLabel.Name = "messageLabel"; 62 this.messageLabel.Padding = new System.Windows.Forms.Padding(0, 0, 3, 0); 63 this.messageLabel.Size = new System.Drawing.Size(217, 23); 64 this.messageLabel.TabIndex = 1; 65 this.messageLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; 66 // 67 // borderPanel 68 // 69 this.borderPanel.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 70 | System.Windows.Forms.AnchorStyles.Left) 57 71 | System.Windows.Forms.AnchorStyles.Right))); 58 this.statusLabel.Location = new System.Drawing.Point(3, 33); 59 this.statusLabel.Name = "statusLabel"; 60 this.statusLabel.Size = new System.Drawing.Size(352, 17); 61 this.statusLabel.TabIndex = 1; 72 this.borderPanel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; 73 this.borderPanel.Controls.Add(this.panel); 74 this.borderPanel.Controls.Add(this.progressBar); 75 this.borderPanel.Location = new System.Drawing.Point(0, 0); 76 this.borderPanel.Name = "borderPanel"; 77 this.borderPanel.Size = new System.Drawing.Size(374, 62); 78 this.borderPanel.TabIndex = 3; 79 // 80 // panel 81 // 82 this.panel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 83 | System.Windows.Forms.AnchorStyles.Right))); 84 this.panel.Controls.Add(this.messageLabel); 85 this.panel.Controls.Add(this.stopButton); 86 this.panel.Controls.Add(this.cancelButton); 87 this.panel.Location = new System.Drawing.Point(3, 32); 88 this.panel.Name = "panel"; 89 this.panel.Size = new System.Drawing.Size(367, 23); 90 this.panel.TabIndex = 4; 91 // 92 // stopButton 93 // 94 this.stopButton.Dock = System.Windows.Forms.DockStyle.Right; 95 this.stopButton.Location = new System.Drawing.Point(217, 0); 96 this.stopButton.Name = "stopButton"; 97 this.stopButton.Size = new System.Drawing.Size(75, 23); 98 this.stopButton.TabIndex = 3; 99 this.stopButton.Text = "Stop"; 100 this.stopButton.UseVisualStyleBackColor = true; 101 this.stopButton.Click += new System.EventHandler(this.stopButton_Click); 62 102 // 63 103 // cancelButton 64 104 // 65 this.cancelButton. Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));66 this.cancelButton.Location = new System.Drawing.Point(2 80, 53);105 this.cancelButton.Dock = System.Windows.Forms.DockStyle.Right; 106 this.cancelButton.Location = new System.Drawing.Point(292, 0); 67 107 this.cancelButton.Name = "cancelButton"; 68 108 this.cancelButton.Size = new System.Drawing.Size(75, 23); … … 72 112 this.cancelButton.Click += new System.EventHandler(this.cancelButton_Click); 73 113 // 74 // panel75 //76 this.panel.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)77 | System.Windows.Forms.AnchorStyles.Left)78 | System.Windows.Forms.AnchorStyles.Right)));79 this.panel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;80 this.panel.Controls.Add(this.progressBar);81 this.panel.Controls.Add(this.cancelButton);82 this.panel.Controls.Add(this.statusLabel);83 this.panel.Location = new System.Drawing.Point(0, 0);84 this.panel.Name = "panel";85 this.panel.Size = new System.Drawing.Size(360, 88);86 this.panel.TabIndex = 3;87 //88 114 // ProgressView 89 115 // 90 116 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit; 91 this.Controls.Add(this. panel);117 this.Controls.Add(this.borderPanel); 92 118 this.Name = "ProgressView"; 93 this.Size = new System.Drawing.Size(360, 88); 119 this.Size = new System.Drawing.Size(374, 62); 120 this.borderPanel.ResumeLayout(false); 94 121 this.panel.ResumeLayout(false); 95 122 this.ResumeLayout(false); … … 100 127 101 128 private System.Windows.Forms.ProgressBar progressBar; 102 private System.Windows.Forms.Label statusLabel; 129 private System.Windows.Forms.Label messageLabel; 130 private System.Windows.Forms.Panel borderPanel; 131 private System.Windows.Forms.Button stopButton; 132 private System.Windows.Forms.Panel panel; 103 133 private System.Windows.Forms.Button cancelButton; 104 private System.Windows.Forms.Panel panel;105 134 } 106 135 }
Note: See TracChangeset
for help on using the changeset viewer.