在Visual Basic中对齐字符串

时间:2016-10-14 02:28:27

标签: vb.net visual-studio

所以我有这个字符串,并且所有字符串都有不同的字符串长度我尝试过padding但它没有正确对齐这是我到目前为止

  Structure studentDetails

        Dim FirstName As String
        Dim SurName As String
        Dim marks As Integer
    End Structure

     Private Sub btnDetails_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDetails.Click

     Dim sd(3) as studentDetails 
     sd(0).FirstName = "Tom"
     sd(0).SurName = "jay"
     sd(0).marks = "0"

     sd(1).FirstName = "steven"
     sd(1).SurName = "crab"
     sd(1).marks = "20"

     sd(2).FirstName = "jack"
     sd(2).SurName = "thisislonglastname"
     sd(2).marks = "60"


     For I To 3

     label1.text =  label1.text & sd(I).FirstName.padright(10) & sd(I).SurName.padright(10) & sd(I).marks.ToString().padright(10) & Vbnewline

     Next
     End Sub

但我的输出格式不正确我希望它像这样显示

 Tom     jay                      0
 Steven  crab                     20
 jack    thisislonglastname       60

我该怎么做

1 个答案:

答案 0 :(得分:0)

使用DataGridView(显然你在Winforms中) 将值放在不同的列中,删除列和行之间的边框 - 您将获得想要实现的相同视图。

Private Sub btnDetails_Click(ByVal sender As System.Object, 
                             ByVal e As System.EventArgs) Handles btnDetails.Click

    Dim sd(3) As studentDetails 

    'Fill array with students values

    yourDataGridView.AutoGenerateColumns = True; 'This is true by default
    yourDataGridView.ColumnHeadersVisible = False
    yourDataGridView.RowHeadersVisible = False
    yourDataGridView.CellBorderStyle = DataGridViewCellBorderStyle.None

    yourDataGridView.DataSource = sd
End Sub

确保yourDataGridView.AutoGenerateColumns设置为True,并根据班级属性自动生成列