Simple Question *WinForms VB.NET TABCONTROL Saving values from 2 textboxes and print


I have added a tab control to the windows form application.......... I have  2 Tabs Added lets say tab1 and tab2 and control name is tabcontrol
I added One Textbox in tab1 and another textbox in tab2 .........I have a common SAVE Button at the bottom........
In One shot I want to print the values entered in textbox1 and textbox2 when SAVE Button is clicked .......

For this I declared 2 variables    dim x as string, y as string at the top of the general declarations
I used SelectedIndex change of TabControl
Select case  Tabcontrol_selectedIndexchanged
case  0                             ie tab1 control clicked
if textbox1.text.trim <> "" then
 x = textbox1.text.trim
end if
case 1                               'Tab1 control clicked
if textbox2.text.trim <> "" then
y = textbox2.text.trim
end if
end select

Private Sub BtnSave_Click()
end sub

I am not able to achieve the desired results properly

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

both text boxes are accessible when you hit the save button. if the event for the save button click you can get the values from the textboxes. what is the purpose of the tab click even? why set values for x and y?

Private Sub BtnSave_Click()
end sub
Did you declare X and Y to be local to that Sub/Function, or did you declare them to be Private/Public?
if you declare x and y as a private in the from containing the tab control you will get the same result. i don't quite understand where you are declaring your variable when you say general declarations at the top. i am assuming this is the top of the form class.
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Try instead of:

Dim x as string
Dim y as string

Try this:

Private x as string
Private y as string
sainavya1215Author Commented:
But the problem is :

Lets say in the first tab there exists 3 textboxes and in second tab there are two textboxes

Each Tab has some validation logic .....Like for eg in the first tab if the user Enters data in first textbox and leaves to second tab and enters data in first textbox and if he clicks SAVE it should tell him that Data has not been filled entirely for both tab1 and tab2
Thats the condition. So i thought i should validate it both in SAVE click as well as tab clicks

Could you post your current code somewhere? Maybe then, I can better diagnose the source of the problem.
sainavya1215Author Commented:
ok got you.........If i can get answer to this question I am done

Lets say when the user presses tab1 i have 10 Textboxes.

The validation is :
All the textboxes have to be entered ...User cant enter one text box or two and leave others empty
he can leave all the textboxes empty which isfine.(IS THERE ANY EASY WAY TO CHECK THIS VALIDATION LOOPING THRU THE CONTROLS ON FORM)
All the texboxes are placed inside a groupBox
sainavya1215Author Commented:
For clear Understanding this is what I am looking for

I'm looking for a way do validation on 3 text boxes.

If all 3 text boxes are blank, then that is okay.

If one or two of the text boxes is filled in and the others are empty, then this is an error condition.

Ok, I think I know where you're going.

Code to place on the TabControl's change:

If Textbox1.Text = "" and Textbox2.Text = "" and Textbox3.Text = "" Then

     Exit Sub


     If Textbox1.Text = "" or Textbox2.Text = "" or Textbox3.Text = "" Then

MsgBox "You must complete all fields before continuing!"

     End If

End If

This will validate the three fields, making sure that all of them are filled (or all of them are blank).


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PS - You can replace the MsgBox line with the code that is to be run when validation has failed.
PS #2 - You can also place that code in your button's click event.
yes you can create a sub to validate the textbox on the form. for example:

Public Class YourForm

Private Function Validate_Form() as Boolean

'perform your validation here
if len(textbox1.text) = 0 orelse len(textbox2.text) = 0 orelse len(textbox3.text) then
     return false
     return true
end if

End Function

End Class

the textboxes is within the scope of the form and can be accessed in a sub or function within the form. there is no need to assign it to x or y. doing that just uses more memory allocation which is not necessary(not that its that big a performance hit). also i used len to check if empty string because string comparisons are another performance degradation.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.