Question Text box question... grrrrr~

black zero

Well-known member
Joined
Jan 15, 2009
Messages
53
Programming Experience
Beginner
I have a 3 textboxes.

If I write anything (example: char '1') on the first box, the char would appear on the third box.

Then if I add char '2' on second textbox, the first char would appear at the first line on the third box, and char '2' would appear on the second line, below that '1'.

I am able to do it by using a function like this:

VB.NET:
textbox3.text = textbox1.text & vbCrLf _
                       & textbox2.text

But the problem is, if the first box doesn't have anything written inside it, but the second does have char ex: '2', the third box would display '2' on the second line. I just want to have it displayed on the first line instead two...

In this example, there are only 3 boxes. But in fact, I have 20 or so textboxes :confused:

Help me fix my code. :(
 
You could create a function that takes a textbox and a string as param and
- adds the string to the textbox if textbox.text is empty
- adds a linefeed and the string to the textbox if the string is not empty and the textbox is not empty
- does nothing if the string is empty
 
For example:
VB.NET:
Private Sub WriteLineToTextbox(ByVal tb As TextBox, ByVal s As String, Optional ByVal AddLineIfStringIsEmpty As Boolean = False)
        If String.IsNullOrEmpty(s) Then
            If AddLineIfStringIsEmpty Then
                tb.Text &= Environment.NewLine
            End If
        Else
            If String.IsNullOrEmpty(tb.Text) Then
                tb.Text &= s
            Else
                tb.Text &= Environment.NewLine & s
            End If
        End If
    End Sub

Private Sub TextBox_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Validated, TextBox2.Validated
        TextBox3.Text = String.Empty
        WriteLineToTextbox(TextBox3, TextBox1.Text)
        WriteLineToTextbox(TextBox3, TextBox2.Text)
    End Sub
Having more texboxes it would make sense to cycle through them programmatically of course instead of adding one line per textbox to add to the resultbox.
 
T.T You got me.... I don't know how to use those codes you provided. I tried to insert it on my code, but.... none's working?
 
Do you know how to use a If statement? Start with an empty string. For each TextBox you check whether it contains any text and only IF it does then you add that text and a line break. If there's no text then you don't add anything.
 
How about?

mm... well, this should help to understand the picoflop's code, as i have inserted 'Comments :p

Okay, so

VB.NET:
Private Sub WriteLineToTextbox(ByVal tb As TextBox, ByVal s As String, Optional ByVal AddLineIfStringIsEmpty As Boolean = False)
'This code here is just the opening statement for the code. It is a Private Sub, and its function is to write a line, to the textbox.   

If String.IsNullOrEmpty(s) Then
            If AddLineIfStringIsEmpty Then
                tb.Text &= Environment.NewLine
            End If
'This is saying that is one of the textbox's are empty, then the text of one of the textboxs adds a new line.
        Else
            If String.IsNullOrEmpty(tb.Text) Then
                tb.Text &= s
            Else
                tb.Text &= Environment.NewLine & s
            End If
        End If
'If the above function is not in progresss, then move the text of a textbox into another textbox, and add a new line after it.
    End Sub 'Ending the Sub

Private Sub TextBox_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Validated, TextBox2.Validated
'This Sub is validating the textbox text for textbox 1,2 and 3.
        
TextBox3.Text = String.Empty
        WriteLineToTextbox(TextBox3, TextBox1.Text)
        WriteLineToTextbox(TextBox3, TextBox2.Text)
'These three lines of text are moving the text of Textbox 3 to text box 1, that of text box 3 to text box 2.
    End Sub

The problem with picoflop's code was that that i think you are having with this code is the naming of the Textbox, because in the first sub, the textbox name is tb, but in the second sub, the textbox names are TextBox1, Textbox2, and TextBox3.

If this is the case, this should help:


VB.NET:
Private Sub WriteLineToTextbox(ByVal tb As TextBox, ByVal s As String, Optional ByVal AddLineIfStringIsEmpty As Boolean = False)
        If String.IsNullOrEmpty(s) Then
            If AddLineIfStringIsEmpty Then
                TextBox1.Text &= Environment.NewLine
            End If
        Else
            If String.IsNullOrEmpty(tb.Text) Then
                TextBox2.Text &= s
            Else
                TextBox3.Text &= Environment.NewLine & s
            End If
        End If
    End Sub

Private Sub TextBox_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Validated, TextBox2.Validated
        TextBox3.Text = String.Empty
        WriteLineToTextbox(TextBox3, TextBox1.Text)
        WriteLineToTextbox(TextBox3, TextBox2.Text)
    End Sub

All i changed there was the Name of the text box.

Hope this Helped...
 
I'l do my homework for this now. Thank you for your comment and help, jm and haxaro and pic.

I'll try to make that three boxes working, then starting to add one by one until I get all 20 or so boxes working~

In the meantime, thank you again for your help. I'll post here again when I encounter .... other problem. :)
 
Back
Top