I have 10 textboxes called TextBox1 to 10 and I need to refer to them by their number. I used the code below and it worked fine. When I closed VS and reopened it later, I changed the filename of the list of words and nothing more. Now it throws an exception on all the DirectCast lines and I can't work out why. The error is a NullReferenceException - Reference to object not established as object. It suggests using New but I don't know how to do that to relate to what I want it to do.
The code is below
And the other place I used DirectCast
The only piece of code I changed when I came back to it was this. The old line is commented out (setting the filename)
The code is below
vb.net Code:
Private Sub btnMatchLetters_Click(sender As System.Object, e As System.EventArgs) Handles btnMatchLetters.Click Dim Letters(9) As String Dim WordLength As Integer = 10 'clear the listbox ready for a new list ListBox1.Items.Clear() 'work backwards until find first highlighted textbox which indicates length of word 'DirectCast here allows us to refer to each textbox by its number For CtrlPtr As Integer = 10 To 1 Step -1 If DirectCast(Me.Controls("TextBox" & CtrlPtr.ToString), TextBox).BackColor = Color.DarkCyan Then 'found length so get out WordLength = CtrlPtr Exit For End If Next 'copy the letters to an array so we can check them For CtrlPtr = 10 To 1 Step -1 Letters(CtrlPtr - 1) = DirectCast(Me.Controls("TextBox" & CtrlPtr.ToString), TextBox).Text().ToLower Next For Each word In Dict 'ProgressBar1.PerformStep() Dim MatchLetters As Boolean = True 'set flag to true before entering the routine. It will be set to false on no match If word.Length = WordLength Then 'for speed only check words of the correct length For Letter As Integer = 0 To word.Length - 1 'check each letter in turn 'ignore blank spaces 'if letter does not match, set flag to false If Letters(Letter) <> "" AndAlso Letters(Letter) <> word.Substring(Letter, 1) Then MatchLetters = False Next 'when word is checked, if the flag is still true, add it to the listbox If MatchLetters = True Then ListBox1.Items.Add(word) End If End If Next lblMatches.Text = ListBox1.Items.Count.ToString & " Matches" End Sub
vb.net Code:
Private Sub btnClear_Click(sender As System.Object, e As System.EventArgs) Handles btnClear.Click For CtrlPtr = 10 To 1 Step -1 'directcast allows us to reference the textboxes by their number DirectCast(Me.Controls("TextBox" & CtrlPtr.ToString), TextBox).Clear() DirectCast(Me.Controls("TextBox" & CtrlPtr.ToString), TextBox).BackColor = Control.DefaultBackColor Next End Sub
vb.net Code:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load 'the files are called "A Words" "B Words" etc. CHR65 to 90 represent capital A to Z ' For LetterRef As Integer = 65 To 90 'create the filename ' MyFile = Chr(LetterRef) & " Words.csv" MyFile = "dictionary.txt" 'read it Dim f As Integer = 0 For f = 1 To Words.Length - 1 'add to the final list Dict.Add(Words(f)) Next NoOfWords = f 'save the number of words to be used later by the progressbar ProgressBar1.Maximum = CInt(NoOfWords) ProgressBar1.Minimum = 0 ' Next