【问题标题】:Formatting strings in a mutiline TextBox在多行文本框中格式化字符串
【发布时间】:2017-01-17 10:42:38
【问题描述】:

我想以这种格式将 3 个字符串添加到多行 TextBox:

  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 吗?
  • 我需要的输出是在开头写的,我需要str1、str22、str333右对齐,数字也右对齐
  • 你必须使用文本框吗?
  • 我不能使用文本框,但它是以前工作中存在的

标签: .net string vb.net string-formatting


【解决方案1】:

首先,将字体更改为fixed-width 字体。我选择了Courier New。

现在,您需要知道数字的最大长度是多少。在您的示例中,这是 222222,其长度为 6。

为了实现这一点,我所做的是将数字添加到Dictionary:

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

下一项工作是遍历Dictionary 并将值添加到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()

这给了我以下输出:

如果你不想按照我的逻辑,在你的代码中实现你所追求的,你可以改变:

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

收件人:

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

请注意,6 是硬编码的,不推荐使用,因为其他值可能会发挥作用,长度可能会更长,并且会导致格式失效。

我还直接从您的问题中复制了您的代码,并发现了一个小问题。

这个String.Format(" {0:d6}", n1) 在第一个参数中有一个空格,它将在您的格式中进行说明。去掉String.Format("{0:d6}", n1)之类的空格。

【讨论】:

    【解决方案2】:

    你不能通过 .NET 中已经存在的函数来实现吗?

    txtKopfring.TextAlign = HorizontalAlignment.Right
    

    【讨论】:

      【解决方案3】:

      如果您不必使用文本框,则可以使用数据网格视图。 在表单中插入一个 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
      

      【讨论】:

      • 感谢您的回答,但 datagrid 视图需要更多的空间在表单上,​​实际上我没有所需的空间,因为我必须在表单上显示超过 20 个文本框,我宁愿去团体
      • @waleedalmukawi,有更多行的数据网格视图可以吗?顺便说一句,如果你需要更多空间,你可以改变它的外观;)
      猜你喜欢
      • 1970-01-01
      • 2018-05-20
      • 1970-01-01
      • 1970-01-01
      • 2018-05-22
      • 2021-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多