How to create spaces in a textbox?

67 views Asked by At

I have a vb net program to take a binary value in the image. This syntax produces

111110 

on textbox3.text. I want no spaces at textbox3.text

1 1 1 1 1 0

        Dim x, y As Integer
        Dim gambar As New Bitmap(PictureBox7.Image)
        Dim gray, vektor, biner As Integer

        'biner
        'With gambar
        For x = 0 To gambar.Width - 1
            For y = 0 To gambar.Height - 1
                gray = (CInt(gambar.GetPixel(x, y).R) + _
                             gambar.GetPixel(x, y).G + _
                             gambar.GetPixel(x, y).B) / 3
                gambar.SetPixel(x, y, Color.FromArgb(gray, gray, gray))

                If gray > 128 Then
                    biner = 255
                Else
                    biner = 0
                End If
                gambar.SetPixel(x, y, Color.FromArgb(biner, biner, biner))
                'ttup proses grayscale
                If (biner = 0) Then
                    vektor = 0
                End If
                If (biner = 255) Then
                    vektor = 1
                End If
                'TextBox2.Text = pixel_putihblkg2
                TextBox3.SelectedText = vektor.ToString
            Next y
            PictureBox7.Refresh()
            PictureBox7.Image = gambar
        Next x
        PictureBox7.SizeMode = PictureBoxSizeMode.StretchImage

    Catch exc As Exception
    End Try

IMAGE

1

There are 1 answers

0
Justin Ryan On

Your request is unclear, but if I interpret your example correctly, you want to insert a space between each digit in your string before assigning it to the TextBox. You can do this with a modified loop and String.Insert.

Dim spacedString As String = vektor.ToString
For i As Integer = 0 To (spacedString.Length * 2) Step 2
    spacedString = spacedString.Insert(i + 1, " ")
Next
TextBox3.Text = spacedString

Here I'm copying vektor.ToString into a new variable, which will then be modified. The For loop increments from zero to twice the length of the unmodified string (because the final string will be twice as long), and steps by two (to insert after each character plus space, or two positions). For each iteration, use .Insert to insert a space. Finally, assign the modified string to the TextBox.

This will result in an extra space at the end of the string. If this is a problem, you can use String.TrimEnd to remove it.

TextBox3.Text = spacedString.TrimEnd(" "c)

Update: I failed to notice that you seem to be inserting one digit at a time to the TextBox. In this case you can simply add the spaces directly in code.

TextBox3.Text &= vektor.ToString & " "