我想在消息框中显示一个字符串,格式如下:
机器:TestMachine 用户:UserName
我这样做:
string strMsg = "Machine :" + "TestMahine" + "\r\n" +
"User :" + "UserName";
MessageBox.Show(strMsg);
当我这样做时,消息框不会显示上面格式化的字符串。冒号(“)不保持对齐。上述格式在WPF TextBlock控件中也不起作用。
请帮助!!
答案 0 :(得分:5)
尝试这样的事情:
string strMsg = String.Format("Machine\t: {0}\nUser\t: {1}", "TestMachine", "UserName");
已编辑:必须在那里使用String.Format,或者最后单独的括号很难过。
答案 1 :(得分:3)
原因是消息框以机器和用户长度不同的字体显示。
您可以尝试以下方法:
"Machine\t:" + "TestMahine" + "\r\n" +
"User\t:" + "UserName";
\t
字符可能会正确对齐您的冒号。
答案 2 :(得分:2)
在WPF(或WinForms,或Java,或Qt,或其他任何)TextBlock中,如果要对齐字符,则需要使用固定的字体长度,以使每个字符的长度都与其他。
<强>即。使用“Courier New
”或“Monospaced
”或“Consolas
”等字体。
在MessageBox中,您无法控制font-family。如果您真的想要这个功能,您是否考虑过创建自定义的Window组件?像这样......
<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));
}
}
因此,您可以使用以下命令调用新的消息框:
string strMsg =
"Machine : " + "TestMahine" + "\r\n" +
"User : " + "UserName";
CustomMessageBox.Show("Hello !", strMsg);
当然,你需要对你的自定义消息框视图做一些小的安排,但你明白了这一点; - )
答案 3 :(得分:1)
我尝试在MessageBox
中对齐数据时遇到了同样的问题,搜索了一段时间后,我决定使用TextRenderer.MeasureText
编写自己的方法来获取像素级的测量值。该方法有两个参数:第一个是要格式化的字符串,第二个是要显示在冒号左侧的最长字符串(在此示例中为对齐字符)。
该方法正在执行的操作是在空格之前添加空格,直到字符串达到但不超过最长字符串的长度。调用方法如下所示,其中“部门:”是最长的字符串。
StringBuilder sb = new StringBuilder();
string longest = "Department: ";
sb.AppendLine(StringLengthFormat("Result(s): ", longest) + results);
进行格式化的实际方法是:
private string StringLengthFormat(string inString, string longest)
{
Size textSizeMax = TextRenderer.MeasureText(longest, System.Drawing.SystemFonts.DefaultFont);
Size textSizeCurrent = TextRenderer.MeasureText(inString, System.Drawing.SystemFonts.DefaultFont);
do
{
inString = " " + inString;
textSizeCurrent = TextRenderer.MeasureText(inString, System.Drawing.SystemFonts.DefaultFont);
} while (textSizeCurrent.Width < textSizeMax.Width);
return inString;
}
因为我还可以有一个或多个不以“描述”开头的行,后跟冒号,但仍需要对齐,所以我想出了一种使用相同方法格式化这些行的方法。我使用回车和制表符"\r\t"
连接该数据,然后用我的方法替换制表符"\t"
。 请注意,在此示例中,我在冒号后面添加了两个空格,以便给格式化方法添加一些前缀,并且在格式化字符串之前,我会修剪尾随"\r\t"
。
下面显示了完整的代码部分,然后是该代码创建的示例MessageBox
输出的链接。
string results = "";
StringBuilder sb = new StringBuilder();
string longest = "Department: ";
foreach (EncounterInfo enc in lei)
{
results += enc.Description + " " + enc.ResultValue + " " + enc.ResultUnits + "\r\t";
}
results = results.TrimEnd(new char[] { '\r', '\t' });
results = results.Replace("\t", StringLengthFormat(" ", longest));
sb.AppendLine(StringLengthFormat("Result(s): ", longest) + results);
sb.AppendLine(StringLengthFormat("Patient: ", longest) + ei.PatientName);
sb.AppendLine(StringLengthFormat("Accession: ", longest) + ei.AccessionNumber);
sb.AppendLine(longest + ei.CollectionClassDept);
sb.AppendLine();
sb.AppendLine("Continue?");
dr = MessageBox.Show(sb.ToString(), "Add to Encounters", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
答案 4 :(得分:1)
基于Terrence Meehan指定的方法,我重新编写了StringLengthFormat
方法,如下所示:
using System.Drawing;
using System.Windows.Forms;
enum Alignment { Left = 0, Right = 1};
static string StringLengthFormat(string inputString, string longestString,
Alignment alignment=Alignment.Left, string margin="")
{
Size textSizeMax = TextRenderer.MeasureText(longestString + margin, SystemFonts.MessageBoxFont);
Size textSizeCurrent = TextRenderer.MeasureText(inputString, SystemFonts.MessageBoxFont);
do
{
if (alignment == Alignment.Left)
{
inputString += " ";
}
else
{
inputString = " " + inputString;
}
textSizeCurrent = TextRenderer.MeasureText(inputString, SystemFonts.MessageBoxFont);
} while (textSizeCurrent.Width < textSizeMax.Width);
return inputString;
}
区别如下:
System.Drawing.SystemFonts.DefaultFont
代替System.Drawing.SystemFonts.MessageBoxFont
; margin
设置列之间的距离(通过传输所需的空格数); alignment
设置列的对齐方式(左或右)。如果需要,可以对代码进行补充,以使中心对齐选项也出现。答案 5 :(得分:0)
tring strMsg =“Machine:”+“TestMahine \ t \ nUser:”+“UserName”; MessageBox.Show(strMsg);