在多行文本框中格式化字符串

3
我想要在一个多行的TextBox中按照以下格式添加3个字符串:
  str1:    3000
 srr22:   23044
str333:  222222

我需要将这些字符串右对齐在TextBox中。
我尝试了以下代码:
Dim s1 As String = " str1: "
Dim n1 As Integer = 3000

Dim s3 As String=vbCrLf & String.Format("{0,15}", s1) & String.Format(" {0:d6}", n1)
txtKopfring.Text = s3
s1 = "str22: "
n1 = 23044

s3 = s3 & vbCrLf & String.Format("{0,15}", s1) & String.Format("{0:d6}", n1)
txtKopfring.Text = s3

s1 = "str333: "
n1 = 222222

s3 = s3 & vbCrLf & String.Format("{0,15}", s1) & String.Format("{0:d6}", n1)
txtKopfring.Text = s3

然而输出结果并非预期,您能否提供提示以获得正确的输出结果?


你只需要对n1进行对齐吗? - nbadaud
我需要的输出写在开头,我需要 str1、str22 和 str333 右对齐,同时数字也要右对齐。 - waleed almukawi
你必须使用文本框吗? - nbadaud
我不能使用文本框,但它存在于以前的工作中。 - waleed almukawi
3个回答

3

首先,将字体更改为等宽字体。我选择了Courier New

现在,为了使其正常工作,您需要知道数字的最大长度。在您的示例中,这是222222,长度为6

为了实现这一点,我将数字添加到字典中:

Dim numbers As New Dictionary(Of String, Integer)
numbers.Add("str1: ", 3000)
numbers.Add("str22: ", 23044)
numbers.Add("str333: ", 222222)

我随后发现 .Values 集合的最大长度,以确定哪个数字具有最大的长度,并使用 maxLength 填充这些数字:

Dim maxLength As Integer = numbers.Values.Max.ToString.Length

下一个任务是循环遍历字典并将值添加到StringBuilder中:
'Import System.Text
Dim sb As New StringBuilder

For Each number As KeyValuePair(Of String, Integer) In numbers
    sb.AppendLine(String.Format("{0,15}", number.Key) & String.Format("{0:d6}", number.Value.ToString.PadLeft(maxLength)))
Next

要使用StringBuilder,您需要导入System.Text

最后:
txtKopfring.Text = sb.ToString()

这会给我以下输出:

enter image description here

如果你不想按照我的逻辑来实现你代码中想要达到的效果,你可以修改:
String.Format(" {0:d6}", n1)

致:

String.Format("{0:d6}", n1.ToString.PadLeft(6))

请注意,6是硬编码的,并且不建议使用其他长度可能更长的值,否则会导致格式混乱。此外,我从您的问题中直接复制了您的代码并注意到了一个小问题。这个 String.Format(" {0:d6}", n1) 在第一个参数中有一个空格,这将在您的格式化中显示。请删除空格,像这样 String.Format("{0:d6}", n1)

0
如果您不必使用textBox,可以使用dataGridView。 在您的表单中插入一个dataGridView并将其命名为dtgv。
然后,插入以下代码:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    ' Set columns properties
    initialisation_dtgv()

    With dtgv
        ' Set 3 rows.
        .RowCount = 3

        ' Set values for your cells
        .Rows(0).Cells(0).Value = "str1"
        .Rows(0).Cells(1).Value = 3000

        .Rows(1).Cells(0).Value = "srr22:"
        .Rows(1).Cells(1).Value = 23044

        .Rows(2).Cells(0).Value = "str333:"
        .Rows(2).Cells(1).Value = 222222
    End With
End Sub

Private Sub initialisation_dtgv()
    With dtgv
        Dim cols1 As New System.Windows.Forms.DataGridViewTextBoxColumn
        cols1.Name = "STR"
        cols1.HeaderText = "STR"
        cols1.ToolTipText = "STR"
        cols1.Width = 50
        cols1.Visible = True
        cols1.DisplayIndex = 1
        cols1.SortMode = DataGridViewColumnSortMode.NotSortable
        ' Set the alignment of your column
        cols1.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        .Columns.Add(cols1)

        Dim coln1 As New System.Windows.Forms.DataGridViewTextBoxColumn
        coln1.Name = "NUMBER"
        coln1.HeaderText = "NUMBER"
        coln1.ToolTipText = "NUMBER"
        coln1.Width = 60
        coln1.Visible = True
        coln1.DisplayIndex = 2
        ' Set the alignment of your column
        coln1.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        coln1.SortMode = DataGridViewColumnSortMode.NotSortable
        .Columns.Add(coln1)
    End With
End Sub

谢谢您的回答,但是DataGridView在表单上需要更多的空间,而实际上我没有足够的空间,因为我必须在表单上显示超过20个文本框,所以我宁愿选择使用GroupBox。 - waleed almukawi
@waleedalmukawi,使用更多行的DataGridView是否可以?顺便说一下,如果需要更多空间,您可以更改其外观 ;) - nbadaud

0

你不能使用已经存在于.NET中的函数来完成吗?

txtKopfring.TextAlign = HorizontalAlignment.Right

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接