Ok my title is probably very vague and unclear.
Kind of hard to sumerise in the title.

My form has a bunch of textboxes.
The content is supposed to be saved with a button.
BUT not all the textboxes will have content in them.
So lets say i added content to the 1st textbox/selected textbox.
I want it to save just that.
All textboxes have a different name of course.
So basically a generic name for all textboxes on the form.
But without clearing content which is already present in other textboxes.

Hope i am making somewhat sense here.
Have a feeling i dont.

Recommended Answers

All 6 Replies

Try this:

Private Sub btnSaveText_Click(sender As System.Object, e As System.EventArgs) Handles btnSaveText.Click
    ' assuming the form is the parent container of the textboxes
    ' otherwise use me.<container>.controls
    For Each ctrl As control In me.controls
        If TypeOf ctrl Is Textbox Then
        ' alternative check for ctrl.Name here
            If (Not(ctrl.text is nothing)) Then
                SaveText(ctrl.Text.ToString)
            End If
        ' end ctrl.Name check
        End If
    Next
End Sub

Sub SaveText(stringIn as String)
    ' TODO: your code here to save text
    Throw New NotImplementedException
End Sub

I don't have access to VB while my computer is in the shop but let me make a suggestion (you'll have to verify the syntax). You can check all of the textboxes on the form by

For Each tbx As TextBox In Me.Controls.OfType(Of TextBox)()
    If tbx.Text <> "" Then
        'process textbox with content
    End If
Next

If some of the textboxes do not need to be checked then you can either put all the ones to check in a groupbox and just check within that group, or you could place some value in the Tag property of each textbox to check and ignore the ones without that value.

So lets say i added content to the 1st textbox/selected textbox.
I want it to save just that.

You can't check only the selected textbox because by clicking the button you deselect the textbox.

commented: nice... much cleaner than mine +0

and that's why he is the moderator

I used to do it the hard way too until someone else here did to me what I did to you ;D

Hearty LOL!

I did write that without the IDE though :)

Alright another less learned.
Works the way i needed it.

Thanks guys.

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.