Advertisement
Guest User

Untitled

a guest
Dec 13th, 2014
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.63 KB | None | 0 0
  1. public int filterReference;
  2. public int focusReference;
  3.  
  4. public MainWindow()
  5. {
  6.     InitializeComponent();
  7.     PlotListView.SelectionChanged += PlotListView_SelectionChanged;
  8.  
  9.     // Establish MySQL connection
  10.     var dbObject = new DbConnect();
  11.     dbObject.OpenConnection();
  12.  
  13.     // Reset plot (job/bid)
  14.     ResetPlot(filterReference);
  15.  
  16.     // Refresh plot (job/bid)
  17.     RefreshPlotTimer();
  18. }
  19.  
  20. public void PlotListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
  21. {
  22.     if (PlotListView.SelectedItems.Count == 0) return;
  23.     var selectedItem = (DbConnect.PlotList) PlotListView.SelectedItems[0];
  24.     focusReference = Convert.ToInt32(selectedItem.PlotId);
  25.     Console.Write("PlotListView_SelectionChanged " + focusReference);
  26. }
  27.  
  28. public void ResetPlot(int filterReference)
  29. {
  30.     // Get current plot number;
  31.     int? plotNumber = filterReference;
  32.  
  33.     // Establish MySQL connection
  34.     var dbObject = new DbConnect();
  35.     dbObject.OpenConnection();
  36.  
  37.     // Fill plot list view
  38.     List<DbConnect.PlotList> plotList = dbObject.SelectPlotLists(filterReference);
  39.  
  40.     // Find the plot list in the new list
  41.     DbConnect.PlotList selectPlotList =
  42.         plotNumber.HasValue
  43.             ? plotList.Where(x => Convert.ToInt32(x.PlotId) == plotNumber.Value).FirstOrDefault()
  44.             : null;
  45.  
  46.     Dispatcher.BeginInvoke(
  47.         new ThreadStart(() => PlotListView.ItemsSource = plotList));
  48.  
  49.     // Re-select plot list if found in the new list.
  50.     if (selectPlotList != null)
  51.     {
  52.         PlotListView.SelectedItem = selectPlotList;
  53.     }
  54.  
  55.     int jobSum = 0;
  56.     int bidSum = 0;
  57.     foreach (DbConnect.PlotList item in PlotListView.Items)
  58.     {
  59.         jobSum += Convert.ToInt32(item.Jobs);
  60.         bidSum += Convert.ToInt32(item.Bids);
  61.     }
  62.    
  63.     // Determine job/bid list ratio
  64.     Dispatcher.BeginInvoke(
  65.         new ThreadStart(() => JobBidRatioTextBlock.Text = jobSum + " jobs - " + bidSum + " bids"));
  66. }
  67.  
  68. public void RefreshPlotTimer()
  69. {
  70.     // Create a timer
  71.     var refreshTimer = new Timer();
  72.  
  73.     // Tell the timer what top do once it elapses
  74.     refreshTimer.Elapsed += (sender, e) => RefreshPlot(sender, e);
  75.  
  76.     // Set it to go off at a predefined interval
  77.     refreshTimer.Interval = 2500;
  78.  
  79.     // Start the timer
  80.     refreshTimer.Enabled = true;
  81. }
  82.  
  83. public void RefreshPlot(object source, ElapsedEventArgs e)
  84. {
  85.     var dbObject = new DbConnect();
  86.     dbObject.OpenConnection();
  87.     dbObject.RefreshPlot();
  88.  
  89.     Console.WriteLine("\rBefore refresh " + focusReference);
  90.  
  91.     Dispatcher.Invoke(() =>
  92.     {
  93.         if (!string.IsNullOrWhiteSpace(FilterTextBox.Text) &&
  94.             (!Regex.IsMatch(FilterTextBox.Text, "[^0-9]")))
  95.         {
  96.             filterReference = Convert.ToInt32(FilterTextBox.Text);
  97.         }
  98.     });
  99.     ResetPlot(filterReference);
  100.  
  101.     Console.WriteLine("After refresh " + focusReference);
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement