1,105,386 Community Members

Any way to address multiple textboxes with a button

Member Avatar
Sevyt
Junior Poster in Training
90 posts since Feb 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
john.knapp
Posting Whiz in Training
221 posts since Oct 2012
Reputation Points: 25 [?]
Q&As Helped to Solve: 22 [?]
Skill Endorsements: 4 [?]
 
0
 

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
Member Avatar
Reverend Jim
Noli mentula
5,394 posts since Aug 2010
Reputation Points: 746 [?]
Q&As Helped to Solve: 643 [?]
Skill Endorsements: 51 [?]
Moderator
Featured
 
1
 

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.

Member Avatar
john.knapp
Posting Whiz in Training
221 posts since Oct 2012
Reputation Points: 25 [?]
Q&As Helped to Solve: 22 [?]
Skill Endorsements: 4 [?]
 
0
 

and that's why he is the moderator

Member Avatar
Reverend Jim
Noli mentula
5,394 posts since Aug 2010
Reputation Points: 746 [?]
Q&As Helped to Solve: 643 [?]
Skill Endorsements: 51 [?]
Moderator
Featured
 
0
 

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

Member Avatar
john.knapp
Posting Whiz in Training
221 posts since Oct 2012
Reputation Points: 25 [?]
Q&As Helped to Solve: 22 [?]
Skill Endorsements: 4 [?]
 
0
 

Hearty LOL!

I did write that without the IDE though :)

Member Avatar
Sevyt
Junior Poster in Training
90 posts since Feb 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Thanks guys.

Question Answered as of 1 Year Ago by john.knapp and Reverend Jim
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article