需要逻辑在列表框中分配空间

时间:2014-08-01 07:11:10

标签: vb.net listbox

我正在使用VB.Net开发一个数字游戏。因为我需要在列表框中生成以下模式:

           0
         1 0  1
       2 1 0 1 2
     3 2 1 0 1 2 3
   ..................
10 9 ... 1 0 1...... 9 10 

我使用以下代码生成了模式:

 Private Sub Generate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Generate.Click
        Dim n As Integer = CInt(TextBox1.Text)
        Dim s As String = ""
        For i As Integer = 0 To n
            s = ""
            For j As Integer = i To 1 Step -1
                s = s + CStr(j) & " "
            Next
            For j = 0 To i
                s = s + CStr(j) & " "
            Next
            ListBox1.Items.Add(s)
        Next
  End Sub

但它不会根据需要提供格式化的输出。以下是我的代码的输出:

output of the above code

任何人都可以帮我正确格式化吗?

4 个答案:

答案 0 :(得分:3)

我认为这比我之前的要好得多:

Dim n As Integer = CInt(TextBox1.Text)
Dim numbers = Enumerable.Range(-n,n*2+1) _
    .Select(Function(x) Math.Abs(x))
Dim results = Enumerable.Range(0,n+1) _
    .Select(Function(x) String.Join(" ",numbers.Where(Function(i) i<=x).ToArray()))
Dim maxWidth = results _
    .Select(Function(x) x.length).Max + 2
For Each s As String in results.Select(Function(x,i) new string(" ",((maxWidth - x.length - i.ToString().Length) / 2)) + x)
    ListBox1.Items.Add(s)
Next

enter image description here

答案 1 :(得分:0)

我做了以下事情。 不完整所以有评论的人请发帖。它接近你想要的东西,但是当输入变大时会变得很难看。

Dim n As Integer = CInt(TextBox1.Text)
Dim s As String = ""
dim spaces as integer
For i As Integer = 0 To n
    s = ""
    For j As Integer = i To 1 Step -1
        s = s + CStr(j) & " "
    Next
    For j = 0 To i
        s = s + CStr(j) & " "
    Next
    spaces = (n)*2 - (s.length - 1 - (n-i))/2
    dim addedspaces = new string(" ",if(spaces<0,0,spaces))
    s = addedspaces + s
    ListBox1.Items.Add(s)
Next

我的想法是试图弄清楚可能字符串的最大长度是多少,然后在每次迭代之前在空格中加一半长度减去每次迭代中字符串长度的一半。

我的逻辑存在缺陷所以我必须处理如果spaces为负面会发生什么。

这条线是你(或其他任何人)试图调整的:

spaces = (n)*2 - (s.length - 1 - (n-i))/2

我还应该提一下,我在LINQPad而不是实际的listbox中尝试了这一点,所以我不会在列表框中知道结果。

答案 2 :(得分:0)

我认为最好的选择是将所有元素存储在一个数组中,以便稍后检查所需的格式,例如:

Private Sub Generate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Generate.Click
    '...

    Dim lines(n-1) As List (Of String)
    Dim numLen As Integer = 0

    ' For n...
        lines(n) = New List(Of String)()

        ' Fors
        lines(n).Add(j.ToString())
        If j.ToString().Length > numLen Then numLen = j.ToString().Length

然后你存储了所有的数字和最长的数字,然后你必须在ListBox上打印它:

' For n...
    Dim iSpaces As Integer = CInt(lines(lines.Length -1).Count * (numLen + 1) / 2)

    Dim sLine As String = New String(" "c, iSpaces)
    sLine &= String.Join(New String(" "c, numLen), lines(n).ToArray())

    ListBox1.Add(sLine)        

答案 3 :(得分:0)

使这项工作正常运行的最简单方法可能是将所有字符串累积到一个数组中,然后获取最后一行的宽度,因为这将是最长的,并且填充先前创建的行。 然后您将这些添加到列表框中。在伪代码中(我不会说Visual BASIC):

lines = array of n strings
for i = 0 to (n-1)
  lines[i] = createLine(i)
end

maxWidth = width(last entry of lines)

for i = 0 to (n-1)
  paddingWidth = (maxWidth - width(lines[i])) / 2
  padding = string with paddingWidth spaces
  lines[i] = padding + lines[i]
  // Or even: lines[i] = padding + lines[i]
  // That way all lines should have the same length
end

另外,正如评论中指出的那样,您可能希望确保使用等宽字体。