I've searched through the forum, and I haven't been able to locate any code that is similar that I could modify, or find a thread close to what I'm trying to do, so any suggestions would be appreciated. (I'll get a book when I can get some time off)
I have a form with ten empty "MaskedTextBoxes".
I would like to go through them starting from "MaskedTextBox1" through to MaskedTextBox10" in that order, find the first empty one, and place the value I've selected in "DateTimePicker1" into it.
I'm new to VisualBasic 2005 Express
Mike C.

Find the first empty MaskedTextBox...
oneniner
// I'm new to VisualBasic 2005 Express
Depending on how new you are, it's likely that my core advice would be the advice I give 20 times a day. If you want to learn to code, you should learn the language before any frameworks, including winforms.
However. Your form has a Controls collection. You can use foreach to step through it, and you can check if a control is a maskedtextbox, and if it is, you can cast it to that type, and check it's text property.
Alternatively, you might prefer to build a list containing all your maskedtextboxes, which you could iterate through more simply. Or ( worst case, disgusting code scenario ), just check them one by one.
andy0203
William !
One more thing if I may.
I written some code to store the values to a .txt file 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) How can I make them return to where they came from
Here is the code I'm using:
Private
Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load TryFileOpen(1,
"FSO2.txt", OpenMode.Input) Dim Datepick2 As Windows.Forms.Control For Each Datepick2 In Me.Controls If TypeOf Datepick2 Is MaskedTextBox ThenFileSystem.Input(1, Datepick2.Text)
End If NextFileClose(1)
Catch ex As Exception FileClose(1) End Try End SubDuckPuppy
I didn't see your reply before I wrote my other posts to you.
Cheers,
--William
gamemaker9
Below is the 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
dpogo
Mastering Visual Basic .NET by Evangelos Petroutsos (published by Sybex)
and...
Visual Basic 2005: Programmer's Reference by Rod Stephens (published by Wrox)
Another good one is (it's a little less in-depth)...
Beginning Visual Basic 2005 by Thearon Willis, Bryan Newsome (also published by wrox)
I got them at BooksAMillion/Barnes&Noble. If you have trouble getting out, I have inserted links to amazon.com...you could get them online and have them delivered to your house.
You can get by on only one of those books. They all provide fairly in-depth coverage and similar information. So, you can spend just $50.00 instead of $200.00.
--William
Kyle Fender
William !
Thankyou.Thankyou,Thankyou...Did I say Thankyou
You are one steely eye'd master of code prestidigitation!
You are the best
(Grovel,Grovel,Grovel!)
P.S. I'll get a few good books!
Mike C.
TheReverend
- first check the control's name. If its a MaskedTextBox, then...
- ...check the Text property. If its Text property is empty, you've found it
however, you must be sure that the MaskedTextBoxes were placed on the form in numerical order for the following code to work.I have provided the whole class code as it was short and I had to write it anyway to test it.
Public Class Form1
Sub FindFirstEmptyMaskedTextBox(ByVal desiredText As String)
Dim ControlNameBase As String = "MaskedTextBox"
'let's set up a while loop
Dim foundIt As Boolean = False
Dim MaskedTextBoxCounter As Integer = 1
While Not foundIt
Dim ControlName As String
ControlName = ControlNameBase & MaskedTextBoxCounter
'let's loop through all the controls on this form
'remember collections are usually zero based
For ControlCounter As Integer = 0 To Me.Controls.Count - 1
If Me.Controls.Item(ControlCounter).Name = ControlName Then
'we found a MaskedTextBox control
'let's test for empty string
If Me.Controls.Item(ControlCounter).Text = String.Empty Then
'we found the first empty one
'let's supply desired text
Me.Controls.Item(ControlCounter).Text = desiredText
'and let's exit the while loop
foundIt = True
End If
End If
Next ControlCounter
MaskedTextBoxCounter += 1
End While
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'I put a button on the form to run the subroutine above
Me.FindFirstEmptyMaskedTextBox("hello")
End Sub
End Class
cheers,
--william