- Timestamp:
- 07/04/19 14:45:47 (5 years ago)
- Location:
- stable
- Files:
-
- 2 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 }
Note: See TracChangeset
for help on using the changeset viewer.