Making "MaskedTextBox" values return in order...

I written some code (with some help..alright, alot of help) to store the values to a .txt file(s) so that when I re-open the form, the values will return (Hopfully to where they came from), but unfortunately they don't.

What happens is, they start back filling from the bottom of the form (Highest "MaskedTextBox" number to the lowest)

P.S. I'm already using this solution for "TextBoxes" Using FileOpen(1, "FSO.txt", OpenMode.Input) and it works fine (Confused)

I'm saving the values for "TextBoxes" to one file( FSO.txt) and the values for the "MaskedTextBoxes" to another file(FSO2.txt).

I need them to return to their former boxes, Here is the code I'm using for the return:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Try

FileOpen(1, "FSO2.txt", OpenMode.Input)

Dim Datepick2 As Windows.Forms.Control

For Each Datepick2 In Me.Controls

If TypeOf Datepick2 Is MaskedTextBox Then

FileSystem.Input(1, Datepick2.Text)

End If

Next

FileClose(1)

Catch ex As Exception

FileClose(1)

End Try

End Sub


Answer this question

Making "MaskedTextBox" values return in order...

  • Ryan Lowdermilk

    Thanx for all your hard work William, believe me, it is very much appreciated.

    It seems to be working fine!

    Again, much thanx

    Mikeee


  • Mikener

    This is the code that you need (I think).

    Be sure to get the corrected CollectMaskedTextBoxes subroutine, which I provide in the last post.  The sub will not work in all cases as coded in this post.

    Okay, here's all the code for what you need (I think)...

    The main methods are:

    1. CollectMaskedTextBoxes()
    2. FindFirstEmptyMtb()
    3. SaveMtbValues()
    4. RestoreMtbValues()

    Below is the code, you may copy-n-paste....

    Public Class Form1

        Private MyMaskedTextBoxes As New List(Of MaskedTextBox)
        Private MyMtbFile as String = "C:\MyMtbValues.txt"


        Sub New()

            ' This call is required by the Windows Form Designer.
            InitializeComponent()

            ' Add any initialization after the InitializeComponent() call.
            Me.CollectMaskedTextBoxes()
        End Sub

        Private Sub CollectMaskedTextBoxes()
            'let's assemble a collection of Mtbs in numerical order

            'First, let's loop through all of the form's controls
            'Control indexes are actually in reverse of what you would think...

            'so, we will loop through them backwards
            For i As Integer = (Me.Controls.Count - 1) to 0 Step -1
                'let's check each control's Name property to see if
                'the current control is a MaskedTextBox
                Dim pos As Integer
                pos = Me.Controls.Item(i).Name.IndexOf("MaskedTextBox")

                If pos <> -1 Then
                    'hey, we found a MaskedTextBox control
                    'but right now, the computer considers it only a control in general...
                    'so, let's convert this general control into a MaskedTextBox control

                    Dim mtb As MaskedTextBox
                    mtb = CType(Me.Controls.Item(i), MaskedTextBox)

                    'finally, let's add this control -- mtb -- to our List of MaskedTextBoxes
                    Me.MyMaskedTextBoxes.Add(mtb)
                End If
            Next i

       End Sub

        Private Sub FindFirstEmptyMtb(ByVal someText As String)
            'let's loop through all the Mtbs in the Mtb collection
            For i As Integer = 0 To Me.MyMaskedTextBoxes.Count - 1
                'let's check Text property of each current Mtb
                If Me.MyMaskedTextBoxes.Item(i).Text = String.Empty Then
                    'hey, we found the first empty Mtb...
                    'so, let's supply the text
                    Me.MyMaskedTextBoxes.Item(i).Text = someText
                    'we're done here, so lets exit the for-loop
                    Exit For
                End If
            Next
        End Sub

        Private Sub SaveMtbValues()
            'prepare a file stream to write text to the file
            Dim fs As New System.IO.FileStream(Me.MyMtbFile, IO.FileMode.Create)
            Dim sw As New System.IO.StreamWriter(fs)

            'loop through the MTB collection
            For i As Integer = 0 To Me.MyMaskedTextBoxes.Count - 1
                'for each current MTB's, write the Text value to the file
                sw.WriteLine(Me.MyMaskedTextBoxes.Item(i).Text)
            Next

            'close and dispose of the stream objects
            sw.Close()
            fs.Close()

            sw.Dispose()
            fs.Dispose()

            'alert user that information has been saved...
            'delete or comment out this code if you do not
            'wish to inform the user of this process
            MsgBox("Mtb values have been saved.")

        End Sub

        Private Sub RestoreMtbValues()
            'prepare file stream
            Dim fs As System.IO.FileStream
            Dim sw As System.IO.StreamReader

            'try...catch...we might not find the file
            Try
                fs = New System.IO.FileStream(Me.MyMtbFile, IO.FileMode.Open)
            Catch ex As Exception
                'probably best to just leave the MTBs blank if an exception
                'is thrown, so we'll just exit the whole subroutine if we get an exception.
                Exit Sub
            End Try

            'if we successfully opened the filestream, we can create the streamreader
            sw = New System.IO.StreamReader(fs)

            'now, we'll loop through the MTB collection
            For i As Integer = 0 To Me.MyMaskedTextBoxes.Count - 1
                'for each current MTB, we will read a line of text
                Me.MyMaskedTextBoxes.Item(i).Text = sw.ReadLine()
            Next

            'we're done, so we'll close and dispose the stream objects
            sw.Close()
            fs.Close()

            sw.Dispose()
            fs.Dispose()

        End Sub

        'the next three subs are events for three buttons I had on a test form when I
        'tested the above subroutines
        'most likely, you will have different events call the subroutines
        'so feel free to remove/edit these last three event subs.
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Me.FindFirstEmptyMtb("hello")
        End Sub

        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Me.SaveMtbValues()
        End Sub

        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            Me.RestoreMtbValues()
        End Sub
    End Class

    Cheers,
    --William



  • Remus Radu

    Ignore this post.  I provide a more complete code solution in a later post.

    --william

    Hi Mikeee,

    For the easiest solution to this problem it is best if you use the second technique I gave you on the other thread.  You really should sift the MaskedTextBoxes out of the other controls and collect them into a numerically ordered collection -- i.e,  a List(of MaskedTextBox).

    Below is the code to do that.  It has two basic subroutines:  (1) CollectMaskedTextBoxes and (2) FindFirstEmptyMtb.  The CollectMaskedTextBoxes subroutine gathers the MTBs into a List called MyMaskedTextBoxes.  The FindFirstEmptyMtb finds the first empty MTB and places the desired text in it.

    'begin code
    Public Class Form1

        Private MyMaskedTextBoxes As New List(Of MaskedTextBox)

        Sub New()

            ' This call is required by the Windows Form Designer.
            InitializeComponent()

            ' Add any initialization after the InitializeComponent() call.
            Me.CollectMaskedTextBoxes()

        End Sub

        Private Sub CollectMaskedTextBoxes()
            'we will assemble a collection of Mtbs in numerical order

            Dim myStack As New Stack(Of MaskedTextBox)

            'let's loop through all controls
            'MaskedTextBoxes will be encountered from Highest to Lowest
            'so we will put them on a stack as we find them and
            'then take them from the stack and put them in the List
            For i As Integer = 0 To Me.Controls.Count - 1
                'let's check each control's Name property to see if
                'the current control is a MaskedTextBox
                Dim pos As Integer
                pos = Me.Controls.Item(i).Name.IndexOf("MaskedTextBox")

                If pos <> -1 Then
                    'we found a MaskedTextBox control

                    'let's convert the general control to a MaskedTextBox
                    Dim mtb As MaskedTextBox
                    mtb = CType(Me.Controls.Item(i), MaskedTextBox)

                    'let's add this control to our STACK of MaskedTextBoxes
                    myStack.Push(mtb)

                End If
            Next i

            'let's take the Mtbs from the stack and put them in the List
            For i As Integer = 0 To myStack.Count - 1
                Me.MyMaskedTextBoxes.Add(myStack.Pop())
            Next

        End Sub

        Private Sub FindFirstEmptyMtb(ByVal someText As String)
            'loop through all the Mtbs in the Mtb collection
            For i As Integer = 0 To Me.MyMaskedTextBoxes.Count - 1
                'check Text property of current Mtb
                If Me.MyMaskedTextBoxes.Item(i).Text = String.Empty Then
                    'we found the first empty Mtb

                    'let's supply the text
                    Me.MyMaskedTextBoxes.Item(i).Text = someText

                    'and exit the for loop
                    Exit For
                End If
            Next
        End Sub

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Me.FindFirstEmptyMtb("hello")
        End Sub
    End Class
    'end code

    I will make another post that will solve your problem, but my solution will not work unless you use the above code to solve the first problem of finding the first empty text box


  • vtortola

    Here is corrected CollectMaskedTextBoxes subroutine

    Sorry for all the blunders...

    but the CollectMaskedTextBoxes sub didn't guarantee that you got the MaskedTextBoxes in numerical order (it would be right in most cases...but not all)...plus I discovered a more elegant way of determining the type of the current control (using GetType().Name instead of whatever I did last time).

    here is the corrected sub routine:

     

    Private Sub CollectMaskedTextBoxes()

        'let's assemble a collection of Mtbs in numerical order

        'Step 1: let's set up a temporary list to hold the

        'unsorted Mtbs

        Dim tmpList As New List(Of MaskedTextBox)

        'Step 2: let's loop through all of the form's controls

        For i As Integer = 0 To (Me.Controls.Count - 1)

            'let's check each control's type to see if

            'the current control is a MaskedTextBox

            Dim currentControl As Control

            Dim currentControlType As String

            Dim MtbType As String = "MaskedTextBox"

            currentControl = Me.Controls.Item(i)

            currentControlType = currentControl.GetType().Name

            If currentControlType = MtbType Then

                'hey, we found a MaskedTextBox control

                'but right now, the computer considers it only a control in general...

                'so, let's convert this general control into a MaskedTextBox control

                Dim currentMtb As MaskedTextBox

                currentMtb = CType(currentControl, MaskedTextBox)

                'finally, let's add this control to our

                'temporary unsorted List of MaskedTextBoxes

                tmpList.Add(currentMtb)

            End If

        Next i

        'step 3: now let's construct the permanent sorted List

        'first we will need to figure out how many Mtbs there are

        'to give us our counting numbers

        Dim mtbTotal As Integer = tmpList.Count

        'now we will loop through the counting numbers

        For mtbCounter As Integer = 1 To mtbTotal

            'for each counting number, we will loop through all

            'the Mtbs in the unsorted list to find the match

            'first let's determine what we want to match

            'for instance, "MaskedTextBox3" or whatever...

            Dim mtbNameToMatch As String = "MaskedTextBox" & mtbCounter

            'next, let's do the search

            Dim mtb As MaskedTextBox

            For Each mtb In tmpList

                If mtb.Name = mtbNameToMatch Then

                     'lets add it to our permanent, sorted List

                      Me.MyMaskedTextBoxes.Add(mtb)

                End If

            Next mtb

        Next mtbCounter

    End Sub


  • Qa Dept

    Ignore this post, too...I edited my last post and fixed the problem I'm talking about here.

    --william

    one more thing...I did something really bad...I hardcoded the filename for the text file when making the filestreams...

    it's better to record the file name in a variable at the beginning of the class...

    example:
    Private MyMtbValuesFileName As String = "C:\MyMtbValues.txt"


    then later, in the filestreams you can refer to the variable instead of the actual path name.

    example:
    Dim fs As System.IO.FileStream
    fs = New System.IO.FileStream(Me.MyMtbValuesFileName, IO.FileMode.Create)


  • Making "MaskedTextBox" values return in order...