Set focus to textbox in Microsoft Silverlight

If you want to set the focus on a textbox in Microsoft Silverlight, use the Loaded event and the System.Windows.Browser.HtmlPage.Plugin.Focus() prior to calling Control.Focus()

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace Ada.Tts.Toggle.Silverlight
{
public partial class MainPage : UserControl
{
/// <summary>
/// Event fires on page load
///
/// Set focus to search box
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
System.Windows.Browser.HtmlPage.Plugin.Focus();
this.searchTextBox.Focus();
}
/// <summary>
/// Consturctor
/// </summary>
public MainPage()
{
InitializeComponent();
}
}
}

Full property with backing field code snippet in Microsoft Visual Studio 2010

New in Microsoft Visual Studio 2010 is the “propfull” code snippet. It produces the code for a complete property with backing field.

Press CTRL + spacebar and select [propfull], now press [Tab] once, this will generate the code.
When you press [Tab] a second time you can change the type and name of the property.
You can also directly type propfull in you’re editor and press [Tab].

 

image

 

Generated code

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace Ada.Tts.Toggle.Silverlight
{
public class Task
{
private int myVar;
public int MyProperty
{
get { return myVar; }
set { myVar = value; }
}
}
}

Sync problem with Windows 2008 RC2 or Windows 7 x64 and Microsoft Outlook 2010 with Windows Mobile 6.5 resolved

If you have problems synchronising you’re Windows Mobile 6.5 on Windows 2008 RC2 and Microsoft Outlook 2010 with the message “Outlook is not the default email client”, you can set Microsoft Outlook 2010 as default email client in Microsoft Outlook 2010:

File > Options:

image

Check “Make Outlook the default program for E-Mail, Contacts, and Calendar and ”Click on “Default Programs”

image

Check the Select all checkbox.

Install Windows Mobile device centre 6.1 (Microsoft Windows Mobile Device Center 6.1 Driver for Windows Vista (64-bit).exe). Sync you’re Windows Mobile 6.5 and uncheck the One Notes sync

This worked for me, I am know syncing my HD2 Windows Mobile 6.5 phone with Microsoft Outlook 2010 on a Windows 2008 RC2 x64 server.

Short cut for pasting unformatted text, with the default paste options in Microsoft Office 2010

In most cases when I copy text from a source document or website and want to paste this text in Microsoft Office Outlook or Microsoft Office Word, I want the text to be formatted like the destination document (pasting as unformatted text). Previously I used two methods to accomplish this:

  • Paste > Paste Special > Unformatted Text > OK ( 4 clicks)
  • Notepad ctrl+a then ctrl+v the ctrl+a and then ctrl+c (1 click but many key presses)

Paste special dialog

image

In Microsoft Office 2010 (and perhaps in older Microsoft Office versions) you can now set default paste options to unformatted text. The pasted text will be formatted like the destination document

In Microsoft Office Word 2010

  • File > Options > Advanced > [Cut, copy and paste] section > Keep Text Only

image

In Microsoft Office Outlook 2010

  • Open a new mail message
  • File > Options > Mail > Editor Options > Advanced > [Cut, copy and paste] section > Keep Text Only

image

When you paste text in you’re mail or word document, it will be formatted like the mail or document.

 

Microsoft Office 2010 OneNote

  • In Microsoft Office 2010 OneNote, you can use the [Contenxt Menu Key] + T, instead of [CTRL + V] to paste unformatted text.
  • Or use alt + h + v + t

How to get all items from a list that are not unique with LINQ and C#

If you want to get all items from a list that are not unique with LINQ and C#, you can use:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
namespace Ada.Cdf.Test
{
[TestFixture]
public class IntegrationTester
{
[Test]
[Explicit("Not a unittest")]
public void Test()
{
List<string> items = new List<string>();
items.Add("Item 1");
items.Add("Item 2");
items.Add("Item 3");
items.Add("Item 4");
items.Add("Item 2");
items.Add("Item 3");
var result = from i in items
group i by i into g
where g.Count() > 1
select new {Group=g.Key, ItemCount= g.Count()};
foreach (var test in result)
{
Console.WriteLine(string.Format("Group[{0}] ItemCount[{1}]", test.Group, test.ItemCount));
}
}
}
}

 

Result

Group[Item 2] ItemCount[2]

Group[Item 3] ItemCount[2]

 

How to count items per category with LINQ and C#

