Free cookie consent management tool by TermsFeed Policy Generator

Changeset 8111


Ignore:
Timestamp:
06/26/12 00:27:18 (12 years ago)
Author:
abeham
Message:

#1762: Improved code a little

Location:
trunk/sources/HeuristicLab.MainForm.WindowsForms/3.3/Views
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.MainForm.WindowsForms/3.3/Views/ProgressView.cs

    r7583 r8111  
    2626  public partial class ProgressView : HeuristicLab.MainForm.WindowsForms.View {
    2727    private ContentView parentView;
     28
    2829    private IProgress progress;
    2930    public IProgress Progress {
    30       get {
    31         return this.progress;
     31      get { return progress; }
     32      set {
     33        if (progress == value) return;
     34        DeregisterProgressEvents();
     35        progress = value;
     36        RegisterProgressEvents();
     37        OnProgressChanged();
    3238      }
     39    }
     40
     41    private bool cancelEnabled;
     42    public bool CancelEnabled {
     43      get { return cancelEnabled; }
    3344      set {
    34         if (this.progress != value) {
    35           if (this.progress != null) {
    36             DeregisterProgressEvents();
    37           }
    38           this.progress = value;
    39           RegisterProgressEvents();
    40           OnProgressChanged();
     45        cancelEnabled = value;
     46        SetCancelButtonVisibility();
     47      }
     48    }
     49
     50    /// <param name="parentView">This is the view which will be locked while progress is made.</param>
     51    public ProgressView(ContentView parentView, IProgress progress) {
     52      InitializeComponent();
     53      Progress = progress;
     54      CancelEnabled = false;
     55
     56      if (parentView != null) {
     57        this.parentView = parentView;
     58        this.Left = (parentView.ClientRectangle.Width / 2) - (this.Width / 2);
     59        this.Top = (parentView.ClientRectangle.Height / 2) - (this.Height / 2);
     60        this.Anchor = AnchorStyles.Left | AnchorStyles.Top;
     61
     62        LockBackground();
     63
     64        parentView.Controls.Add(this);
     65        BringToFront();
     66      }
     67    }
     68
     69    private void RegisterProgressEvents() {
     70      if (progress == null) return;
     71      progress.Finished += new EventHandler(progress_Finished);
     72      progress.StatusChanged += new EventHandler(progress_StatusChanged);
     73      progress.ProgressValueChanged += new EventHandler(progress_ProgressValueChanged);
     74    }
     75
     76    private void DeregisterProgressEvents() {
     77      if (progress == null) return;
     78      progress.Finished -= new EventHandler(progress_Finished);
     79      progress.StatusChanged -= new EventHandler(progress_StatusChanged);
     80      progress.ProgressValueChanged -= new EventHandler(progress_ProgressValueChanged);
     81    }
     82
     83    private void progress_Finished(object sender, EventArgs e) {
     84      Finish();
     85    }
     86
     87    private void progress_StatusChanged(object sender, EventArgs e) {
     88      UpdateProgressStatus();
     89    }
     90
     91    private void progress_ProgressValueChanged(object sender, EventArgs e) {
     92      UpdateProgressValue();
     93    }
     94
     95    private void LockBackground() {
     96      if (InvokeRequired) {
     97        Invoke((Action)LockBackground);
     98      } else {
     99        parentView.Locked = true;
     100        parentView.ReadOnly = true;
     101        foreach (Control c in this.parentView.Controls)
     102          c.Enabled = false;
     103        Enabled = true;
     104        ReadOnly = false;
     105      }
     106    }
     107
     108    private void UpdateProgressValue() {
     109      if (InvokeRequired) Invoke((Action)UpdateProgressValue);
     110      else {
     111        double progressValue = progress.ProgressValue;
     112        if (progressValue < progressBar.Minimum || progressValue > progressBar.Maximum) {
     113          progressBar.Style = ProgressBarStyle.Marquee;
     114          progressBar.Value = progressBar.Minimum;
     115        } else {
     116          progressBar.Style = ProgressBarStyle.Blocks;
     117          progressBar.Value = (int)Math.Round(progressBar.Minimum + progressValue * (progressBar.Maximum - progressBar.Minimum));
    41118        }
    42119      }
    43120    }
    44121
    45     public bool CancelEnabled {
    46       get {
    47         return cancelButton.Visible;
    48       }
    49       set {
    50         if (InvokeRequired) {
    51           Invoke(new Action<bool>((ce) => { CancelEnabled = ce; }), value);
    52         } else {
    53           cancelButton.Visible = value;
    54         }
    55       }
    56     }
    57 
    58     /// <param name="parentView">This is the View which will be locked if lockParentView is true</param>
    59     public ProgressView(ContentView parentView, IProgress progress) {
    60       InitializeComponent();
    61       this.parentView = parentView;
    62       this.Progress = progress;
    63 
    64       this.CancelEnabled = false;
    65 
    66       progressBar.Style = ProgressBarStyle.Marquee;
    67 
    68       this.Left = (parentView.ClientRectangle.Width / 2) - (this.Width / 2);
    69       this.Top = (parentView.ClientRectangle.Height / 2) - (this.Height / 2);
    70       this.Anchor = AnchorStyles.Left | AnchorStyles.Top;
    71 
    72       LockBackground();
    73 
    74       parentView.Controls.Add(this);
    75       this.BringToFront();
    76     }
    77 
    78     private void RegisterProgressEvents() {
    79       progress.Finished += new EventHandler(progress_Finished);
    80       progress.StatusChanged += new EventHandler(progress_StatusChanged);
    81       progress.ProgressValueChanged += new EventHandler(progress_ProgressChanged);
    82     }
    83 
    84     private void DeregisterProgressEvents() {
    85       progress.Finished -= new EventHandler(progress_Finished);
    86       progress.StatusChanged -= new EventHandler(progress_StatusChanged);
    87       progress.ProgressValueChanged -= new EventHandler(progress_ProgressChanged);
    88     }
    89 
    90     void progress_Finished(object sender, EventArgs e) {
    91       Finish();
    92     }
    93 
    94     void progress_ProgressChanged(object sender, EventArgs e) {
    95       if (InvokeRequired) {
    96         Invoke(new EventHandler(progress_ProgressChanged), sender, e);
    97       } else {
    98         progressBar.Style = ProgressBarStyle.Blocks;
    99         this.progressBar.Value = Math.Min(this.progressBar.Maximum, (int)(progress.ProgressValue * progressBar.Maximum));
    100       }
    101     }
    102 
    103     void progress_StatusChanged(object sender, EventArgs e) {
    104       if (InvokeRequired) {
    105         Invoke(new EventHandler(progress_StatusChanged), sender, e);
    106       } else {
     122    private void UpdateProgressStatus() {
     123      if (InvokeRequired) Invoke((Action)UpdateProgressStatus);
     124      else {
     125        string status = progress.Status;
    107126        statusLabel.Text = progress.Status;
    108       }
    109     }
    110 
    111     private void LockBackground() {
    112       if (InvokeRequired) {
    113         Invoke(new Action(LockBackground));
    114       } else {
    115         this.parentView.Locked = true;
    116         foreach (Control c in this.parentView.Controls) {
    117           c.Enabled = false;
    118         }
    119         this.Enabled = true;
    120         this.ReadOnly = false;
    121127      }
    122128    }
     
    127133      } else {
    128134        progressBar.Value = progressBar.Maximum;
     135        parentView.Controls.Remove(this);
    129136        parentView.Locked = false;
    130         foreach (Control c in this.parentView.Controls) {
    131           c.Enabled = true;
    132         }
    133         parentView.Controls.Remove(this);
     137        parentView.ReadOnly = false;
     138        foreach (Control c in this.parentView.Controls)
     139          c.Enabled = false;
    134140        DeregisterProgressEvents();
    135         this.Dispose();
     141        Dispose();
    136142      }
    137143    }
    138144
    139145    private void cancelButton_Click(object sender, EventArgs e) {
     146      OnCanceled();
    140147      Finish();
    141148    }
    142149
     150    private void SetCancelButtonVisibility() {
     151      if (InvokeRequired) {
     152        Invoke((Action)SetCancelButtonVisibility);
     153      } else {
     154        cancelButton.Visible = cancelEnabled;
     155      }
     156    }
     157
    143158    private void OnProgressChanged() {
    144       progress_StatusChanged(this, EventArgs.Empty);
    145       progress_ProgressChanged(this, EventArgs.Empty);
     159      UpdateProgressStatus();
     160      UpdateProgressValue();
    146161    }
    147162
  • trunk/sources/HeuristicLab.MainForm.WindowsForms/3.3/Views/ProgressView.designer.cs

    r7967 r8111  
    3939      this.progressBar.Name = "progressBar";
    4040      this.progressBar.Size = new System.Drawing.Size(352, 23);
    41       this.progressBar.Style = System.Windows.Forms.ProgressBarStyle.Continuous;
     41      this.progressBar.Style = System.Windows.Forms.ProgressBarStyle.Marquee;
    4242      this.progressBar.TabIndex = 0;
    4343      //
Note: See TracChangeset for help on using the changeset viewer.