[RESOLVED] System.NullReferenceException Error
Hello,
I am getting NullReferenceException error in the Controls("ComboBox" & m).Text = "No" and probably i didn't define ComboBox and i don't know how to do it. Can you please help?
Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim m As Integer
For m = 35 To 77
Controls("ComboBox" & m).Text = "No"
Next m
End Sub
Re: System.NullReferenceException Error
Indexing the Controls collection with a name that doesn't exist will return Nothing, and you can't get a property of Nothing. Why do you think that ComboBoxes will just appear out of thin air? If you want ComboBoxes on your form then add them to your form. That said, if you added them to some other container, e.g. a Panel, then they will be in the Controls collection of that container, not of the form.
Re: System.NullReferenceException Error
Quote:
Originally Posted by
jmcilhinney
Indexing the Controls collection with a name that doesn't exist will return Nothing, and you can't get a property of Nothing. Why do you think that ComboBoxes will just appear out of thin air? If you want ComboBoxes on your form then add them to your form. That said, if you added them to some other container, e.g. a Panel, then they will be in the Controls collection of that container, not of the form.
Hello jmcilhinney,
You are like The Dooo of programing :) I added the ComboBoxes in TabControl1, does this make a difference? If so how can i put it to my code?
Re: System.NullReferenceException Error
Code:
TabControl1.TabPages(x).Controls("ComboBox" & m).text
where x is the index of the tabpage
Re: System.NullReferenceException Error
Quote:
Originally Posted by
Delaney
Code:
TabControl1.TabPages(x).Controls("ComboBox" & m).text
where x is the index of the tabpage
If the TabPage was added to the TabControl in the designer then there will be a field for it, just like other controls. In that case, you would generally refer to the TabPage by that field, e.g.
Code:
TabPage1.Controls("ComboBox" & m).Text = "No"
Re: System.NullReferenceException Error
Thank you so much guys, solved the problem:wave:
Re: System.NullReferenceException Error
I have one more question. When i try to use the same method with SelectedIndex = -1, it's saying "SelectedIndex is not a member of Control". How can i combine it with the Control method?
Re: System.NullReferenceException Error
Indexing the Controls collection returns a Control reference, so you can only access members of the Control class via that reference. As is always the case, if the actual object referred to is a more specific type and you want to access members of that type then you need to cast the reference you have as that type. The analogy I always use is a vet who knows that all their patients are animals but not what specific type of animal they will be, so can only do things that apply to all animals until the owner takes their pet out of the box and reveals the specific type of animal it is. If you don't know how to cast in VB then that's what you should research.
Re: System.NullReferenceException Error
Solved it, thanks again :)
Code:
TabPage1.Controls("ComboBox" & m).Text = Nothing
Re: System.NullReferenceException Error
The other option would be:
Code:
DirectCast(TabPage1.Controls("ComboBox" & m),ComboBox).SelectedIndex = -1
Re: System.NullReferenceException Error
Quote:
Originally Posted by
Shaggy Hiker
The other option would be:
Code:
DirectCast(TabPage1.Controls("ComboBox" & m),ComboBox).SelectedIndex = -1
Thank you, working great :)