ComponentModel provides classes that are used to implement the run-time and design-time behavior of components and controls.
Search This Blog
Monday, December 23, 2013
C# ComponentModel
C# > System > ComponentModel
ComponentModel provides classes that are used to implement the run-time and design-time behavior of components and controls.
ComponentModel provides classes that are used to implement the run-time and design-time behavior of components and controls.
Etichete:
c#
BackgroundWorker and progress bar C# example
C# > System > ComponentModel > BackgroundWorker
The BackgroundWorker runs an operation on a separate and dedicated thread.
Time-consuming operations can cause the user interface (UI) to seem as frozen and stop responding while they are running.
BackgroundWorker class represents a solution to this.
Example
Use BackgroundWorker and progress bar to display a progress of import operation
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
/// Interaction logic for MainWindow.xaml
///
private BackgroundWorker worker = null;
public MainWindow()
{
}
void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
pBar.Value = e.ProgressPercentage;
lblStatus.Content = "Processing......" + pBar.Value.ToString() + "%";
}
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
// do your task here
for (int i = 0; i <= 100; i++)
{
Thread.Sleep(500);
if (worker == null)
break;
worker.ReportProgress(i);
}
}
private void worker_RunWorkerCompleted(object sender,RunWorkerCompletedEventArgs e)
{
lblStatus.Content = "Task finished";
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (worker == null)
{
worker = new BackgroundWorker();
worker.DoWork += worker_DoWork;
worker.RunWorkerCompleted += worker_RunWorkerCompleted;
worker.ProgressChanged += worker_ProgressChanged;
worker.WorkerReportsProgress = true;
worker.WorkerSupportsCancellation = true;
}
if (worker.IsBusy != true)
{
// Start the asynchronous operation.
worker.RunWorkerAsync();
}
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
pBar.Maximum = 100;
}
private void btnCancel_Click(object sender, RoutedEventArgs e)
{
worker.CancelAsync();
worker = null;
}
}
}
The BackgroundWorker runs an operation on a separate and dedicated thread.
Time-consuming operations can cause the user interface (UI) to seem as frozen and stop responding while they are running.
BackgroundWorker class represents a solution to this.
Example
Use BackgroundWorker and progress bar to display a progress of import operation
using System;
using System.Collections.Generic;using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace BackgroundWorker
{/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{private BackgroundWorker worker = null;
public MainWindow()
{
}
void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
pBar.Value = e.ProgressPercentage;
lblStatus.Content = "Processing......" + pBar.Value.ToString() + "%";
}
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
// do your task here
for (int i = 0; i <= 100; i++)
{
Thread.Sleep(500);
if (worker == null)
break;
worker.ReportProgress(i);
}
}
private void worker_RunWorkerCompleted(object sender,RunWorkerCompletedEventArgs e)
{
lblStatus.Content = "Task finished";
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (worker == null)
{
worker = new BackgroundWorker();
worker.DoWork += worker_DoWork;
worker.RunWorkerCompleted += worker_RunWorkerCompleted;
worker.ProgressChanged += worker_ProgressChanged;
worker.WorkerReportsProgress = true;
worker.WorkerSupportsCancellation = true;
}
if (worker.IsBusy != true)
{
// Start the asynchronous operation.
worker.RunWorkerAsync();
}
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
pBar.Maximum = 100;
}
private void btnCancel_Click(object sender, RoutedEventArgs e)
{
worker.CancelAsync();
worker = null;
}
}
}
Etichete:
c#
Parentheses () Operator C# Example
C# > Operators > () Operator
Parentheses operator is used for
1. Casting
double a = 4.5;
int b;
a = (int)q; // Cast double to int
2. Invoke
Method(); // Call method
Note: This operator cannot be overloaded!
Parentheses operator is used for
1. Casting
double a = 4.5;
int b;
a = (int)q; // Cast double to int
2. Invoke
Method(); // Call method
Note: This operator cannot be overloaded!
Etichete:
c#
Predicate Delegate C# Example
C# > System > Predicate<T> Delegate
This delegate is used to search for elements in the collection.
Predicate<T> delegate is represented by a lambda expression.
Example
public string Name { get; set; }
public int Year { get; set; }
public Student(string name, int year)
{
this.Name = name;
this.Year = year;
}
}
new Student("Stud2", 2000),
new Student("Stud3", 2010),
new Student("Stud4", 1999),
new Student("Stud5", 1995) });
This delegate is used to search for elements in the collection.
Predicate<T> delegate is represented by a lambda expression.
Example
public class Student
{public string Name { get; set; }
public int Year { get; set; }
public Student(string name, int year)
{
this.Name = name;
this.Year = year;
}
}
List<Student> groups = new List<Student>();
groups.AddRange(new Student[] { new Student("Stud1", 1973), new Student("Stud2", 2000),
new Student("Stud3", 2010),
new Student("Stud4", 1999),
new Student("Stud5", 1995) });
foreach (var g in groups.FindAll(x => x.Year >= 2000))
System.Windows.MessageBox.Show(g.Name);
Etichete:
c#
IEquatable C# Example
C# > System > Interfaces > IEquatable
public int Power { get; set; }
public bool Equals(Car other)
{
if (other == null)
return false;
if(this.Power == other.Power)
return true;
return false;
}
public override bool Equals(Object obj)
{
if (obj == null)
return false;
Car carObj = obj as Car;
if (carObj == null)
return false;
else
return Equals(carObj);
}
}
IEquatable<T> Interface defines Equals method for determining equality of instances.
Examplepublic class Car : IEquatable<Car>
{public int Power { get; set; }
public bool Equals(Car other)
{
if (other == null)
return false;
if(this.Power == other.Power)
return true;
return false;
}
public override bool Equals(Object obj)
{
if (obj == null)
return false;
Car carObj = obj as Car;
if (carObj == null)
return false;
else
return Equals(carObj);
}
}
Car c1 = new Car();
c1.Power = 100;
Car c2 = new Car();
c2.Power = 100;
if (c1.Equals(c2))
System.Windows.MessageBox.Show("Same power");
c1.Power = 100;
Car c2 = new Car();
c2.Power = 100;
if (c1.Equals(c2))
System.Windows.MessageBox.Show("Same power");
Etichete:
c#
Friday, December 20, 2013
Identify feature DotSpatial Map
DotSpatial > Controls > Map
Identify feature DotSpatial Map on MouseUP event.
Coordinate cor = map1.PixelToProj(e.Location);
Extent ex = new Extent(cor.X, cor.Y, cor.X, cor.Y);
var mLayer = map1.Layers[0];
IFeatureLayer il = (IFeatureLayer) mLayer;
List<IFeature> result = il.DataSet.Select(ex);
if (result.Count>0)
{
IFeature ift = result.FirstOrDefault();
MessageBox.Show(ift.DataRow[0].ToString());
}
}
Identify feature DotSpatial Map on MouseUP event.
private void map1_MouseUp(object sender, MouseEventArgs e)
{Coordinate cor = map1.PixelToProj(e.Location);
Extent ex = new Extent(cor.X, cor.Y, cor.X, cor.Y);
var mLayer = map1.Layers[0];
IFeatureLayer il = (IFeatureLayer) mLayer;
List<IFeature> result = il.DataSet.Select(ex);
if (result.Count>0)
{
IFeature ift = result.FirstOrDefault();
MessageBox.Show(ift.DataRow[0].ToString());
}
}
Etichete:
DotSpatial
Subscribe to:
Posts (Atom)