Gyula Soós
Gyula Soós

Reputation: 487

Texboxes Values in specified order from tabcontrol VB

I have a vb project of an event ticket selling stuff, and at the end I need to save a text file for each purchase.

I have tabbed controls, and at the very end, all the data that needs to go in the text file (event and customer) are in one tab.

I have this code, that will read the text from each textbox, and for now for testing purposes it throws a message box with the value. It is working, the only thing is, that it displays the values in an odd order and I don't know how to have them read in the required order. (also it wouldn't hurt, if I could add the labels before the textbox.text but I'm not that greedy :) )

 For Each GenericControl In TabPurchaseTickets.Controls
            If TypeOf GenericControl Is System.Windows.Forms.TextBox Then
                Dim tb As TextBox = DirectCast(GenericControl, TextBox)
                MsgBox(tb.Text)
            End If
        Next

Upvotes: 0

Views: 32

Answers (2)

Gyula Soós
Gyula Soós

Reputation: 487

It is a really ugly solution, but it works. Unfortunately in this project this is plenty! :D

Thanks for the tag idea, it's nice to find out that there is another field where I can "store" text in a textbox.

anyway, here is the bodge code *note the msgbox is just for testing the output, this will be modified to save in a textfile later:

Dim labels(15) As String Dim fields(15) As String

    For Each GenericControl In TabPurchaseTickets.Controls.OfType(Of TextBox)()
        Dim tb As TextBox = DirectCast(GenericControl, TextBox)
        fields(tb.TabIndex) = tb.Text
        labels(tb.TabIndex) = tb.Tag

    Next
    For i As Integer = 0 To 15
        MsgBox(labels(i) & ": " & fields(i))
    Next

Upvotes: 0

Steve
Steve

Reputation: 216323

You need to have something that helps you identify these textboxes and their values before writing them on a file.
Relying on the order of the textboxes on the tabcontrol will be a great mistake in future revision of your application. If you need to change that order older file will cause a 'versioning' problem.

You could define the Tag property at design time for each textbox with a value that helps you identify them and write your file with the tag value, a separator and the textbox value

  Dim sb = new StringBuilder()
  For Each GenericControl In TabPurchaseTickets.Controls.OfType(Of TextBox)
     Dim tb As TextBox = DirectCast(GenericControl, TextBox)
     sb.AppendFormat("{0};{1}", tb.Tag, tb.Text)
     sb.AppendLine()
 Next

And now write the StringBuilder.ToString to your textfile, you will end up with something like this

Name;John
Surname;McInroe
Sport;Tennis
....

In this way you could change the order of your textboxes as you like becase every value is associated to the Tag property and you could easily reload it.

Of course this is just an example and I suggest you to investigate the use of a proper database system instead of a simple file.

Upvotes: 1

Related Questions