Windows Phone Developers

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

How to Add Resources to C# Windows Application

How to add images as resources to C# Windows Application


Resources can be added to the C# Project using Resource Page in the Project Designer (Project à Project Properties)

The Resources page of the Project Designer hosts an instance of the Resource Designer that stores and maintains resources in a single location (Resources.resx).

The Resource Designer is language-neutral and supports projects in all Visual Studio languages. Items that you add to the project by using the Resource Designer are placed in the Resources directory for your project. The designer information is stored in a file named Resources.resx, and code for the resource is stored in Resources.Designer.cs, Resources.Designer.vb, or Resources.Designer.jsl.










Right Click the Resources section in the solution explorer and Select Add à Existing Item and select the Image files that need to be added






The images will be added as shown below






Drag the images to the Resources page – Rename the resource names if needed




Now you can use the Resources in your Form. Select the BackGroundImage from the Form’s properties box – Click on the ellipsis next to the BackGroundImage property. This will open the Select Resource dialog Box





Select appropriate resource from the list



The same can be set through code using

this.BackgroundImage = Properties.Resources.User_Single

For linkedresources see - Create HTML Message with Embedded Images in VB.NET / Embed images in HTML mail message using VB.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

Cannot find wrapper assembly for type library "Microsoft.Office.Core".

Cannot find wrapper assembly for type library "Microsoft.Office.Core".


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

There are updated custom wrappers available for the following referenced components: Office.

There are updated custom wrappers available for the following referenced components: Office.



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 referenced component 'Microsoft.Office.Core' could not be found.

The referenced component 'Excel' could not be found.

The warning occurs when you open the C#/ VB.Net Solution referring a different version of Microsoft Excel / Office. For example, If you open the Project, which refers Excel 2003 (Excel 11.0 Library) in Excel 2007 machine



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 remove the Word from memory using C#

How to delete the leftover Word object using C#.

Closing Word Runtimes not removed by Word.Quit / Application.Quit

When Word crashes, it often takes with it the open documents. These documents will be write-locked and will appear as read-only to subsequent users. There are umpteen no. of reasons to crash a Word document and leave the ‘Winword.exe’ running in the memory. The following VB.NET code will remove the unused Word objects from memory.

Public Sub KillUnusedWordProcess()

Dim oXlProcess As Process() = Process.GetProcessesByName("Winword")

For Each oXLP As Process In oXlProcess

If Len(oXLP.MainWindowTitle) = 0 Then

oXLP.Kill()

End If

Next

End Sub

We have not tested the code in real-time. Our assumption was that any Word instance opened by user has a Window Title and programs using Word have been terminated. Check if the code is of any use and post the updations / suggestions to our ‘dud’ buddiesJ

automation does not close Microsoft Word, Application.Quit not closing Word

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

Excel Runtimes not removed by Excel.Quit / Application.Quit

How to delete the leftover Excel object using VB.NET / Vb.NET delete Excel from memory

Excel runtimes that remain after the program is a nemesis. Many times this might be due to the leftover Excel objects in the program that causes Excel to remain in memory even after Application.Quit. Workbooks that are modified and left open often causes this problem. We have tried out one method to get rid of the Excel in memory using Vb.NET. We assumed that the programs that used Excel has been terminated and if any Excel remains in the memory it should be of the ‘legitimate’ application that the user has opened or the left over Excel.

We checked the presence of Title for Excel Application, which was present in all cases for the open instance of Excel. Left over instances didn’t had the title. The following code ‘kills’ the instances of Excel in memory that don’t have a WindowTitle.

Public Sub KillUnusedExcelProcess()

Dim oXlProcess As Process() = Process.GetProcessesByName("Excel")

For Each oXLP As Process In oXlProcess

If Len(oXLP.MainWindowTitle) = 0 Then

oXLP.Kill()

End If

Next

End Sub

Try if it works for you and post your suggestions or modifications

Keywords : automation does not close microsoft excel, Application.Quit not closing Excel

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, January 16, 2009

No overload for method 'get_Range' takes '1' arguments

No overload for method 'get_Range' takes '1' arguments is error encountered by VB programmers trying to code in C#.

Excel.Workbook oWB = oXL.Workbooks.Open(sXLFileName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing,missing );

// processing

Excel.Worksheet oWS = (Excel.Worksheet)oWB.Worksheets[1];

oWS.get_Range("A1").Value2 = "No";

This occurs when get_Range method is supplied with only one argument. Instead use any of the following:

oWS.get_Range("A1","A1").Value2 = "No";

or

Object missing = System.Type.Missing;

oWS.get_Range("A1",missing ).Value2 = "No";




Error Dialog
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

Non-invocable member 'Microsoft.Office.Interop.Excel._Workbook.Worksheets' cannot be used like a method.

Non-invocable member 'Microsoft.Office.Interop.Excel._Workbook.Worksheets' cannot be used like a method is a common error for a VB programmer working on C# projects. The error is solved when the round brackets are replaced with square ones

