Windows Phone Developers

Sunday, May 29, 2011

ForEach Loop in Array (C# / VB.NET)

How to iterate elements of Array using ForEach in .NET (C#)

Here is a simple example of that:

string[] arString = null;
            arString = new string[2];
            arString[0] = "First Element";
            arString[1] = "Second Element";
            foreach (string s1 in arString)
            {
                Console.WriteLine(s1);
            }
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, May 25, 2011

How to Write Padded (Formatted) Text File using VB.NET/C#

Writing Equi-Spaced Output using C# and VB.NET

Here is a small code to write a multiplication table.

For i1 As Integer = 1 To 20

Console.WriteLine(i1.ToString & " x 12 = " & (i1 * 12).ToString)

Next i1




Have you ever thought it would be better to have it properly aligned. Then use the Padding options (PadeLeft here) to format the output



Private Sub WritePaddedText()

        ' Write a Padded Multiplication Table
        For i1 As Integer = 1 To 20
            Console.WriteLine(i1.ToString.PadLeft(2) & " x  12 = " & (i1 * 12).ToString.PadLeft(3))
        Next i1
    End Sub


See also :How to Create Equispaced Text File using VBA; Spacing in Text Files 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

Unable to copy file "obj\x86\Debug\*.exe" to "bin\Debug\*.exe". The process cannot access the file 'bin\Debug\*.exe' because it is being used by another process.

To release the file go to the Task Manager and see if the Exe is running in the process Tab. Kill the process and Build the project it should work.

But before that make sure why the Exe was still running in the process even after the application is termninated. It might be due to some malperformance of garbage collection 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, May 24, 2011

VB.NET How to Resize the form along with Controls

Windows Form - Resize Controls Dynamically using VB.NET

There would be many occasions you would have come across the need to resize the form to fit some controls/text etc. In the following example we use the combination of LinkLabel and Panel Control to achieve that

Here is the sample form

This is how it needs to be expanded to accomodate the additonal information

I have used a '+/-' hyperlink (LinkLabel Control) to resize the form. (For more info on Link Label : Hyperlink Control in Windows Forms (VB.NET) Application). You can use >> or << etc to be more clear

The controls for Year Founded and Financial Performance are enclosed in a Panel

Add the following code to the LinkClicked event of the LinkLabel

If Panel1.Height = 0 Then
            Panel1.Height = 110
            Me.Height = Me.Height + 110
        Else
            Panel1.Height = 0
            Me.Height = Me.Height - 110

        End If


The form gets re-sized on clicking the hyperlink (it actually toggles)

See also : How to Get Width and Height of Desktop Screen using C#/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

Sunday, May 15, 2011

Hyperlink Control in Windows Forms (VB.NET) Application

How to use Hyperlinks on WinForms (VB.NET)

Create a small Windows Forms like the one below and add LinkLabel Control on the form:


Add the following code to the LinkLabel's LinkClicked event:

Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
        System.Diagnostics.Process.Start("www.google.com")
    End Sub


This will open the website in a new IE 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 find Difference between two time (Dates) using VB.NET

Difference between two different dates using VB.NET

DATEDIFF function will come handy to find the difference between a set of dates. The snippet given below gets the difference in seconds

Private Sub DateDiffExample()

        Dim dt1 As Date = New Date(2011, 5, 15, 10, 11, 2)
        Dim dt2 As Date = New Date(2011, 5, 15, 10, 12, 22)

        Dim dt3 As Long = DateDiff(DateInterval.Second, dt1, dt2)

    End Sub

You can tweak a bit to get for Minutes or Hours etc by

Private Sub DateDiffExample()

        Dim dt1 As Date = New Date(2011, 5, 15, 10, 11, 2)
        Dim dt2 As Date = New Date(2011, 5, 15, 10, 12, 22)

        Dim dt3 As Long = DateDiff(DateInterval.Minute, dt1, dt2)

    End Sub

Other useful links:

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

Changes are not allowed while code is running or if the option 'Break all

If you are getting this error while editing the code, check if the Break all process option is enabled.


If 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, May 14, 2011

How to get number of Sundays/Saturdays in a given month using (C#/VB.NET)

Retrieve No of Fridays for the Current Month.

If you come across a situation where you need to know the number of Fridays/Sundays etc for a given month you can use the following snippet.

private void NoOfSpecificDaysThisMonth()
        {
            int iDayCnt = 4; // Defaults to four days
            DayOfWeek dw = DayOfWeek.Wednesday;
            
            DateTime firstDay = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
            int iRemainder = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month) % 7;

            if ((dw >= firstDay.DayOfWeek) & ((dw - firstDay.DayOfWeek) < iRemainder))
                {
                     iDayCnt = iDayCnt+1;
                }
                else
                    if ((dw < firstDay.DayOfWeek) & ((7+ dw - firstDay.DayOfWeek) < iRemainder))
                {

                    iDayCnt = iDayCnt + 1;
                }
            
            MessageBox.Show(iDayCnt.ToString());
        }

The above gives the No Of Wednesdays for the Current month. If you want for any specific month / year you can tweak it a bit like:


DateTime firstDay = new DateTime(2012, DateTime.Now.Month, 1);
            int iRemainder = DateTime.DaysInMonth(2012, DateTime.Now.Month) % 7;

Other links that might be relevant:

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

Select Case Statement in C# / C-sharp

Switch Case Statement in C# (.NET)

Here is an example of Select Case like construct using C#:

private string ShirtSizes(int ShirtSize)
        {
            switch (ShirtSize)
            {
                case 36:
                    return "Small";
                case 38:
                    return "Medium";
                case 40:
                    return "Large";
                case 42:
                    return "Extra Large";
                case 44:
                    return "XXL";
                default:
                    return "Free Size";
             }

        }

Fall-Thru in Switch Case Statement in C#(Csharp)

Fall through can be achieved as follows:

switch (ShirtSize)
            {
                case 34:
                case 35:
                case 36:
                    return "Small";
                case 38:
                    return "Medium";
... 

The Switch construct requires jump statements like GoTo or Break to transfer control outside the loop. Else it will throw an Error Control cannot fall through from one case label ### to another

To avoid this use break / goto statements. On the other hand if you use more than one jump statement for a case - like the one shown below - you will get Unreachable code detected warning

case 38:
                    return "Medium";
                    break;
             
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, May 11, 2011

Control cannot fall through from one case label - Switch Statement (C#/CSharp)

This error occurs when there are no jump statement in the switch. Like the following

switch  (sDayName.ToUpper () )
            {
                case"1":
                    MessageBox.Show("One");
                case "2":
                    MessageBox.Show("Two");
        }

A break or goto statement should solve the problem

switch  (sDayName.ToUpper () )
            {
                case"1":
                    MessageBox.Show("One");
                    break;
                case "2":
                    MessageBox.Show("Two");
                    goto default;
...

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, May 10, 2011

Mod Functon in C# (Csharp)

Mod Operator in C#

Here is the code for Mod function in C#. Have given two ways. The first one is the C % operator

private void ModFunctionCsharpExample()
        {
            int iRemainder = 31 % 3;
            MessageBox.Show("Remainder is " + iRemainder.ToString());

            int iQuotient = Math.DivRem(31, 3, out iRemainder);
            MessageBox.Show("Remainder is " + iRemainder.ToString() + " and the Quotient is " + iQuotient);
        }

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, May 1, 2011

Form's Keydown Event not Firing in VB.NET

How to enable Keydown, Keypress events for Windows forms (.NET)

If the Keydown, Keypress events etc are not fired in WinForms application, check if the KeyPreview is set

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 Function Keys in VB.NET Windows Application





Capture Function Keys in VB.NET Windows Application

The following snippet might help you in capturing the Function Keys (F1 to F12) in a VB.NET application

Private Sub frmWinSamAppMain_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        ' -- Sample Code for http://DotNetDud.BlogSpot.com
        Select Case e.KeyCode
            Case Keys.F1
                MessageBox.Show("Help is on the way")
            Case Keys.F5
                RefreshMethod()
            Case Keys.F9
                MessageBox.Show("You have pressed F9")
        End Select
    End Sub
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 Bind an Object to TextBox

Binding Objects to Windows Form Controls

Here is a simple example to bind a object to textbox control

Let us have a small form with two Textboxes, a label and a button



We also have a class that has two properties - Name of Company and the Business they are involved. Here is how the class looks

Public Class ClassDataBind

    Private sPrvName As String
    Private sOfferings As String

    Public Sub New(ByVal sName As String, ByVal sOffer As String)
        Me.Name = sName
        Me.Solutions = sOffer
    End Sub

    Property Name() As String

        Get
            Return sPrvName
        End Get

        Set(ByVal value As String)
            sPrvName = value
        End Set

    End Property


Let us now Bind the class to the text boxes using

Private Sub FormDataBind_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        oDataBind = New ClassDataBind("Accenture", "Consulting")

        TextBox1.DataBindings.Add("Text", oDataBind, "Name", True, DataSourceUpdateMode.OnPropertyChanged)
        TextBox2.DataBindings.Add("Text", oDataBind, "Solutions", True, DataSourceUpdateMode.OnPropertyChanged)
    End Sub


Once the binding is done , run the code to view how it looks :


Since the text boxes are bound to the Objects and we have set the Objects to be refreshed we can change the contents in textboxes and see if the Objects are refreshed.

Private Sub ButtonClicked(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Label1.Text = oDataBind.Name + " - " + oDataBind.Solutions
    End Sub


You can now save the object back to DB etc if needed. If you don;t want to update certain field. For example, the company name here - you can do the following: 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