locked
VB.NET change number in 2D Array with "Nothing" RRS feed

  • Question

  • Hello,

    At the moment I'm building a number generator program in VB.NET.

    I have a 2D array (with two rows and five columns) with 10 numbers in a textbox (Array.Text):

    Row 1: 4, 7, 8, 2, 1

    Row 2: 3, 9, 5, 7, 10

    A number generator is guessing if the generated number is in the textbox (Array.Text). If the number is in the textbox (Array.Text). I want that the matching number will be erased in my textbox. 

    My code:

    Generator = Random.Next(0, 100)
    
    If Array.Text.Contains(Generator) Then
    Array.Text = Array.Text.Replace(Generator, Nothing & ",")
    End If

    The number is replaced with a empty line, but my lay-out is messy after the replacement. For example when number 4 is generated:

    Row 1:, 7, 8, 2, 1

    Row 2: 3, 9, 5, 7, 10

    My question: Is it possible to maintain the original lay-out after a replacement?

    Thanks in advance.

    Saturday, May 2, 2020 5:51 PM

Answers


  • The number is replaced with a empty line, but my lay-out is messy after the replacement. For example when number 4 is generated:

    Row 1:, 7, 8, 2, 1

    Row 2: 3, 9, 5, 7, 10

    My question: Is it possible to maintain the original lay-out after a replacement?


    You aen't going to get perfect alignment of the numbers in the two rows
    because different characters will have different widths in most fonts.
    Only using a fixed-width font will allow getting equal widths for all
    characters.

    Also a 10 will have a different width than a single digit number.

    Can you use one or more space characters instead of Nothing to at least
    occupy some visible area? Or use tab characters to get each number to
    always start at the same location?

    - Wayne

    • Marked as answer by Jonathan.12 Saturday, May 2, 2020 9:32 PM
    Saturday, May 2, 2020 6:47 PM
  • Thank you very much for the response. I will try VbTab.

    There is no other option to 'erase' my numbers in the textbox? 


    Instead of using tabs, change the font used with the textbox to Lucidia Console.
    Then characters including a space will have the same width.

    - Wayne

    • Marked as answer by Jonathan.12 Saturday, May 2, 2020 9:32 PM
    Saturday, May 2, 2020 7:39 PM

  • There is no other option to 'erase' my numbers in the textbox? 


    To illustrate some of the formatting options you can use, try this example.
    Create a project with a button and a multi-line textbox. Set the font for the
    textbox to Lucidia Console in the designer.

    Public Class Form1
        Dim a(,) As Integer = {{1, 2, 3, 4, 5}, {6, 10, 8, 9, 7}}
        Dim str As String = ""
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            For n As Integer = 0 To 1
                For m As Integer = 0 To 4
                    str &= String.Format("{0,2:##}", a(n, m)) & ","
                Next m
                str = str.TrimEnd(",".ToCharArray)
                str &= vbCrLf
            Next n
            TextBox1.Text = str
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            str = str.Replace(" 4", "  ")
            str = str.Replace("10", "  ")
            TextBox1.Text = str
        End Sub
    End Class
    
    

    Output before button press:

    Output after button press:

    - Wayne

    • Marked as answer by Jonathan.12 Saturday, May 2, 2020 9:33 PM
    Saturday, May 2, 2020 8:35 PM

All replies


  • The number is replaced with a empty line, but my lay-out is messy after the replacement. For example when number 4 is generated:

    Row 1:, 7, 8, 2, 1

    Row 2: 3, 9, 5, 7, 10

    My question: Is it possible to maintain the original lay-out after a replacement?


    You aen't going to get perfect alignment of the numbers in the two rows
    because different characters will have different widths in most fonts.
    Only using a fixed-width font will allow getting equal widths for all
    characters.

    Also a 10 will have a different width than a single digit number.

    Can you use one or more space characters instead of Nothing to at least
    occupy some visible area? Or use tab characters to get each number to
    always start at the same location?

    - Wayne

    • Marked as answer by Jonathan.12 Saturday, May 2, 2020 9:32 PM
    Saturday, May 2, 2020 6:47 PM
  • Thank you very much for the response. I will try VbTab.

    There is no other option to 'erase' my numbers in the textbox? 


    Saturday, May 2, 2020 7:04 PM
  • Thank you very much for the response. I will try VbTab.

    There is no other option to 'erase' my numbers in the textbox? 


    Instead of using tabs, change the font used with the textbox to Lucidia Console.
    Then characters including a space will have the same width.

    - Wayne

    • Marked as answer by Jonathan.12 Saturday, May 2, 2020 9:32 PM
    Saturday, May 2, 2020 7:39 PM

  • There is no other option to 'erase' my numbers in the textbox? 


    To illustrate some of the formatting options you can use, try this example.
    Create a project with a button and a multi-line textbox. Set the font for the
    textbox to Lucidia Console in the designer.

    Public Class Form1
        Dim a(,) As Integer = {{1, 2, 3, 4, 5}, {6, 10, 8, 9, 7}}
        Dim str As String = ""
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            For n As Integer = 0 To 1
                For m As Integer = 0 To 4
                    str &= String.Format("{0,2:##}", a(n, m)) & ","
                Next m
                str = str.TrimEnd(",".ToCharArray)
                str &= vbCrLf
            Next n
            TextBox1.Text = str
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            str = str.Replace(" 4", "  ")
            str = str.Replace("10", "  ")
            TextBox1.Text = str
        End Sub
    End Class
    
    

    Output before button press:

    Output after button press:

    - Wayne

    • Marked as answer by Jonathan.12 Saturday, May 2, 2020 9:33 PM
    Saturday, May 2, 2020 8:35 PM
  • Thank you very much, it is working.
    Saturday, May 2, 2020 9:33 PM