tags:

views:

66

answers:

4

I want to display a string in a message box with a following format:

Machine : TestMachine User : UserName

I am doing this:

string strMsg = "Machine  :" + "TestMahine" + "\r\n" +
                "User     :" + "UserName";

MessageBox.Show(strMsg);

When I do this the message box do not display a string as formated above. Colon(") dosen't keep alligned. The above format is also do not work in WPF TextBlock control.

Please help!!

A: 

tring strMsg = "Machine :" + "TestMahine\t\nUser :" + "UserName"; MessageBox.Show(strMsg);

william
+3  A: 

The reason for this is that the message box is displayed in a font where Machine and User may not be the same length.

You could try the following:

"Machine\t:" + "TestMahine" + "\r\n" +
"User\t:" + "UserName";

The \t character will probably correctly align your colons.

Pieter
Thanks Pieter,its working for c# message box but not for WPF text block.
Arvind Bhatt
You should continue with @JonVD if you accepted that answer.
Pieter
+1  A: 

Try something like this:

string strMsg = String.Format("Machine\t: {0}\nUser\t: {1}", "TestMachine", "UserName");

Edited: Gotta have that String.Format there or that lone bracket at the end is sad.

JonVD
string strMsg = string.Format(...
Aurélien Ribon
+1  A: 

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 ;-)

Aurélien Ribon
You can also create a more specific window, for example by using a grid to align 4 textblocks in two columns, and binding the right textblocks to some UserName and Machine fields. People are used to MessageBoxes that do not consider "Courier New" as a font, do not disturb their habits ;-)
Aurélien Ribon