Archive

Archive for January, 2011

How to Determining the Active MDI Child

January 28, 2011 Leave a comment

There are situations wherein we need to work with the active MDI child window from a group of opened MDI child windows. In such situation .NET provides “ActiveMDIChild” property which returns the child form that has the focus or that was nost recently active.

From the example given below, we have an MDI parent form (MDIParent1.cs) and assume that we have one or more child windows. the following code to retrieve the active MDI Child is: “this.ActiveMdiChild;”

// Determine the active child form.
Form activeChild = this.ActiveMdiChild;

// If there is an active child form, do 
if (activeChild != null)
{
	
}

In the event to iterate among the availabe MDI child windows, we use “this.MdiChildren”:

foreach (Form child in this.MdiChildren)
{

}
Advertisements
Categories: C# Tags: , , ,

Convert Milliseconds, Seconds, Minutes, Hours, Days using C#

January 27, 2011 7 comments

In our example here, The “TimeConversionUtil” class contains all the required wrapper methods (already provided by the “TimeSpan” class) for converting to and from Milliseconds, Seconds, Minutes, Hours and Days; This helper class gives us the flexibility to convert easily using C# language.

using System;

class TimeConversionUtil
{

    #region Convert To Days

    public static double ConvertMillisecondsToDays(double milliseconds)
    {
        return TimeSpan.FromMilliseconds(milliseconds).TotalDays;
    }

    public static double ConvertSecondsToDays(double seconds)
    {
        return TimeSpan.FromSeconds(seconds).TotalDays;
    }

    public static double ConvertMinutesToDays(double minutes)
    {
        return TimeSpan.FromMinutes(minutes).TotalDays;
    }

    public static double ConvertHoursToDays(double hours)
    {
        return TimeSpan.FromHours(hours).TotalDays;
    }

    #endregion

    #region Convert To Hours

    public static double ConvertMillisecondsToHours(double milliseconds)
    {
        return TimeSpan.FromMilliseconds(milliseconds).TotalHours;
    }

    public static double ConvertSecondsToHours(double seconds)
    {
        return TimeSpan.FromSeconds(seconds).TotalHours;
    }

    public static double ConvertMinutesToHours(double minutes)
    {
        return TimeSpan.FromMinutes(minutes).TotalHours;
    }

    public static double ConvertDaysToHours(double days)
    {
        return TimeSpan.FromDays(days).TotalHours;
    }

    #endregion

    #region Convert To Minutes

    public static double ConvertMillisecondsToMinutes(double milliseconds)
    {
        return TimeSpan.FromMilliseconds(milliseconds).TotalMinutes;
    }

    public static double ConvertSecondsToMinutes(double seconds)
    {
        return TimeSpan.FromSeconds(seconds).TotalMinutes;
    }

    public static double ConvertHoursToMinutes(double hours)
    {
        return TimeSpan.FromHours(hours).TotalMinutes;
    }

    public static double ConvertDaysToMinutes(double days)
    {
        return TimeSpan.FromDays(days).TotalMinutes;
    }

    #endregion

    #region Convert To Seconds

    public static double ConvertMillisecondsToSeconds(double milliseconds)
    {
        return TimeSpan.FromMilliseconds(milliseconds).TotalSeconds;
    }

    public static double ConvertMinutesToSeconds(double minutes)
    {
        return TimeSpan.FromMinutes(minutes).TotalSeconds;
    }

    public static double ConvertHoursToSeconds(double hours)
    {
        return TimeSpan.FromHours(hours).TotalSeconds;
    }

    public static double ConvertDaysToSeconds(double days)
    {
        return TimeSpan.FromDays(days).TotalSeconds;
    }

    #endregion

    #region Convert To Milliseconds

    public static double ConvertSecondsToMilliseconds(double seconds)
    {
        return TimeSpan.FromSeconds(seconds).TotalMilliseconds;
    }

    public static double ConvertMinutesToMilliseconds(double minutes)
    {
        return TimeSpan.FromMinutes(minutes).TotalMilliseconds;
    }

    public static double ConvertHoursToMilliseconds(double hours)
    {
        return TimeSpan.FromHours(hours).TotalMilliseconds;
    }

