Wednesday, March 4, 2009

Accessing user name of a NTID using LDAP

public static string GetFirstName(string NTID)
{
char[] seperator ={','};
char[] sep1 = {'('};
string [] st = CheckLDAPUser(NTID).Split(seperator);
string[] sp = st[1].Split(sep1);
return sp[0].ToString();
}

private static string CheckLDAPUser(string ldapUserId)
{
System.DirectoryServices.DirectoryEntry directoryEntry = new System.DirectoryServices.DirectoryEntry("LDAP://CompanyNameURL");
System.DirectoryServices.DirectorySearcher directorySearcher = new System.DirectoryServices.DirectorySearcher(directoryEntry);
directorySearcher.Filter = "(SAMAccountName=" + ldapUserId + ")";
System.DirectoryServices.SearchResult sResultSet = directorySearcher.FindOne();
string fullName = "";
if (sResultSet != null)
{
if (sResultSet.Properties.Contains("displayName"))
{
fullName = sResultSet.Properties["displayName"][0].ToString();
}

return fullName;
}
return "Anonymous";
}

Wednesday, February 4, 2009

Readonly Listbox

Use the following snippet to extend the C# listBox to a readOnly List box.

using System.Windows.Forms;
namespace ReadOnlyListBox
{
public class ReadOnlyListBox : ListBox
{
private bool _readOnly = false;
public bool ReadOnly
{
get { return _readOnly; }
set { _readOnly = value; }
}

protected override void DefWndProc(ref Message m)
{
// If ReadOnly is set to true, then block any messages
// to the selection area from the mouse or keyboard.
// Let all other messages pass through to the
// Windows default implementation of DefWndProc.
if (!_readOnly || ((m.Msg <= 0x0200 || m.Msg >= 0x020E)
&& (m.Msg <= 0x0100 || m.Msg >= 0x0109)
&& m.Msg != 0x2111
&& m.Msg != 0x87))
{
base.DefWndProc(ref m);
}
}
}
}

Screen Right Bottom Co-Ordinates

Hi,

Use the following snippet to get the right bottom coordinates in the screen. This will be useful when you write apps which alerts the user from the system Tray.

Me.Left = Screen.PrimaryScreen.WorkingArea.Width - Me.Width
Me.Top = Screen.PrimaryScreen.WorkingArea.Height - Me.Height