Showing posts with label c#. Show all posts
Showing posts with label c#. Show all posts

Wednesday, July 03, 2013

Lock task bar in windows mobile

I had to do a kiosk mode application in windows mobile for which locking task bar was very much mandatory, as I had to block users from accessing mobile settings.
Here is a small piece of code that I had to use to achieve the same.


#region enable or disable taskbar swipe
public static bool enableTaskbar(bool bEnable)
{
    bool bRet = false;
    IntPtr hTaskbar = FindWindow("HHTaskBar", String.Empty);
    if (hTaskbar != IntPtr.Zero)
        bRet = EnableWindow(hTaskbar, bEnable);
    return bRet;
}

[DllImport("coredll.dll", SetLastError = true)]
private static extern IntPtr FindWindow(string lpClass, string lpTitle);

[DllImportAttribute("coredll.dll", SetLastError = true)]
private static extern bool EnableWindow(IntPtr hWnd, bool bEnable);

#endregion
There is a catch in this code. Every time mobile is locked and unlocked, task bar can be used. So write this piece of code in paint event of the form as below.

private void frmMain_Paint(object sender, PaintEventArgs e)
{
    KioskMode.enableTaskbar(false);
}

Wednesday, June 26, 2013

Change System date time of windows Mobile

This class will change the system date time  of windows Mobile.
scenario:
sync your mobile time with centralized server.

class TimeSyncer
    {
        [DllImport("coredll.dll")]
        private extern static uint SetSystemTime(ref SYSTEMTIME lpSystemTime);


        private struct SYSTEMTIME
        {
            public ushort wYear;
            public ushort wMonth;
            public ushort wDayOfWeek;
            public ushort wDay;
            public ushort wHour;
            public ushort wMinute;
            public ushort wSecond;
            public ushort wMilliseconds;
        }

        private DateTime GetDateTime(DateTime currentDateTime)
        {
            double hourOffsetValue;

            // *** Converting time to GMT
            //gmt from india is -5.5 hrs
            hourOffsetValue = -5.5;
            currentDateTime = currentDateTime.AddHours(
            hourOffsetValue);

            return currentDateTime;
        }

        public void SetSystemDateTime(DateTime serverDateTime)
        {
            try
            {
                SYSTEMTIME st = new SYSTEMTIME();

                DateTime updatedDateTime = GetDateTime(
                 serverDateTime);

                st.wYear = (ushort)updatedDateTime.Year;
                st.wMonth = (ushort)updatedDateTime.Month;
                st.wDay = (ushort)updatedDateTime.Day;
                st.wHour = (ushort)updatedDateTime.Hour;
                st.wMinute = (ushort)updatedDateTime.Minute;
                st.wSecond = (ushort)updatedDateTime.Second;

                uint nReturn = SetSystemTime(ref st);


            }
            catch (Exception ex)
            {
                throw;
            }

        }
    }


Friday, June 14, 2013

Getting only IPv4 address in c#

 private string GetMyIP()
        {
            IPHostEntry hostentry = Dns.GetHostEntry(Dns.GetHostName());
            if (hostentry != null)
            {
                IPAddress[] collectionOfIPs = hostentry.AddressList;

                foreach (IPAddress address in collectionOfIPs)
                {
                    if (address.AddressFamily != System.Net.Sockets.AddressFamily.InterNetwork)
                        continue;
                    if (IPAddress.IsLoopback(address))
                        continue;

                    return address.ToString();
                }

                return null;
            }
            else
                return null;
        }


Allow only numbers and decimal in a textbox

This piece of code will allow users to enter only numbers and one decimal point (.) in a textbox
private void txtMCurReading_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsControl(e.KeyChar))
            {
                //
            }
            else if (char.IsLetter(e.KeyChar))
            {
                e.Handled = true;
            }
            else if (Char.IsNumber(e.KeyChar) || e.KeyChar == '.')
            {
                TextBox tb = sender as TextBox;
                int cursorPosLeft = tb.SelectionStart;
                int cursorPosRight = tb.SelectionStart + tb.SelectionLength;
                string result = tb.Text.Substring(0, cursorPosLeft) + e.KeyChar + tb.Text.Substring(cursorPosRight);
                string[] parts = result.Split('.');
                if (parts.Length > 1)
                {
                    if (parts[1].Length > 2 || parts.Length > 2) // change here for setting number of chars after decimal

                    {
                        e.Handled = true;
                    }
                }
                if (parts[0].Length > 10) // change here for setting number of chars before decimal
                {
                    e.Handled = true;
                }
            }
            else if (!Char.IsNumber(e.KeyChar))
            {
                e.Handled = true;
            }

        }