In a WPF (or WinForms, or Java, or Qt, or whatever) TextBlock, if you want characters to be aligned, you need to use a fixed font length, in order for every character to have the same length than the others.
i.e. Use a font like "Courier New
" or "Monospaced
" or "Consolas
".
In a MessageBox, you cannot control the font-family. If you really want this feature, did you consider creating a customized Window component ? Like this...
<Window x:Class="WpfApplication1.CustomMessageBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
SizeToContent="WidthAndHeight" MaxWidth="500">
<Grid Margin="10">
<TextBlock FontFamily="Courier New" Text="{Binding Message}" />
</Grid>
</Window>
.
public partial class CustomMessageBox : Window, INotifyPropertyChanged {
private string message;
public string Message {
get { return message; }
set { message = value; RaisePropertyChanged("Message"); }
}
public CustomMessageBox() {
InitializeComponent();
DataContext = this;
}
public static void Show(string title, string message) {
var mbox = new CustomMessageBox();
mbox.Title = title;
mbox.Message = message;
mbox.ShowDialog();
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string property) {
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
As a result, you can invoke your new messagebox with:
string strMsg =
"Machine : " + "TestMahine" + "\r\n" +
"User : " + "UserName";
CustomMessageBox.Show("Hello !", strMsg);
Of course, you will need to do some little arrangements to your custom message box view but you got the idea ;-)