hi there... i am very new (and i mean VERY NEW at visual basic) programming, lets just say this is my 5th hour (and counting) learning programming, i googled since hour 1, i even read almost all i can read while im installing visual studio 2010 coz i really want to learn this programming language coz somebody said that this is an easy language to learn coz of GUI.. so please be easy on me on this one

i got a program here with 10 textboxes and 3 buttons, and i want to save every text on that textboxes in a file (myfile.txt)..

i did that by using this commands

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Dim sb As New StringBuilder()
        Dim user As String = Environment.UserName
        sb.Append(TextBox1.Text)
        sb.Append(TextBox2.Text)
        sb.Append(TextBox3.Text & vbCrLf)
        sb.Append(TextBox4.Text & vbCrLf)
        sb.Append(TextBox5.Text & vbCrLf)
        File.WriteAllText("c:\account\myfile.txt", sb.ToString())
        MessageBox.Show("File Saved")

OR

Dim output As String = ""
        output = TextBox1.Text & Environment.NewLine & TextBox2.Text & Environment.NewLine & TextBox3.Text & Environment.NewLine
        My.Computer.FileSystem.WriteAllText("C:/account/myfile.txt", output, True)
        MessageBox.Show("File Saved")

all of that is working, i know everyone of them is doing the same darn thing but i wanted to learn every possible approach on this program

now what i dont know is how to save that text in a format..

1. i want the textbox1 to be only 6 digits, i already edited the maxlength property of the textbox to 6. now the problem is if i input 3 character e.g. "aaa" and i want the remaining length to be automatically filled with a "space"
so it will become "aaa " (with 3 spaces after the last "a")
2. then i want the textbox2 to do the same thing AFTER the textbox1 and with space also..

*darn english is not my first language*

can anyone help me please?

Dani AI

Generated

Brief summary and a cleaner approach for fixed-width saving (pads or trims each field to an exact length).

's loop idea and 's mention of PadRight point in the right direction. The original If that simply does TextBox1.Text += " " only ever adds one space, so it cannot fill up to a 6-character width. Padding is best done when building the record for the file (not by trying to force trailing spaces into the TextBox while typing).

A compact, maintainable pattern is to keep a parallel array of widths and walk the TextBoxes once, trimming or truncating and padding each field before appending to a StringBuilder. Example helper (adapted for clarity):

Function BuildFixedRecord(boxes As TextBox(), widths As Integer()) As String
    Dim sb As New System.Text.StringBuilder()
    Dim count = Math.Min(boxes.Length, widths.Length)
    For i As Integer = 0 To count - 1
        Dim s = If(boxes(i).Text, String.Empty).Trim()
        If s.Length > widths(i) Then s = s.Substring(0, widths(i))
        sb.Append(s.PadRight(widths(i)))
    Next
    Return sb.ToString()
End Function

Notes and practical tips:

  • Keep padding on write so the UI remains natural; MaxLength only limits input, it does not pad.
  • Ensure the target folder exists before saving (create it programmatically) and build paths with Path.Combine to avoid slash/backslash errors (as pointed out).
  • When verifying padding, open the saved file in a plain-text editor with a monospaced font or wrap fields in quotes temporarily so trailing spaces are visible.
  • If fixed-width is not strictly required downstream, consider a delimited format (CSV/JSON) which avoids fragile space-padding.

This keeps the code readable, avoids repeated MyStringN variables, and makes future field-width changes trivial.

Recommended Answers

All 5 Replies

aight, i did a little research again and this is the code of my button

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        TextBox1.SelectionStart = TextBox1.Text.Length
        If TextBox1.Text.Length < 6 Then
            TextBox1.Text += " "
            TextBox1.SelectionStart = TextBox1.Text.Length
        End If
        Dim n As String = Environment.NewLine
        My.Computer.FileSystem.WriteAllText("C:/account/myfile.txt", TextBox1.Text & TextBox2.Text & TextBox3.Text & n & TextBox4.Text & TextBox5.Text, False)
    End Sub

but the problem is its not autofilling the remaining spaces..
e.g. i typed in "aa" but in myfile.txt i can see that its "aa (with 1 space only)" how can i make it to autofill the remaining character length automatically?! e.g. "aa (4 spaces)"

Use a for loop:
Try this:
Dim i As Integer
If textbox1.Length < 6 Then
For i = textbox1.Length To 5
textbox1.Append("*")
Next
End If

The code says you loop through the deficiency of the length of the string.
Got it?
Oh, and change the "*" char to " " (space)

TextBox1.Text.PadRight(6)

will fill your string with spaces

guys thanks for the help, managed to build up a code using google :) i know its a dirty and bulky code but it works, can anyone gimme some tips or tweaks so that i can make this a lil bit lighter on the eyes.. i wanted to learn everything bout this visual basic :)

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Dim MyString1 As String = TextBox1.Text.PadRight(6)
        Dim MyString2 As String = TextBox2.Text.PadRight(3)
        Dim MyString3 As String = TextBox3.Text.PadRight(10)
        Dim MyString4 As String = TextBox4.Text.PadRight(6)
        Dim MyString5 As String = TextBox5.Text.PadRight(8)
        Dim MyString6 As String = TextBox6.Text.PadRight(20)
        Dim MyString7 As String = TextBox7.Text.PadRight(25)
        Dim MyString8 As String = TextBox8.Text.PadRight(3)
        Dim MyString9 As String = TextBox9.Text.PadRight(9)
        Dim MyString10 As String = TextBox10.Text.PadRight(35)
        Dim n As String = Environment.NewLine
        My.Computer.FileSystem.WriteAllText("C:/account/myfile.txt", MyString1 & MyString2 & MyString3 & n & MyString4 & MyString5 & MyString6 & MyString7 & MyString8 & MyString9 & MyString10, False)
    End Sub

This part for FileName "C:/account/myfile.txt", use \ instead of /.
Example: "C:\account\myfile.txt"

As for the rest, see if this helps.

Dim n As String = Environment.NewLine
        Dim MyString As String = TextBox1.Text.PadRight(6) '// add first TextBox text.
        '// add to the same string a new line and the following TextBoxes' text.
        MyString &= n & TextBox2.Text.PadRight(3)
        MyString &= n & TextBox3.Text.PadRight(10)
        MyString &= n & TextBox4.Text.PadRight(6)
        MyString &= n & TextBox5.Text.PadRight(8)
        MyString &= n & TextBox6.Text.PadRight(20)
        MyString &= n & TextBox7.Text.PadRight(25)
        MyString &= n & TextBox8.Text.PadRight(3)
        MyString &= n & TextBox9.Text.PadRight(9)
        MyString &= n & TextBox10.Text.PadRight(35)
        My.Computer.FileSystem.WriteAllText("C:\account\myfile.txt", MyString, False) '// write to file.

Btw, my first few hours of programming in vb.net, I was having trouble adding a TextBox to the Form. :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.