Kategorie: Programming

Adding Localization to .NET WPF Applications

Here I’d like to describe some approaches how .NET applications can be localized. General In general the texts that are supposed to be localized should be added to the Resources.resx file located under Properties of the project: The data is stores in an XML format, but also type save access properties are created automatically. Don’t…
Weiterlesen

Strip BOM from a File or Text

The following code removes the BOM (Byte Order Mark) from texts. Some text editors add that while saving files. Sometimes this leads to problems while reading or converting text files (e.g. XML files). The code removes the BOM and Zero White Spaces from the string and returns the clean string. /// <summary> /// Strips the…
Weiterlesen

Check if Code is Executed at Design Time

Sometimes it is important to prevent certain code being executed at design time. public static bool IsDesignTime { get { return (System.ComponentModel.LicenseManager.UsageMode == System.ComponentModel.LicenseUsageMode.Designtime); } } In WPF applications the following code can be used instead: public static bool IsDesignTime { get { System.ComponentModel.DesignerProperties.GetIsInDesignMode(new DependencyObject()); } }  

Get the Path of the Executing Assembly

public static string GetExecutingAssemblyPath() { string codeBase = Assembly.GetExecutingAssembly().CodeBase; UriBuilder uri = new UriBuilder(codeBase); string path = Uri.UnescapeDataString(uri.Path); return Path.GetDirectoryName(path); }  

Showing a ComboBox with Installed Fonts

<ComboBox ItemsSource=“{Binding Source={x:Static Fonts.SystemFontFamilies}}“ SelectedItem=“{Binding SelectedFont, UpdateSourceTrigger=PropertyChanged}“ /> The ComboBox is bound to the System Fonts enumeration. The SelectedItem DependencyProperty is bound to a SelectedFont property of the ViewModel.

Using a Comparer to Sort CultureInfos by their Name

If you need to show a list of languages, or more general CultureInfos, you might want to sort them by the name they show up in the GUI. This can easily be done by using a custom comparer. This implements the generic IComparer<T> interface. public class CultureInfoComparer : IComparer<CultureInfo> { public int Compare(CultureInfo x, CultureInfo…
Weiterlesen

Calendar tools and algorithms

Calculating a leap year The correct calculation of a leap year was first introduced by pope Gregor in 1582. The Gregorian Calendar is still commonly used in most parts of the world. Before that, the leap years were simply defined being every 4th year. public static bool IsLeapYear(int year) { if (year % 100 ==…
Weiterlesen

Use Text Wrapping in a WPF DataGrid TextColumn

You can simply use styles to enable text wrapping in a WPF DataGrid Column. This sets the element style of the standard column control and of the column control that is used when the cell is in edit mode. <DataGridTextColumn> <DataGridTextColumn.ElementStyle> <Style TargetType=“TextBlock“> <Setter Property=“TextWrapping“ Value=“WrapWithOverflow“/> <Setter Property=“TextTrimming“ Value=“WordEllipsis“/> </Style> </DataGridTextColumn.ElementStyle> <DataGridTextColumn.EditingElementStyle> <Style TargetType=“TextBox“> <Setter…
Weiterlesen

Check Availability of a Server

This checks the availability of a server by sending a ping to the given IP address and waiting a certain amount of time for a response from the server: public static bool ServerAvailable(string ip, int timeout) { Ping pingSender = new Ping(); PingReply reply; try { reply = pingSender.Send(ip, timeout); } catch { return false;…
Weiterlesen

Change string into a valid file name

Not all possible characters are suitable for file names. For example path separators cannot be used for file names. This changes any string into a valid file name by replacing the invalid characters with an underscore: public static string GetValidFileName(string fileName) { foreach (char c in Path.GetInvalidFileNameChars()) {   fileName = fileName.Replace(c, ‚_‘); } return fileName; }