Windows Phone Developers

Wednesday, December 23, 2009

Check Multiple Instances of an Application using C#

How to check if multiple instances of the application are running using C# (.NET)

Here is a simple code to check if multiple instances of current application are running in the machine using System.Diagnostics

private static void check_application_process_instances()

{

Process[] oProcess;

String sModuleName;

String sProcessName;

sModuleName = Process.GetCurrentProcess().MainModule.ModuleName;

sProcessName = System.IO.Path.GetFileNameWithoutExtension(sModuleName);

oProcess = Process.GetProcessesByName(sProcessName);

if (oProcess.Length > 1 )

{

MessageBox.Show("More than one instance is running!");

}

}


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

Where are the .NET DLLs available in the system

.NET DLLs (3.0 and above)are available @ C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5

Or

C:\Windows\Microsoft.NET\Framework\v2.0.50727

C# .NET DLLs, System DLLs in Windows



.NET DLL Location.NET DLL Location


.NET DLL Location.NET DLL Location

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

Cannot implicitly convert type 'System.Diagnostics.Process[]' to 'System.Diagnostics.Process'

The compiler requires an explicit conversion. For example, you may need to cast an r-value to be the same type as an l-value. Or, you must provide conversion routines to support certain operator overloads.

Process oProcess;

oProcess = Process.GetProcessesByName(sProcessName);

Since GetProcessesByName creates an array of new Process components and associates them with the existing process resources that all share the specified process name, we need to declare oProcess as shown below:

Process[] oProcess;

oProcess = Process.GetProcessesByName(sProcessName);

C# Error Cannot implicitly convert type 'System.Diagnostics.Process[]' to 'System.Diagnostics.Process', .NET Error Cannot implicitly convert type 'System.Diagnostics.Process[]' to 'System.Diagnostics.Process'

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

Cannot declare instance members in a static class

This error occurs if you declare a non-static member in a class that is declared static. It is not possible to create instances of static classes, so instance variables would not be meaningful. The static keyword should be applied to all members of static classes.

In this example get_application_process is not declared as static

static class Program

{

private void get_application_process()

{

Process apProcess;

String sModuleName;

}

Solution:

private static void get_application_process()

should solve the problem

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

The type or namespace name 'Process' could not be found (are you missing a using directive or an assembly reference?)

Yes, the using directive is missing!

using System.Diagnostics;

Should solve the problem

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 check user input in C# (.NET) / VB.NET

How to parse user input using C# (.NET)

Many times we do not get the anticipated input from the user (either as direct entry / through some input files). Converting the input, for example, to say integer can be done as shown below

public static void TryParse_Examples()

{

bool bConversion;

int result;

// Failure //

bConversion = int.TryParse(null, out result);

if (bConversion == false)

{

MessageBox.Show ("Error Occurred!");

}

// Failure //

bConversion = int.TryParse("s1", out result);

if (bConversion == false)

{

MessageBox.Show("Error Occurred!");

}

// Success //

bConversion = int.TryParse("100", out result);

if (bConversion == false)

{

MessageBox.Show("Error Occurred!");

}

}

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, June 13, 2009

How to mask the Password text in Textboxes (in VB.NET / C#)

It is simple to mask the characters entered in a textbox by setting the PasswordChar Property

The same can be done using C# code as shown below:

TextBox2.PasswordChar = "*"



Text Box without Mask
PasswordChar Property in TextBox

Masked Textbox


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

Frame Control in VB.NET / C#

GroupBox control in .NET (Vb/C#)

GroupBox and Panel control provide the functionality similar to the Frame control in visual basic 6.0. Drag and Drop the Frame Control to the form and set its properties accordingly




Alter the properties from properties dialog box




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, 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

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

Sunday, January 25, 2009

A simple StatusStrip Control Example using C#

C# StatusBar Example

Status bars play an important part in informing the user of the use of controls, the status of progress of the application etc. .Net provides a new control – the StatusStrip control – an extension of StatusBar

Let us have a small Windows form like the one as shown below.

Select the StatusStrip control in the ToolBox and drop it on the form




When you drop the control will be placed on the Tray below the form and a StatusStrip with a ToolStripLabel will be added to the Form






Now the ToolStripLabel can be used to apprise the user of the context and progress as shown below

private void buttonSelectXL_MouseHover(object sender, EventArgs e)

{

toolStripStatusLabel1.Text = "Click the Button to select the file";

}






.NET StatusBar Example, .NET ToolStripLabel Example

See also : Hide Excel Status Bar / Show Excel Status Bar using VBA

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 C# MouseOver Events in Windows Forms

Yet again we take the good old select file form as our example. Let us use the MouseHover event of the ellipsis button to inform the user of its function.



To add an function to handle the event, click the event tab of the button and scroll to MouseHover event and double click it.

Add the following code to the event procedure.

private void buttonSelectXL_MouseHover(object sender, EventArgs e)

{

toolStripStatusLabel1.Text = "Click the Button to select the file";

}

The toolstrip label will be updated whenever the mouse pointer is hovered over the ellipsis button.





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

C# Array UBound Function

How to get the Upper Bound of an Array in C#

The upper bound of the array can be retrieved using the Length property as shown below:

private void GetUbound()

{

String[] arTemp = {"First","Second"};

MessageBox.Show (arTemp.Length.ToString ());

}

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

OpenFileDialog in C# - How to Use OpenFileDialog in C# Windows Application

Common Dialog in C# (.NET)

CommonDialog was widely used to select the files in Visual Basic 6.0. .Net provides a OpenFileDialog component, which can be used for the same.

Let us have a small Windows form where the file to process is to be selected. The form contains a CommandButton to invoke the Open File dialog box and a TextBox to display the selected file.





To use the OpenFileDialog, insert the component from Toolbox to the form



When it is droped to a form, the OpenFileDialog component appears in the tray at the bottom of the Windows Forms Designer.





Now write the following code in the Button’s Click event to display the Open file dialog box

private void buttonSelectXL_Click(object sender, EventArgs e)

{

openXLFile.Filter = "Microsoft Excel Workbooks (*.xls)*.xls";

if (openXLFile.ShowDialog() == DialogResult.OK )

{

String sFileName = openXLFile.FileName;

txtExcelFile.Text = sFileName;

}

else

{

MessageBox.Show("Please select a file");

}

}







The above code filers only Excel files in the Open Dialog Box

The selected file will be displayed in the Textbox


See also OpenFileDialog in Visual Basic .Net

For CommonDialog implementation in VB6.0 refer http://vbadud.blogspot.com/2007/06/visual-basic-common-dialog.html 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