Windows Phone Developers

Sunday, February 22, 2009

Cannot implicitly convert type 'int' to 'System.Collections.Generic.IEnumerable

Cannot implicitly convert type 'int' to 'System.Collections.Generic.IEnumerable

public static IEnumerable<int> GetOdd()

{

// Use yield to return only the odd numbers.

foreach (int i in ints)

if (i % 2 == 1)

return i;

}

Check if you are missing any yield statement in the return

public static IEnumerable<int> GetOdd()

{

// Use yield to return only the odd numbers.

foreach (int i in ints)

if (i % 2 == 1)

yield return i;

}


Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

Tuesday, February 17, 2009

How to use Background worker control in C#

How to print a document in background using C#

Many time-consuming tasks can be done asynchronously. This can be done using multithreaded applications. Fortunately visual studio .net has the background worker control does all the mundane multithreading efforts by itself

Let us go back to our good old select file application (as shown below). To the form add the background worker control, which will be placed on the tray.















Once the user clicks the OK button, the background worker is called using RunWorkerAsync method. This will execute the backgroundWorker1_DoWork event while the Windows form responds to the user actions.

private void buttonOK_Click(object sender, EventArgs e)

{

if (txtExcelFile.Text.Length == 0)

{

System.Console.Beep();

MessageBox.Show ("Select the file and continue...");

}

backgroundWorker1.RunWorkerAsync(txtExcelFile.Text);

}

Status of the process is updated to the Form

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)

{

// Replace with the actual code of printing the document

String sFile = e.Argument.ToString();

for (int i1 = 1; i1 <>

{

for (int i2 = 1; i2 <>

{

toolStripStatusLabel1.Text = "Printing " + sFile + "...";

}

}

}

You can also use the backgroundWorker1.ReportProgress method to update the status through backgroundWorker1_ProgressChanged event

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)

{

toolStripStatusLabel1.Text = e.ProgressPercentage.ToString() + " percent completed";

}

The completion of the background worker can be ascertained by the RunWorkerCompleted event

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)

{

toolStripStatusLabel1.Text = "Printing completed";

}


How to run a process in background using .NET, How to handle background events in .NET

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

How to use Treeview designer in C# Application

C# Treeview designer example

Treeview control gives a nice hierarchical view for the users of a given concept. The following example shows how to create a simple tree-view control using the designer

Add a Tree View Control to the form





Select the TreeView Tasks from the control

Add the root item – XYZ Automobiles




Click the root item and then click on the Add Child to add children. Repeat the steps to add more children




The form will be displayed as shown below





The above can be done through C# code as shown below:

private void AddTreeViewControls()

{

treeView1.Nodes.Add("Root","XYZ Autombiles");

treeView1.Nodes["Root"].Nodes.Add("PV", "Passenger Vehicles");

treeView1.Nodes["Root"].Nodes.Add("CV", "Commercial Vehicles");

}

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

This BackgroundWorker states that it doesn't report progress. Modify WorkerReportsProgress to state that it does report progress.



The error occurs when the WorkerReportsProgress of the background worker is not set.

This should be set (to True ) from the properties window



Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

How to get the selected node in tree view from MouseDown event using C#

C# - MouseDown event in Tree View Control

Here is a way to get the selected tree view node using C#. The snippet uses the X and Y co-ordinates to get the node

private void treeView1_MouseDown(object sender, MouseEventArgs e)

{

Point p1 = new Point(e.X, e.Y);

TreeNode N1 = treeView1.GetNodeAt(p1) ;

MessageBox.Show(N1.Text);

}

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

How to add nodes to the tree view control using C#

How to build a tree view using C#

The following snippet helps to build a simple tree view.



Tree View Control

Add a Tree View Control to the form




The following code adds a simple treeview with a root node and two children

private void AddTreeViewControls()

{

treeView1.Nodes.Add("Root","XYZ Autombiles");

treeView1.Nodes["Root"].Nodes.Add("PV", "Passenger Vehicles");

treeView1.Nodes["Root"].Nodes.Add("CV", "Commercial Vehicles");

}

The nodes are added using the Add method – the first parameter is the Key and the second is the node text. The child nodes are added to the root using the Key

treeView1.Nodes["Root"]


Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

How to sort listview items in C#

Here is a simple example of sorting list view items using C#

listView1.Items.Add("Banana");

listView1.Items.Add("Apple");

listView1.Sorting = SortOrder.Ascending ;




ListView before Sorting


ListView after Sorting



Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

How to delete all items from a ListView using C#

To remove all listview items use the clear method

For example:

listView1.Clear();

will empty the listview

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon