venerdì 3 luglio 2015

Create a custom dialog box with Modern UI 1.5.0

The Modern UIfor WPF is a good solution to create pretty WPF application.
Unfortunately the FirstFloor.ModernUI.Windows.Controls.ModernDialog class is usefull only to show simple text (alert dialog).

The following function helps you to create a custom dialog box using the ModernDialog class.

/// <summary>Simple input dialog box</summary>
/// <returns>null if the dialog box was closed with "cancel",
/// otherwise the user input<returns>
public static string ShowInputDialog(string initialText=null)
{
  // create the dialog content
  TextBox content = new TextBox();
  content.Text = initialText;

  // create the ModernUI dialog component with the buttons
  var dlg = new ModernDialog
  {
    Title = "Insert a text",
    Content = content,
    MinHeight = 0,
    MinWidth = 0,
    MaxHeight = 480,
    MaxWidth = 640,
  };
  dlg.Buttons = new Button[] { dlg.OkButton, dlg.CancelButton };

  // register the event to retrieve the result of the dialog box 
  string result = null;
  dlg.OkButton.Click += (object sender, RoutedEventArgs e) =>
  {
    result = content.Text;
  };

  dlg.ShowDialog();
  return result;
}

giovedì 2 luglio 2015

Retrieve the version of the .NET runtime

Sometimes the simplest questions remains unanswered: what's the .NET runtime version executes my app? .NET 4.0 or .Net 4.5?
Hier you find a simple C# function to retrieve the version of the .NET runtime on which your app is executed.

public static string RetrieveHumanRuntimeVersion()
{
 string text = Environment.Version.ToString();
 if (!text.StartsWith("4.0.30319.")) return text;
 int build, start = text.LastIndexOf('.');
 if (!Int32.TryParse(text.Substring(start + 1), out build)) return text;
 if (build <= 17000) return ".NET 4.0";
 if (build <= 19000) return ".NET 4.5";
 if (build <= 34000) return ".NET 4.5.1";
 return ".NET 4.5.2";
}

More details here