Windows Phone Developers

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