If you want to count items per category with LINQ in C#, you van use:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
namespace Ada.Cdf.Test
{
[TestFixture]
public class IntegrationTester
{
[Test]
[Explicit("Not a unittest")]
public void Test()
{
List<string> items = new List<string>();
items.Add("Item 1");
items.Add("Item 2");
items.Add("Item 3");
items.Add("Item 4");
items.Add("Item 2");
items.Add("Item 3");
var result = from i in items
group i by i into g
select new {Group=g.Key, ItemCount= g.Count()};
foreach (var test in result)
{
Console.WriteLine(string.Format("Group[{0}] ItemCount[{1}]", test.Group, test.ItemCount));
}
}
}
}

 

Result

Group[Item 1] ItemCount[1]

Group[Item 2] ItemCount[2]

Group[Item 3] ItemCount[2]

Group[Item 4] ItemCount[1]

 

 

Get index of an item with LINQ and C#

If you want to use the index of an item with LINQ in C#, you can use the “index” statement:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
namespace Ada.Cdf.Test
{
[TestFixture]
public class IntegrationTester
{
[Test]
[Explicit("Not a unittest")]
public void Test()
{
List<string> items = new List<string>();
items.Add("Item 1");
items.Add("Item 2");
items.Add("Item 3");
items.Add("Item 4");
items.Add("Item 2");
items.Add("Item 3");
var result = items.Select((item, index) => new { index, item });
//select new { Name=i, Ind=index };
foreach (var test in result)
{
Console.WriteLine(string.Format("item[{0}] index[{1}]", test.item, test.index));
}
}
}
}

Result

item[Item 1] index[0]

item[Item 2] index[1]

item[Item 3] index[2]

item[Item 4] index[3]

item[Item 2] index[4]

item[Item 3] index[5]

1 passed, 0 failed, 0 skipped, took 2,81 seconds (NUnit 2.4).

How to automatically determine the installation order / sequence of TSQL table scripts with foreignkeys by using C#

If you have a folder with (*.sql) TSQL table scripts and you want to deploy these scripts, you can manually add the file names by adding numbers to set the order of installation.

001. Cars.sql
002. Customers.sql
etc.

Or you can use a C# function to automatically determine the installation order of the TSQL table scripts:

public void ExecuteTableTSQLScriptFiles(string server, string database, string userId, string password, bool trustedConnection, string folder)
{
// Validate parameters
if (string.IsNullOrEmpty(server)) { throw new ArgumentNullException("server"); }
if (string.IsNullOrEmpty(database)) { throw new ArgumentNullException("database"); }
if (string.IsNullOrEmpty(userId)) { throw new ArgumentNullException("userId"); }
if (string.IsNullOrEmpty(password)) { throw new ArgumentNullException("password"); }
if (string.IsNullOrEmpty(folder)) { throw new ArgumentNullException("folder"); }
if (!Directory.Exists(folder)) { new ApplicationException(string.Format("Folder [{0]] does not exist", folder)); }
// Get *.sql files in folder
List<string> filesToProcess = new List<string>();
filesToProcess.AddRange(Directory.GetFiles(folder, "*.sql"));
// First loop will install all tables with no foreignkeys
// Second loop will install all tables with foreignkeys that are not part of a foreignkey
// Preceding loops will install all tables with foreignkeys that are part of a foreignekey
// Use loopCounter to prevent endless loop
int loopCounter = 0;
while (filesToProcess.Count() > 0 && loopCounter <= 10)
{
List<string> reProcessFiles = new List<string>();
foreach (string file in filesToProcess)
{
try
{
// Get script file contents
string scriptContents = File.ReadAllText(file);
// Make sure alle GO statements are capatilized
scriptContents = scriptContents.Replace("\r\ngo\r\n", "\r\nGO\r\n");
// Remove leading and trailing spaces
scriptContents = scriptContents.Trim();
// Determine if file contents ends with go statement
if (scriptContents.EndsWith("go") || scriptContents.EndsWith("GO"))
{
// Remvove last go statement
scriptContents = scriptContents.Remove(scriptContents.Length - 2);
}
//split the script on "GO" commands
string[] splitter = new string[] { "\r\nGO\r\n" };
string[] commandTexts = scriptContents.Split(splitter, StringSplitOptions.RemoveEmptyEntries);
// Determine connection string
string connectionString = string.Format(@"Server=""{0}"";Database=""{1}"";User ID=""{2}"";Password=""{3}"";Trusted_Connection={4};", server, database, userId, password, trustedConnection);
// Run each sql command in the sql script file
foreach (string commandText in commandTexts)
{
using (SqlConnection sqlConnection = new SqlConnection(connectionString))
{
using (SqlCommand command = new SqlCommand(commandText, sqlConnection))
{
command.Connection.Open();
command.ExecuteNonQuery();
}
}
}
}
catch (Exception ex)
{
// Add as first file to reProcessFiles, when exception starts with [Foreign key]
if (!string.IsNullOrEmpty(ex.Message) && ex.Message.StartsWith("Foreign key"))
{
reProcessFiles.Insert(0, file);
}
}
}
filesToProcess = reProcessFiles;
loopCounter++;
}
}