Excel.Worksheet oWS = (Excel.Worksheet)oWB.Worksheets(1);

To

Excel.Worksheet oWS = (Excel.Worksheet)oWB.Worksheets[1];


Error Message
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 'object' to 'Microsoft.Office.Interop.Excel.Worksheet'. An explicit conversion exists (are you missing a cast?)

Cannot implicitly convert type 'object' to 'Microsoft.Office.Interop.Excel.Worksheet'. An explicit conversion exists (are you missing a cast?) throws up when there is no Explicit casting - (Excel.Worksheet)

Excel.Worksheet oWS =oWB.Worksheets[1];

To

Excel.Worksheet oWS = (Excel.Worksheet)oWB.Worksheets[1];


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, January 10, 2009

How to use Multithreading in C# Console Application

C# Multithreading Example – Continuous Alert

A query from our good blog friend SnakyJake led me to code a small multithreading stuff. The following example will torment the user with beeps till he/she inputs some value.

First let us create a small class that will handle the beeps

public class ClassBeep

{

private volatile bool EnoughBeeping;

public void StartBeeping()

{

while (!EnoughBeeping)

{

Console.Beep();

}

}

public void StopBeeping()

{

EnoughBeeping = true;

}

}

ClassBeep has two public methods, one to start beeping (and continuing) with it and another to stop the beep.

We also have a volatile variable EnoughBeeping which sets/resets the Beeps

This is quite simple. Isn’t it.

The next step is to code the main function to get the user input.

Wait a minute … here are the directives

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading;

The last one “System.Threading” is the one we have added to the default directives.

Let us create an object for our Beep class

ClassBeep CBeep = new ClassBeep();

and create a thread and attach the function to it

Thread CBeepThread = new Thread(CBeep.StartBeeping);

We can now start the additional thread (worker thread)

CBeepThread.Start();

while (!CBeepThread.IsAlive) ;

Beep starts …

Now we can provide some instructions to the main thread

Thread.Sleep(1);

Console.WriteLine("Please enter the name");

string sText = Console.ReadLine();

int iLen = 0;

iLen = sText.Length;

while (iLen == 0)

{

sText = Console.ReadLine();

iLen = sText.Length;

}

The above loop will be executed till the user inputs a value.

Once the execution comes out of the loop, let us stop the beep and join the worker thread with the main one

CBeep.StopBeeping();

CBeepThread.Join();

Here is the full code of the main module

static void Main(string[] args)

{

ClassBeep CBeep = new ClassBeep();

Thread CBeepThread = new Thread(CBeep.StartBeeping);

CBeepThread.Start();

while (!CBeepThread.IsAlive) ;

Thread.Sleep(1);

Console.WriteLine("Please enter the name");

string sText = Console.ReadLine();

int iLen = 0;

iLen = sText.Length;

while (iLen == 0)

{

sText = Console.ReadLine();

iLen = sText.Length;

}

CBeep.StopBeeping();

CBeepThread.Join();

Console.WriteLine("Welcome {0}", sText);

}

Try it out. You can try overload of Beep Function to make your user have soothing and rhyming beeps as background.

Hope you get things done through beepsJ

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

Beep in C# Windows forms

Alert user with beep sound for invalid entries using C#

Many times when we alert the user with some messages but the user doesn’t heed immediately. A combination of sound and popup should be more helpful. The following example would alert the user if he/she hasn’t input the required field

private void buttonOK_Click(object sender, EventArgs e)

{

if (txtExcelFile.Text.Length == 0)

{

System.Console.Beep();

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

}

}



Windows User Form

For a Rhyming Beep peep at http://dotnetdud.blogspot.com/2009/01/how-to-create-rhyming-beeps-in-c.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

How to create Rhyming Beeps in C#

How to compose a tune using C#

Beep method is a simple instrument to create some wonderful tunes. I love music, but haven’t practiced or played. Hence I will stop posting the overloaded Beep function that can be ‘tuned’ for your use

Overloaded Console.Beep method takes two arguments

Console.Beep(int32 frequency, int32 duration)

Here is a way to do it

private static void RhymingBeeps()

{

int duration = 1000;

for (int i = 1500; i <= 2000; i = i +50)

{

Console.Beep(i, duration);

duration = duration / 2;

if (duration <>

{

duration = 1000;

}

}

}

Agreed! It is not that Rhyming. But post you are welcomed to post the rhyming ones here

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

Wednesday, January 7, 2009

Check if a String has Value using IsNullOrEmpty method

Check if a String has Value using IsNullOrEmpty method

IsNullOrEmpty is a convenience method that enables you to simultaneously test whether a String is nullNothingnullptra null reference (Nothing in Visual Basic) or its value is Empty.

Unlike the usual way to check twice (once for null and next for empty string), this method does it in a single check

private void CheckValue()

{

String s1 = null ;

String s2 = "";

if (String.IsNullOrEmpty(s1) == true)

{

MessageBox.Show("String does not hold any text");

}

}

This method actually checks if the Value is null or if the length of the string is Zero.

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