    public static double ConvertDaysToMilliseconds(double days)
    {
        return TimeSpan.FromDays(days).TotalMilliseconds;
    }

    #endregion

}

How to email data from an InfoPath using JavaScript

January 27, 2011 2 comments

There are many ways to send data through email from an Infopath form.
1.) Use Rules to send email in InfoPath
2.) Use an EmailSubmitConnection to send email in InfoPath
3.) Use SPUtility.SendMail to send email in InfoPath
4.) Use System.Net.Mail classes to send email in InfoPath

Here is another way to send data using JavaScript:

function SendEmail(to,cc,bcc,subject,body)
{
	var myOlApp = new ActiveXObject("Outlook.Application");
	newMail = myOlApp.CreateItem(0);
	newMail.To = to;
	newMail.Subject = subject;
	newMail.CC = cc;
	newMail.BCC = bcc;
	newMail.Body = body;
	newMail.Send();
}

The “SendEmail” function takes the following “string” parameters and an example would be:

SendEmail('Balaji@example.com','Baskar@example.com','','Regarding JavaScript Example','A new sourcecode has been posted for sending email using JavaScript. thanks!');

Using PowerStatus in System.Windows.Forms.SystemInformation

January 26, 2011 9 comments

From the definitions of MSDN “PowerStatus” that indicates the current system power status. This is a needed class if you are developing applications that uses mobile devices; This class allows you to gets information about the battery life of a mobile device (in my case ‘laptop’).

I however do not feel convinced placing this class under “System.Windows.Forms” namespace and could have been under “System.Diagnostics” Namespace; Anyways may be its because “System.Windows.Forms” had “SystemInformation”.

An example program is given below: (If you are trying this in console program, dont forgot to add reference to “System.Windows.Forms”)

using System;
using System.Windows.Forms;

namespace PowerStatusConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {

            string strBatteryChargeStatus, strBatteryFullLifetime, strBatteryLifePercent, strBatteryLifeRemaining, strPowerLineStatus;
            strBatteryChargeStatus = strBatteryFullLifetime = strBatteryLifePercent = strBatteryLifeRemaining = strPowerLineStatus = string.Empty;

            // Getting the current battery charge status. 
            switch (SystemInformation.PowerStatus.BatteryChargeStatus)
            {
                case BatteryChargeStatus.Charging:
                    strBatteryChargeStatus = "BatteryChargeStatus: Charging";
                    break;
                case BatteryChargeStatus.Critical:
                    strBatteryChargeStatus = "BatteryChargeStatus: Critical";
                    break;
                case BatteryChargeStatus.High:
                    strBatteryChargeStatus = "BatteryChargeStatus: High";
                    break;
                case BatteryChargeStatus.Low:
                    strBatteryChargeStatus = "BatteryChargeStatus: Low";
                    break;
                case BatteryChargeStatus.NoSystemBattery:
                    strBatteryChargeStatus = "BatteryChargeStatus: NoSystemBattery";
                    break;
                case BatteryChargeStatus.Unknown:
                    strBatteryChargeStatus = "BatteryChargeStatus: Unknown";
                    break;
            }

            // Getting the reported full charge lifetime of the primary battery power source in seconds.
            strBatteryFullLifetime = "BatteryFullLifetime: " + SystemInformation.PowerStatus.BatteryFullLifetime.ToString();

            // Getting the approximate percentage of full battery time remaining.
            strBatteryLifePercent = "BatteryLifePercent: " + (100 * SystemInformation.PowerStatus.BatteryLifePercent).ToString();

            // Getting the approximate number of seconds of battery time remaining.
            strBatteryLifeRemaining = "BatteryLifeRemaining: " + (TimeSpan.FromSeconds(SystemInformation.PowerStatus.BatteryLifeRemaining).Minutes).ToString();

            // Getting the current system power status.
            switch (SystemInformation.PowerStatus.PowerLineStatus)
            {
                case PowerLineStatus.Offline:
                    strPowerLineStatus = "PowerLineStatus: Offline";
                    break;
                case PowerLineStatus.Online:
                    strPowerLineStatus = "PowerLineStatus: Online";
                    break;
                case PowerLineStatus.Unknown:
                    strPowerLineStatus = "PowerLineStatus: Unknown";
                    break;
            }