It’s a dirty function but somebody had to make it 😉

How to PowerPivot outlook data (*.ost or *.pst) data in Microsoft Office Excel 2010

First I extracted some data from the outlook inbox with C# to a CSV file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Outlook;
using System.IO;
namespace Common
{
public class Generator
{
const string Splitter = ",";
const string CsvFIleName = "MailData.csv";
const string CsvPath = @"C:\BDATA";
public void Start()
{
Application outlook = new Application();
// Get the inbox folder
MAPIFolder folder = outlook.GetNamespace("MAPI").GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
// Get received mails
IEnumerable<Microsoft.Office.Interop.Outlook.MailItem> items = folder.Items.OfType<Microsoft.Office.Interop.Outlook.MailItem>();
// Clear file
File.WriteAllText(Path.Combine(CsvPath, CsvFIleName), string.Empty);
foreach (MailItem item in items)
{
StringBuilder csvContent = new StringBuilder(string.Empty);
// Add [Sender] to output
if (item.Sender != null && !string.IsNullOrEmpty(item.Sender.Name)) { csvContent.Append(item.Sender.Name.Replace(Splitter, string.Empty)); }
csvContent.Append(Splitter);
// Add [Subject] to output
if (!string.IsNullOrEmpty(item.Subject)) { csvContent.Append(item.Subject.Replace(Splitter, string.Empty)); }
csvContent.Append(Splitter);
// Add [Year] to output
csvContent.Append(item.ReceivedTime.Year);
csvContent.Append(Splitter);
// Add [Month] to output
csvContent.Append(item.ReceivedTime.Month);
csvContent.Append(Splitter);
// Add [Day] to output
csvContent.Append(item.ReceivedTime.Day);
csvContent.Append(Splitter);
// Add [Hour] to output
csvContent.Append(item.ReceivedTime.Hour);
// Per line export, so you will see the filesize grow on filesystem
using (StreamWriter sw = File.AppendText(Path.Combine(CsvPath, CsvFIleName)))
{
sw.WriteLine(csvContent.ToString());
}
}
}
}
}

  • Open the CSV file with Microsoft Office Excel 2010 and File > Save As “MailData.xlsx”

image

image

  • Click on the ribbon tab [Data], select column A and then click on [Text to Columns]

image

  • Click [Delimited] and click [Next]

image

  • Uncheck [Tab] and check [Comma], then click [Next]

image

  • Click Finish

image

  • Result

image

  • Press Ctrl + L to create a table

image

  • Rename the generated column names (Column1 to Name etc.)

image

  • Click on the excel tab [PowerPivot] and click on [Create Linked Table]

image

  • In the powerpivot screen add column [Count] with formula [=1]

image

  • Click on PivotTable to create a PowerPivot report in Microsoft Excel 2010

image

  • Click on [Chart and Table (Horizontal)

image

  • Click ok
  • Select chart
  • Drag and drop [Count] to [Values]
  • Drag and drop [Name] to [Axis Fields (Categories)]
  • Select chart and right click chart
  • Click [Change Chart Type]

image

  • Select Bar and click [OK]

image

  • Click on [Name] in the chart

image

  • Choose More Sort Options…

image

  • Sort [Ascending (A to Z) by] [Sum of Count ]

image

 

  • Again click on [Name] in the chart and click [Value Filters] [Greater Than]

image

  • The email adresses in the screendumps are partial hidden, but you can see by hovering over the first bar, that [Heide] has send me the most messages [679]

image

  • Select chart and Drag and drop [Hour] to [Slicers Horizontal]

image

  • If you click on 12 in the hour slicer, you can see [Heide] had sent 49 mails on 12 o’clock