在多行文本框中格式化字符串
Formatting strings in a mutiline TextBox
我想以这种格式将 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
但是输出结果与预期不符,您能否提供提示以使输出正确?
你不能通过 .NET 中已经存在的函数来实现吗?
txtKopfring.TextAlign = HorizontalAlignment.Right
如果您不必使用文本框,则可以使用数据网格视图。
在您的表单中插入一个 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
首先,将字体更改为 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
To use a StringBuilder
you will have to import System.Text
.
最后:
txtKopfring.Text = sb.ToString()
这给了我以下输出:
如果您不想遵循我的逻辑,要在您的代码中实现您想要的,您可以更改:
String.Format(" {0:d6}", n1)
收件人:
String.Format("{0:d6}", n1.ToString.PadLeft(6))
Note the 6 is hardcoded and not recommended as other values may come into play which could be longer in length and will throw the formatting out.
另外,我直接从你的问题中复制了你的代码,发现了一个小问题。
此 String.Format(" {0:d6}", n1)
在第一个参数中有一个 space,它将在您的格式中显示。像这样删除 space String.Format("{0:d6}", n1)
.
我想以这种格式将 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
但是输出结果与预期不符,您能否提供提示以使输出正确?
你不能通过 .NET 中已经存在的函数来实现吗?
txtKopfring.TextAlign = HorizontalAlignment.Right
如果您不必使用文本框,则可以使用数据网格视图。 在您的表单中插入一个 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
首先,将字体更改为 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
To use a
StringBuilder
you will have to importSystem.Text
.
最后:
txtKopfring.Text = sb.ToString()
这给了我以下输出:
如果您不想遵循我的逻辑,要在您的代码中实现您想要的,您可以更改:
String.Format(" {0:d6}", n1)
收件人:
String.Format("{0:d6}", n1.ToString.PadLeft(6))
Note the 6 is hardcoded and not recommended as other values may come into play which could be longer in length and will throw the formatting out.
另外,我直接从你的问题中复制了你的代码,发现了一个小问题。
此 String.Format(" {0:d6}", n1)
在第一个参数中有一个 space,它将在您的格式中显示。像这样删除 space String.Format("{0:d6}", n1)
.