            Console.WriteLine(strBatteryChargeStatus);
            Console.WriteLine(strBatteryFullLifetime);
            Console.WriteLine(strBatteryLifePercent);
            Console.WriteLine(strBatteryLifeRemaining);
            Console.WriteLine(strPowerLineStatus);
            Console.ReadLine();

        }
    }

Getting username() in Infopath 2003 using JScript

January 25, 2011 2 comments

In Infopath 2007 version you have username() function to get the user id (lan id) of the user, however Infopath 2003 you use java script to get the user name.

function GetUserID()
{
var objNetwork = new ActiveXObject("WScript.network");
var uid = objNetwork.UserName;
return uid;
}

Getting Infopath 2007 talk to Sharepoint’s “UserProfileService”

January 24, 2011 1 comment

Requirement: If you wanted to read any information about the user (example: Preferred name, Phone) based on the the login id then querying for Sharepoint’s “UserProfileService” would be an ideal option.

The “UserProfileService” can be found at:

http://<server>/_vti_bin/UserProfileService.asmx

or, if it’s a sub-site

http://<server>/<site1>/_vti_bin/UserProfileService.asmx

From your Infopath 2007 Form:

Goto Tools -> Data Connections -> click "Add" to open up "Data Connection Wizard".

Select "Create a new connection to: Receivet data" and click "Next" button.

Select "To a Web service" under "How do you want to submit your data?" and click "Next" button.

Specify full URL for the "UserProfileService" web service and click "Next" button.

Select "GetUserProfileByName" under "Select an operation:" and click "Next" button.

Do not specify any value (" Do not click on the 'Set Value' button") and click "Next" button.

Make sure that "Store a copy of the data in the form template" remains unselected and click on "Next" button.

Enter a name for this data connection and ensure that the "Automatically retrieve data when form is opened" is unselected and click "Finish" button to close this wizard.

Click "Close" button to close the "Data Connections" window.

In a real-time scenario lets say, we need “WorkEmail” for the given “UserName”:

From Rules window, click "Add" button to open "Rule" window.

I am not setting any conditions for this rule.

Click "Add Action" button to open "Action" window.

Select "Set a field's value" under Action, Click on the icon button under Field to open "Select a Field or Group" window, choose the data source (the name specified under data connections for "GetUserProfileByName").

Navigate myFields -> queryFields -> s0GetUserProfileByName -> select "AccountName" and click "OK" button.

Enter / Type "userName()" under Value and click "OK" button.

Click "Add Action" button again to open "Action" window.

Select "Query using a data connection" under "Action" and choose the data source (the name specified under data connections for "GetUserProfileByName") and click "OK" button.

Click "Add Action" button again to open "Action" window and Select "Set a field's value" under Action and choose "your" desired field under "Field".

Click on the "fx" button under "Value" to open "Insert Formula" window.

Click on "Insert Field or Group" button top open "Select a Field or Group" window.

In "Select a Field or Group" window, choose the data source (the name specified under data connections for "GetUserProfileByName") and navigate to "myFields" -> "dataFields" -> "s0:GetUserProfileByNameResponse" -> "GetUserProfileByNameResult" -> "PropertyData" -> Select "Name" and click on "Filter Data" button.

In "Filter Data" window, Click "Add" button to open "Specify Filter Conditions".

The first drop-down contains "Name" by default, choose "Select a field or group" to open "Select a Field or Group" window.

Make sure that the Data source points to the "GetUserProfileByName" data source (the name specified under data connections for "GetUserProfileByName") and navigate to "myFields" -> "dataFields" -> "s0:GetUserProfileByNameResponse" -> "GetUserProfileByNameResult" -> "PropertyData" -> "Values" -> "ValueData" -> select "Value" and click on "OK" button to return to "Specify Filter Conditions" window.

The next drop-down is set to "is equal to" and for the third drop-down, type text "<em>WorkEmail</em>" and click "OK" for all the open windows until you exit the "Rules" window.

Note: in our example here, we have used “WorkEmail” to get the Email address, likewise we can specify FirstName, LastName, PreferredName, WorkPhone, Office, Department, CellPhone, Fax, HomePhone, etc.

%d bloggers like this: