Windows Phone Developers

Tuesday, April 21, 2009

How to Disable Custom Colors in ColorDialog using C# (.NET)

How to restrict the user to pre-defined colors in C# ColorDialog

With .NET’s ColorDialog one can select the pre-defined as well as the custom colors. For some reason, if you want only the pre-defined colors to be selected. Set the AllowFullOpen to false.

private void btnSelectColor_Click(object sender, EventArgs e)

{

colorDialog1.AllowFullOpen = false;

colorDialog1.ShowDialog();

this.BackColor = colorDialog1.Color;

}


ColorDialog with custom colors
Restricted ColorDialog

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

Friday, April 17, 2009

How to use ColorDialog in C# (.NET)

Allow user to customize form background color using ColorDialog

The Windows Forms ColorDialog component is a pre-configured dialog box that allows the user to select a color from a palette and to add custom colors to that palette. It is the same dialog box that you see in other Windows-based applications to select colors. Use it within your Windows-based application as a simple solution in lieu of configuring your own dialog box.

To add the dialog to Windows Form select the ColorDialog from the tool box





The component will be dropped in the component-tray below the form


Use the following code to show the dialog and set the selected color as the background of the form

private void btnSelectColor_Click(object sender, EventArgs e)

{

colorDialog1.ShowDialog();

this.BackColor = colorDialog1.Color;

}



The form will be set as shown below




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

Saturday, March 7, 2009

Argument '1': cannot convert from 'int' to 'System.Windows.Forms.ListViewItem.ListViewSubItem'

The best overloaded method match for 'System.Windows.Forms.ListViewItem.ListViewSubItemCollection.Add(System.Windows.Forms.ListViewItem.ListViewSubItem)' has some invalid arguments

ListViewItem lView = new ListViewItem() ;

lView.SubItems.Add(36);

lView.SubItems.Add("36");

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 Set the Control’s background to system colors

Here is a way to use the available default system colors like control, button face etc using C#

listView1.BackColor = System.Drawing.SystemColors.ControlDark;



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

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